我想从 Java 执行 Maven 命令来开发插件。我试过maven-embedder
了,但现在好像不支持了。有人知道其他可以使用的工具吗?
问问题
34400 次
3 回答
56
一个简单的调用 API:maven-invoker。
项目文档:http ://maven.apache.org/shared/maven-invoker/
用法:http ://maven.apache.org/shared/maven-invoker/usage.html
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Arrays.asList( "clean", "install" ) );
Invoker invoker = new DefaultInvoker();
invoker.execute( request );
于 2014-02-03T18:00:35.810 回答
17
使用Maven 嵌入器
Maven 嵌入器仍然受支持且易于配置,因此这对您来说是更好的选择。
Java 代码
MavenCli cli = new MavenCli();
cli.doMain(new String[]{"clean", "install"}, "project_dir", System.out, System.out);
项目配置
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-wagon</artifactId>
<version>0.9.0.M2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http-lightweight</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
于 2013-11-11T10:43:14.853 回答
5
确实不再支持 Maven 嵌入器(只有 hudson 仍在使用它)。但是,就像在 hudson 中一样,还有其他几种运行 maven 的方法。您可以简单地将 maven 作为外部程序运行:
Runtime.getRuntime().exec("mvn clean install");
或者您可以考虑为 maven 创建一个ant 脚本。 然后可以将该脚本作为外部程序调用,或者(如果您需要更多控制)将 ant 添加到您的类路径并调用 Antrunner。
更新
现在再次支持 Maven 嵌入器,因此这是您的最佳选择。
于 2011-02-28T12:20:34.390 回答