Springboot 整合maven 一键打包

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Springboot 整合maven 一键打包

孤独的帅哥   2022-05-28 我要评论

maven release plugin配置

参考 https:  配置好pom。

整合maven-invoker使程序去执行mvn命令

1.导包

<dependency>
  <groupId>org.apache.maven.shared</groupId>
  <artifactId>maven-invoker</artifactId>
  <version>3.1.0</version>
</dependency>

注意maven-invoker版本。版本过低可能导致不兼容。

2.测试程序

public class MavenTest {
    //直接执行mvn release:prepare -X 为交互式执行,无法在程序进行时输入版本参数。
    //而执行mvn -B release:prepare -X 则可以通过程序直接执行。但是若要自定义版本信息则需要配置pom release plugin的参数,或采用如下命令
    //实际执行的mvn命令.
    //mvn -B release:prepare -X -Dtag=VersionControlDemo-"0.4.0" -DreleaseVersion="0.4.0" -DdevelopmentVersion="0.4.1-SNAPSHOT"
    //mvn release:perform -X
    public static void main(String[] args) throws MavenInvocationException {
        InvocationRequest request = new DefaultInvocationRequest();
        //获取pom文件地址
        String relativelyPath=System.getProperty("user.dir");
        String pomPath = relativelyPath+"/pom.xml";
        System.out.println(pomPath);
        request.setPomFile(new File(pomPath));
        Invoker invoker = new DefaultInvoker();
        //获取maven环境变量地址
        String m2Path = System.getenv("MAVEN_HOME");
        System.out.println("m2Path:"+m2Path);
        invoker.setMavenHome(new File(m2Path));
        try {
            //该版本tag信息
            String tag = "VersiongControlDemo-0.4.0";
            //此次发布的版本号
            String version = "0.4.0";
            //下一次版本的快照版本号
            String developmentVersion="0.4.1-SNAPSHOT";

            List<String> goals = new ArrayList<>();
            goals.add("-B");
            goals.add("release:prepare");
            goals.add("-X");
            goals.add("-Dtag=" + tag);
            goals.add("-DreleaseVersion=" + version);
            goals.add("-DdevelopmentVersion=" + developmentVersion);
            request.setGoals(goals);
            System.out.println("开始: " + request.getGoals());
            invoker.execute(request);
            request.setGoals(Collections.singletonList("release:perform -X"));
            System.out.println("开始:  " + request.getGoals());
            invoker.execute(request);
        } catch (MavenInvocationException e) {
            e.printStackTrace();
            return;
        }
    }
}

运行测试demo。结果成功。(每次运行前都要注意版本参数,切勿冲突。)

同理可配置release:rollback等相关回滚命令。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们