Java Maven MOJO - 从项目POM获取信息 [英] Java Maven MOJO - getting information from project POM

查看:205
本文介绍了Java Maven MOJO - 从项目POM获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个maven插件。我似乎很难搞清楚,从执行MOJO的项目中获取POM信息的好方法是什么?

I am working on a maven plugin. I seem to have a hard time figuring out, what would be a good way to get POM information from project in which you execute the MOJO ?

例如,如果我执行我的mojo在另一个maven项目中我想得到项目名称或其他一些参数。

For instance if I execute my mojo in another maven project I would like to get project name or some other parameters.

还有一件事,在AbstractMojo.java类中有一个上下文MAP有私有映射pluginContext,如果我错了,有人可以纠正我,但是这可以用来在mojos之间传递信息吗?

And one more thing, there is a context MAP in AbstractMojo.java class there is private Map pluginContext, could someone correct me if I am wrong but this is suppose to be used for passing information between mojos ?

推荐答案

你可以使用如下声明的字段将当前的maven项目注入到你的mojo中:

You can inject the current maven project into your mojo with a field declared like this:

/**
 * @parameter default-value="${project}"
 * @required
 * @readonly
 */
MavenProject project;

然后只需调用 project.getName()<即可获得项目名称/ code>。
要使用此API,您需要将 maven-project 工件添加为依赖项:

The projects name is then available by simply calling project.getName(). To use this API, you need to add the maven-project artifact as a dependency:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-project</artifactId>
    <version>2.0.6</version>
</dependency>

这篇关于Java Maven MOJO - 从项目POM获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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