用代码更新你的jar包

所属分类: 软件编程 / java 阅读数: 46
收藏 0 赞 0 分享

假设目录结构是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 SpEL语法扫盲与查询手册的实现

这篇文章主要介绍了SpringBoot SpEL语法扫盲与查询手册的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Java创建子线程的两种方法

这篇文章主要介绍了Java创建子线程的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot2.x集成JPA快速开发的示例代码

这篇文章主要介绍了Spring Boot2.x集成JPA快速开发,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

关于Java中的mysql时区问题详解

这篇文章主要给大家介绍了关于Java中mysql时区问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

JAVA参数传递方式实例浅析【按值传递与引用传递区别】

这篇文章主要介绍了JAVA参数传递方式,结合实例形式分析了java按值传递与引用传递区别及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中MessageDigest来实现数据加密的方法

这篇文章主要介绍了Java中MessageDigest来实现数据加密的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

spring 注解验证@NotNull等使用方法

这篇文章主要介绍了spring 注解验证@NotNull等使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

浅谈如何优雅地停止Spring Boot应用

这篇文章主要介绍了浅谈如何优雅地停止Spring Boot应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Python如何使用@property @x.setter及@x.deleter

这篇文章主要介绍了Python如何使用@property @x.setter及@x.deleter,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java Jmeter全局变量设置过程图解

这篇文章主要介绍了Java Jmeter全局变量设置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多