如何在不使用进程调用的情况下从插件中以编程方式调用Maven?

时间:2012-07-17 19:15:33

标签: maven maven-plugin maven-3

我想在插件中调用Maven的目标,并且不愿意接受将进程作为初始化的成本。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:0)

我怀疑这是一种受支持的方法,但它对我有用。

/**
 * @parameter expression="${session}"
 * @required
 * @readonly
 */
 private MavenSession session;

/**
 * @component
 * @readonly
 */
 private Maven maven;
/**
 * @parameter default-value="${project.remoteArtifactRepositories}"
 * @readonly
 **/
protected List<ArtifactRepository> remoteArtifactRepositories;
/**
 * The project's remote repositories to use for the resolution of plugins
 * and their dependencies.
 * 
 * @parameter default-value="${project.pluginArtifactRepositories}"
 * @readonly
 */
private List<ArtifactRepository> pluginArtifactRepositories;
/**
 * @parameter default-value="${localRepository}"
 * @readonly
 * */
private ArtifactRepository localRepository;

...

List<String> goals = ...
File pomFile = ...
MavenExecutionRequest req = new DefaultMavenExecutionRequest();
req.setPom(pomFile);
req.setBaseDirectory(pomFile.getParentFile());
req.setGoals(goals);
req.setProxies(session.getSettings().getProxies());
req.setMirrors(session.getSettings().getMirrors());
req.setLocalRepository(localRepository);
req.setRemoteRepositories(remoteArtifactRepositories);
req.setPluginArtifactRepositories(pluginArtifactRepositories);
MavenExecutionResult result = session.execute(req);

要自省处理,您可以继承AbstractExecutionListener并使用req.setExecutionListener()来应用它。

答案 1 :(得分:0)

我错过了提到的是:

http://timmoore.github.com/mojo-executor/