问题描述
如何编程一个Mojo以设置另一个Mojo的配置?例如:Mojo A要求定义配置参数A.foo
.用户可以手动指定A.foo
或运行插件B,插件B将为其计算值.
How do I program one Mojo to set another Mojo's configuration? For example: Mojo A requires configuration parameter A.foo
to be defined. A user can either specify A.foo
manually or run plugin B which will calculate the value for him/her.
推荐答案
回答我自己的问题:
可以使用MavenProject
实例在运行时访问插件的配置或项目范围的属性:
It's possible to access a plugin's configuration or project-wide properties at runtime using a MavenProject
instance:
/**
* The maven project.
*
* @parameter expression="${project}"
* @readonly
*/
private MavenProject project;
然后您可以在运行时访问插件的配置:
You can then access a plugin's configuration at runtime:
private Plugin lookupPlugin(String key)
{
List plugins = getProject().getBuildPlugins();
for (Iterator iterator = plugins.iterator(); iterator.hasNext();)
{
Plugin plugin = (Plugin) iterator.next();
if(key.equalsIgnoreCase(plugin.getKey()))
return plugin;
}
return null;
}
...
Xpp3Dom configuration = (Xpp3Dom) Plugin.getConfiguration()
configuration.getChild("parameterName"); // get parameter
configuration.addChild(new Xpp3Dom("parameterName")); // add parameter
...
注意:在当前阶段结束时,所有配置更改都将被丢弃.
Note: Any configuration changes are discarded at the end of the current phase.
来源:
或者,您可以使用MavenProject.getProperties()
获取/设置项目范围的参数.
Alternatively, you can get/set project-wide parameters using MavenProject.getProperties()
.
这篇关于Maven:如何在Mojos之间传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!