用代码更新你的jar包
发布时间:2023-09-19 23:01:04
假设目录结构是maven标准结构
-srcpackage com.foo.common.base.utils.development;
-target
-test.jar(你需要更新的jar包)
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 项目启动之后执行自定义方法的两种方式在测试配置中心的配置时,想在项目启动成功之后打印配置项,然后需要执行自定义的类一般
- 解决C#中WebBrowser的DocumentCompleted事件不执行的实现方法 :使用WebBrowser的ProgressChan
- 报错信息一:jQuery.handleError is not a function 上传图片的时候,通过F12,查看到这个错误。
- 第一种方式:使用@Param注解方式此种方式用法是我们在接口中写方法的参数时,在每个参数的前面加上一个@Param注解即可。该注解有一个va
- 这个东西我已经用了有段时间了,从开始写文章就在用这个,主要原因还是因为我比较懒。懒得去寻找图片,同时又怕万一惹来版权争议。。。跟我所有的文章
- 本文演示如何在Android中实现ListView圆角效果。无论是网站,还是APP,人们都爱看一些新颖的视图效果。直角看多了,就想看看圆角,
- springboot环境切换失效概述最近在使用-Dspring.profiles.active=te 来切换spring-boot的环境时,
- 关于MouseWheelListener的鼠标滚轮事件Java中JPanel面板中对鼠标滚轮事件的处理。一、MouseWheelListen
- 在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的。对于不同公众号
- 概述在JDK 1.8里,可以使用如下代码获取List元素对象中某个属性的列表。package test;import java.util.A
- 一、事务的基本原理Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的。对于纯JDBC操
- What will be removed If you click Clear Data Button in the System Appl
- 内部类的介绍定义在另外一个类中的类,叫内部类成员内部类1..new 创建成员内部类必须先创建外部类的实例,然后通过.new 创建内部类的对象
- 一、引言在刷算法的时候经常需要对数组进行排序,第一反应就是直接使用java.util包下的Arrays.sort()方法直接排序。但在刷算法
- AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Map
- GridView是类似于ListView的控件,只是GridView可以使用多个列来呈现内容,而ListView是以行为单位,所以用法上是差
- 一、OutputStreamWriter流 API说明:OutputStreamWriter是从字符流到
- 最近在机顶盒上做一个gridview,其焦点需要在item的子控件上,但gridview的焦点默认在item上,通过android:desc
- 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现。对于文件上传,浏览器在上
- 今天的几个目标: 1. 自定义ActionProvider 2. Toolbar ActionBar自定义Menu 3. Toolbar A