国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

用代碼更新你的jar包

2019-11-26 15:44:31
字體:
來源:轉載
供稿:網友

假設目錄結構是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());
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 射阳县| 舟山市| 北流市| 龙海市| 仁怀市| 建德市| 阿尔山市| 洪湖市| 宾阳县| 永平县| 襄城县| 西乡县| 宁乡县| 抚宁县| 信丰县| 林甸县| 永安市| 阳新县| 仙居县| 伊宁市| 永宁县| 达孜县| 天台县| 德钦县| 方山县| 昌吉市| 高安市| 平泉县| 资兴市| 墨脱县| 云林县| 运城市| 兴国县| 孝义市| 万山特区| 澳门| 丹棱县| 沈阳市| 镇巴县| 中牟县| 三明市|