디렉토리 구조가 Maven 표준 구조라고 가정합니다.
다음과 같이 코드 코드를 복사합니다.
-src
-목표
-test.jar(업데이트해야 하는 jar 패키지)
다음과 같이 코드 코드를 복사합니다.
패키지 com.foo.common.base.utils.development;
import static org.junit.Assert.*;
java.io.파일 가져오기;
import java.io.IOException;
java.util.Date 가져오기;
java.util.Properties 가져오기;
org.apache.commons.io.FileUtils 가져오기;
org.junit.Test 가져오기;
org.slf4j.Logger 가져오기;
org.slf4j.LoggerFactory 가져오기;
org.springframework.core.io.ClassPathResource 가져오기;
공개 클래스 JarUpdater {
공개 정적 최종 로거 로거 = LoggerFactory
.getLogger(JarUpdater.class);
@시험
/**
* com 디렉토리의 모든 파일을 jar의 해당 디렉토리 구조로 업데이트합니다.
*
* 성공적인 코드 업데이트에서는 jar의 크기가 다르다고 주장합니다.
*/
public void updateClass()는 IOException, InterruptedException을 발생시킵니다.
ClassPathResource myPath = 새로운 ClassPathResource(
"jarUpdaterConfig.properties");
속성 p = 새 속성();
p.load(myPath.getInputStream());
ClassUpdater classUpdater = new ClassUpdater().applySettings(p);
classUpdater.compileAndCopyClass();
String WorkingDirectory = p.getProperty("workingDirectory");
String jar4UpdateName = p.getProperty("jar4UpdateName");
// 클래스 컴파일 경로
문자열 updateSourcePath = WorkingDirectory + "대상";
// 클래스 루트 폴더
문자열 updateSourceDir = "com";
날짜 startDate = new Date();
File myJar = new File(workingDirectory + jar4UpdateName);
if (!myJar.isFile()) {
logger.error("다음 경로가 {}인 파일이 존재하지 않습니다.",
jar4업데이트이름);
반품;
}
long oldLength = myJar.length();
logger.info("이제 이름:{},크기:{}로 jar 파일을 업데이트할 준비가 되었습니다.",
myJar.getName(), myJar.length());
문자열 myCommand = "jar uf " + WorkingDirectory + jar4UpdateName
+ " -C " + updateSourcePath + " " + updateSourceDir;
logger.info("업데이트 명령【{}】", myCommand);
Runtime.getRuntime().exec(myCommand);
while (!FileUtils.isFileNewer(myJar, startDate)) {
logger.info("2초 동안 대기, 변경 사항 확인 중...");
Thread.sleep(2000);
}
주장NotEquals(
"jar이 성공적으로 업데이트되지 않았을 수 있습니다. 코드를 확인하세요.",
oldLength, myJar.length());
logger.info("이제 크기:{}로 jar 파일 업데이트를 완료합니다.", myJar.length());
}
}