Springboot 整合maven插口调用maven release plugin。实现一键打包功能。
maven release plugin配置
参考https://www.cnblogs.com/jiujixin/p/16003321.html 配置好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。结果成功。(每次运行前都要注意版本参数,切勿冲突。)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律