用代码更新你的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());
}
}
相关文章
SpringCloud应用idea实现可相互调用的多模块程序详解
IDEA 全称 IntelliJ IDEA,是java编程语言的集成开发环境。IntelliJ在业界被公认为最好的Java开发工具,尤其在智能代码助手、代码自动提示、重构、JavaEE支持、各类版本工具(git、svn等)、JUnit、CVS整合、代码分析、 创新的GUI设计等方面的功能可以说是超常的2022-07-07浅谈java Iterator.remove()方法的用法(详解)
下面小编就为大家带来一篇浅谈java Iterator.remove()方法的用法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-01-01Spring boot详解fastjson过滤字段为null值如何解决
这篇文章主要介绍了解决Spring boot中fastjson过滤字段为null值的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-07-07
最新评论