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;
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值