java代码上传到私服,java生成 java代码 上传maven私服
功能:利用java 生成java類 ,并上傳至maven私服
使用開源框架:https://github.com/square/javapoet JavaWriter分支
環境:jdk1.8 mavem
import com.squareup.javawriter.JavaWriter;
public class TestClz {
@Test
public void genrateJava() throws IOException {
enumDeclaration();
isOSLinux();
Runtime runtime=Runtime.getRuntime();
try {
Process exec1 = runtime.exec("cmd /k cd D:\\javawriter\\generate && javac TestEnum.java && jar cvf test.jar TestEnum.class &&" +
" mvn deploy:deploy-file -DgroupId=org.testulitmate -DartifactId=code-test -Dversion=0.0.2-SNAPSHOT -Dpackaging=jar -Dfile=D:/javawriter/generate/test.jar -Durl=http://0.0.0.0:8081/nexus/content/repositories/hry-snapshots -DrepositoryId=hry-snapshots");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*生成java類
**/
public static void enumDeclaration() throws IOException {
JavaWriter javaWriter = getReader("TestEnum");
// 生成的包名
javaWriter.emitPackage("com.javawriter.generate");
//定義生成類的信息 類名 類型 訪問修飾符 注釋
javaWriter.beginType("TestEnum", "enum", EnumSet.of(PUBLIC)).emitJavadoc("this is enum.");
javaWriter.emitEnumValues(Arrays.asList("ZIP","WAR","TAG","Z7"));
javaWriter.endType().close();
}
/**
*構建 javaWriter對象
**/
public static JavaWriter getReader(String className)throws IOException {
String packageName = "com.javawriter.generate";
File outFile = new File("src/main/java/" + packageName.replaceAll("\\.", "/") + "/" + className + ".java");
if(!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
if (!outFile.exists()) {
outFile.createNewFile();
}
System.out.println(outFile.getAbsolutePath());
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile));
JavaWriter javaWriter=new JavaWriter(writer);
return javaWriter;
}
/**
*判斷是否 是liunx
**/
public static boolean isOSLinux() {
Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
System.out.println(os);
if (os != null && os.toLowerCase().indexOf("linux") > -1) {
return true;
} else {
return false;
}
}
}
總結
以上是生活随笔為你收集整理的java代码上传到私服,java生成 java代码 上传maven私服的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 实例化 bean,Spring
- 下一篇: php配置文件修改数据库上传,请问php