java 如何以编程方式调用 Maven 任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2146580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically call a Maven-task
提问by pmf
I'm using Maven in the context of another build-tool (leiningen for Clojure, but this should not matter), and I would like to know how I would call a plugin like dependency:build-classpathprogrammatically (i.e. via the Maven-API, not via the mvn-command).
我在另一个构建工具的上下文中使用 Maven(Clojure 的 leiningen,但这应该无关紧要),我想知道如何以编程方式调用像依赖项:build-classpath这样的插件(即通过 Maven- API,而不是通过mvn-command)。
采纳答案by lexicore
See how org.apache.maven.plugin.testing.AbstractMojoTestCase from maven-plugin-testing-harness is implemented. Here's a code snippet from some of my tests which may be helpful.
查看 maven-plugin-testing-harness 中的 org.apache.maven.plugin.testing.AbstractMojoTestCase 是如何实现的。这是我的一些测试中的代码片段,可能会有所帮助。
public abstract class JAXBGenerateTest extends AbstractMojoTestCase {
static {
System.setProperty("basedir", getBaseDir().getAbsolutePath());
}
protected MavenProjectBuilder mavenProjectBuilder;
protected void setUp() throws Exception {
super.setUp();
mavenProjectBuilder = (MavenProjectBuilder) getContainer().lookup(
MavenProjectBuilder.ROLE);
}
protected static File getBaseDir() {...}
/**
* Validate the generation of a java files from purchaseorder.xsd.
*
* @throws MojoExecutionException
*/
public void testExecute() throws Exception {
final File pom = new File(getBaseDir(),
"src/test/resources/test-pom.xml");
final ArtifactRepository localRepository = new DefaultArtifactRepository( "local",
new File(getBaseDir(), "target/test-repository").toURI().toURL().toString() , new DefaultRepositoryLayout());
final MavenProject mavenProject = mavenProjectBuilder.build(pom, localRepository, null);
final XJC2Mojo generator = (XJC2Mojo) lookupMojo("generate", pom);
generator.setProject(mavenProject);
generator.setLocalRepository(localRepository);
generator.setSchemaDirectory(new File(getBaseDir(),"src/test/resources/"));
generator.setSchemaIncludes(new String[] { "*.xsd" });
generator.setBindingIncludes(new String[] { "*.xjb" });
generator.setGenerateDirectory(new File(getBaseDir(), "target/test/generated-sources"));
generator.setVerbose(true);
generator.setGeneratePackage("unittest");
generator.setRemoveOldOutput(false);
generator.execute();
}
}
回答by Ring
Use the Maven Invoker API. Its quick and easy.
使用Maven 调用程序 API。它快速简便。
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile( new File( "/path/to/pom.xml" ) );
request.setGoals( Collections.singletonList( "install" ) );
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File("/usr"));
try
{
invoker.execute( request );
}
catch (MavenInvocationException e)
{
e.printStackTrace();
}
pom.xml:
pom.xml:
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>2.1.1</version>
</dependency>
回答by Gili
Here's a better solution: use the Mojo-Executorlibrary. It exposes a simple Java API for invoking Maven plugins.
这是一个更好的解决方案:使用Mojo-Executor库。它公开了一个用于调用 Maven 插件的简单 Java API。
回答by Thomas Jung
The Ant Maven taskscan give you an idea how to do it (DependenciesTask source). Another route may be the PomModuleDescriptorParserused by Ivy.
该蚂蚁Maven的任务可以给你一个想法如何做到这一点(DependenciesTask源)。另一条路线可能是Ivy使用的PomModuleDescriptorParser。

