Java Maven MOJO - 从项目 POM 中获取信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10606293/
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
Java Maven MOJO - getting information from project POM
提问by Xeperis
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 ?
我正在开发一个 Maven 插件。我似乎很难弄清楚,从执行 MOJO 的项目中获取 POM 信息的好方法是什么?
For instance if I execute my mojo in another maven project I would like to get project name or some other parameters.
例如,如果我在另一个 Maven 项目中执行我的 mojo,我想获取项目名称或其他一些参数。
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 ?
还有一件事,AbstractMojo.java 类中有一个上下文 MAP,有一个私有 Map pluginContext,如果我错了,有人可以纠正我,但这应该用于在 mojos 之间传递信息吗?
回答by J?rn Horstmann
You can inject the current maven project into your mojo with a field declared like this:
您可以使用如下声明的字段将当前的 maven 项目注入到您的 mojo 中:
/**
* @parameter default-value="${project}"
* @required
* @readonly
*/
MavenProject 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:
然后只需调用即可获得项目名称project.getName()
。要使用此 API,您需要添加maven-project
工件作为依赖项:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.6</version>
</dependency>
回答by Jesse Glick
@Component
private MavenProject project;
also works (more succinctly and intuitively) if using the new maven-plugin-annotations
, which is the default for new mojos created from maven-archetype-plugin
.
如果使用 new 也可以(更简洁直观)maven-plugin-annotations
,这是从maven-archetype-plugin
.
EDIT(thanks to @bmargulies): although the @Component
Javadoc as of 3.2 suggests using it for MavenProject
, apparently that is deprecated and the suggestion is dropped as of 3.3; the idiom suggestedby maven-plugin-tools-annotations
(as of 3.3) is something like this (both seem to work):
编辑(感谢@bmargulies):尽管@Component
3.2的Javadoc 建议将其用于MavenProject
,但显然已弃用并且该建议从 3.3 开始被删除;该建议成语的maven-plugin-tools-annotations
(如3.3)是这样的(似乎都工作):
@Parameter(defaultValue="${project}", readonly=true, required=true)
private MavenProject project;
回答by Partly Cloudy
The preferred syntax is now:
现在首选的语法是:
@Parameter(defaultValue = "${project}", required = true, readonly = true)
MavenProject project;
You will have to add a dependency for maven-project
to your plugin's pom:
您必须为maven-project
插件的 pom添加一个依赖项:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.6</version>
</dependency>
(Thanks to others who have supplied this information already. This answer combines them in one place.)
(感谢已经提供此信息的其他人。这个答案将它们组合在一个地方。)