温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • Springboot如何整合maven插口调用maven release plugin实现一键打包功能

Springboot如何整合maven插口调用maven release plugin实现一键打包功能

发布时间:2022-03-15 12:54:02 来源:亿速云 阅读:152 作者:小新 栏目:开发技术

小编给大家分享一下Springboot如何整合maven插口调用maven release plugin实现一键打包功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

maven release plugin配置

整合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。结果成功。(每次运行前都要注意版本参数,切勿冲突。)

Springboot如何整合maven插口调用maven release plugin实现一键打包功能

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

看完了这篇文章,相信你对“Springboot如何整合maven插口调用maven release plugin实现一键打包功能”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI