用代碼更新你的jar包。本站提示廣大學習愛好者:(用代碼更新你的jar包)文章只能為提供參考,不一定能成為您想要的結果。以下是用代碼更新你的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());
}
}