使用Maven 3 API进行编程构建 [英] Programmatic builds using Maven 3 API

查看:172
本文介绍了使用Maven 3 API进行编程构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你问为什么我可以产生一个进程来执行mvn之前,我希望通过Maven API执行一个Maven构建,这样我就可以收集关于构建中发生的事情,产生的工件等信息。取决于 org.apache.maven:maven-core:jar:3.0.4 ,我写了以下方法试图做这样的事情:

Before you ask why I can just spawn a process to execute mvn, I wish to execute a Maven build through the Maven API, so that I can gather info on what goes on in the build, the artifacts produced etc etc. After depending on org.apache.maven:maven-core:jar:3.0.4, I have written the following method in an attempt to do such a thing:

public static void build(File directory, File pom) {
    Maven maven = new DefaultMaven();
    MavenExecutionRequest exec = new DefaultMavenExecutionRequest();
    exec.setBaseDirectory(directory);
    exec.setPom(pom);
    MavenExecutionResult result = maven.execute(exec);
    MavenProject proj = result.getProject();
    Artifact art = proj.getArtifact();
    System.out.println(art);
}

但是此代码在 maven.execute失败由于空指针异常。由于 DefaultMaven 中的私有字段未被初始化,因此这些空指针异常基本上无处不在。它们都用 @Required 注释,所以我猜这与Plexus有关。

However this code fails at maven.execute due to null pointer exceptions. These null pointer exceptions are basically everywhere due to private fields in DefaultMaven not being initialized. They are all annotated with @Required, so I am guessing this is something to do with Plexus.

怎么能我成功使用Maven来执行这样的构建吗?

How can I successfully use Maven to execute such a build?

推荐答案

您将需要使用实际的Maven嵌入API:

You'll want to use the actual Maven embedding API:

http:/ /maven.apache.org/ref/3.0/maven-embedder/apidocs/index.html

要查看示例,请查看M2Eclipse的开源。

To see examples, look towards the open source of M2Eclipse.

现在,这个组件的命名并不是很好。它实际上是一个旨在制作CLI的便利包装器。所以,你要做的就是阅读它的来源。

Now, this component is not really very well-named. It's actually a convenience wrapper aimed at making an CLI. so, what you'll want to do is read the source of it.

这篇关于使用Maven 3 API进行编程构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆