用代码更新你的jar包
假设目录结构是maven标准结构
-src
-target
-test.jar(你需要更新的jar包)
package com.foo.common.base.utils.development;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
public class JarUpdater {
public static final Logger logger = LoggerFactory
.getLogger(JarUpdater.class);
@Test
/**
* 更新com目录下的所有文件到jar的对应目录结构中去
*
* 一次成功的代码更新,我们断言jar的大小是不一样的
*/
public void updateClass() throws IOException, InterruptedException {
ClassPathResource myPath = new ClassPathResource(
"jarUpdaterConfig.properties");
Properties p = new Properties();
p.load(myPath.getInputStream());
ClassUpdater classUpdater = new ClassUpdater().applySettings(p);
classUpdater.compileAndCopyClass();
String workingDirectory = p.getProperty("workingDirectory");
String jar4UpdateName = p.getProperty("jar4UpdateName");
// class compile path
String updateSourcePath = workingDirectory + "target";
// class root folder
String updateSourceDir = "com";
Date startDate = new Date();
File myJar = new File(workingDirectory + jar4UpdateName);
if (!myJar.isFile()) {
logger.error("file with following path {} does not exist.",
jar4UpdateName);
return;
}
long oldLength = myJar.length();
logger.info("Now ready to update jar file with name:{},size:{}",
myJar.getName(), myJar.length());
String myCommand = "jar uf " + workingDirectory + jar4UpdateName
+ " -C " + updateSourcePath + " " + updateSourceDir;
logger.info("Update command【{}】", myCommand);
Runtime.getRuntime().exec(myCommand);
while (!FileUtils.isFileNewer(myJar, startDate)) {
logger.info("sleep for two seconds,checking changes...");
Thread.sleep(2000);
}
assertNotEquals(
"jar may not be updated successfully,check the code please",
oldLength, myJar.length());
logger.info("Now finish update jar file with size:{}", myJar.length());
}
}
上一篇:Springboot整合MongoDB的Docker开发教程全解
栏 目:JAVA代码
本文标题:用代码更新你的jar包
本文地址:http://www.codeinn.net/misctech/65960.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




