Maven:如何在Mojos之间传递参数?

发布于 2024-09-29 18:33:06 字数 123 浏览 1 评论 0原文

如何对一个 Mojo 进行编程以设置另一个 Mojo 的配置?例如:Mojo A 需要定义配置参数A.foo。用户可以手动指定 A.foo 或运行插件 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的巷 2024-10-06 18:33:06

回答我自己的问题:

可以使用 MavenProject 实例在运行时访问插件的配置或项目范围的属性:

/**
 * The maven project.
 * 
 * @parameter expression="${project}"
 * @readonly
 */
 private MavenProject project;

然后您可以在运行时访问插件的配置:

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
...

注意:任何配置更改都会在当前阶段结束。

来源:从自定义 mojo 访问 Maven 插件运行时配置的最佳方式?

或者,您可以使用 MavenProject.getProperties() 获取/设置项目范围的参数。

Answering my own question:

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.

Source: Best way to access the runtime configuration of a maven plugin from a custom mojo?

Alternatively, you can get/set project-wide parameters using MavenProject.getProperties().

白龙吟 2024-10-06 18:33:06

事实证明这是一件棘手的事情,主要是由于 Maven 运行时“配置”插件的时间安排。从 getBuildPlugins 更改“配置”通常不起作用。

如果您正在编写目标插件,最好的方法是默认值,否则使用属性。

有了属性,但你必须小心如何使用这些属性。需要注意的是,如果您的 POM(或任何父级)定义了属性的值,那么在加载 POM 时,${property} 引用将被替换。但是,如果没有“property”属性,则 ${property} 引用将保留,并且仅在最后可能的时刻用空值替换。

“默认值”也在最后可能的时刻进行评估,我认为这是一个更安全的解决方案,因为有一个逻辑原因为什么必须在最后可能的时刻评估它,因为不存在的属性可能只是Maven 未来版本中可能会更改的实现细节。

就我而言,我不得不求助于属性,因为我想控制 Surefire 插件的“classesDirectory”。当 Cobertura 未运行时,我希望它继续默认为 ${project.build.outputDirectory},但是当 Cobertura 运行时,我希望它使用 ${project.build.outputDirectory}/ generated-classes/cobertura。

在你的插件部分中定义:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>${maven-surefire-plugin.version}</version>
 <configuration>
  <classesDirectory>${instrumentedClassesDirectory}</classesDirectory>
 </configuration>
</plugin>

然后在“源”插件中:

getProject().getProperties().put("instrumentedClassesDirectory", coberturaDir);

并确保在任何情况下都不要在任何 POM 中放置类似以下内容:

<properties>
  <instrumentedClassesDirectory>${project.build.outputDirectory}</instrumentedClassesDirectory>
</properties>

因为如果你这样做,即使该属性的值是由你的源插件设置的,你的目标插件将看不到该值。如果您使用传递到源插件默认值的属性,您可以忽略最后一个警告,但正如我所说,这不起作用,因为我不想更改 project.build.outputDirectory 的值。

This turns out to be a tricky thing to do mostly due to the timing of "configuration" of the plugins by the Maven runtime. Changing the "configuration" from getBuildPlugins is usually not going to work.

The best method is default-value if you are writing the target plugin, otherwise use properties.

With properties, but you have to be careful about how you use the properties. The caution is to realize that if your POM (or any parent) defines a value for a property, then the ${property} reference will be replaced when the POM is loaded. However if there is no "property" property then the ${property} reference stays and is only replaced with a null value at the last possible moment.

"default-value" is also evaluated at the last possible moment, and I think this is a safer solution because there is a logical reason why it must be evaluated at the last possible moment, where-as the non-existent property may just be an implementation detail that could change in future versions of Maven.

In my case I had to resort to properties because I wanted to control the "classesDirectory" of the surefire plugin. I wanted it to continue to default to ${project.build.outputDirectory} when Cobertura was not run, but when Cobertura was run I wanted it to use ${project.build.outputDirectory}/generated-classes/cobertura.

Define in your plugins section:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>${maven-surefire-plugin.version}</version>
 <configuration>
  <classesDirectory>${instrumentedClassesDirectory}</classesDirectory>
 </configuration>
</plugin>

Then in the "source" plugin:

getProject().getProperties().put("instrumentedClassesDirectory", coberturaDir);

And make sure under no circumstances ever put anything like the following in any POM:

<properties>
  <instrumentedClassesDirectory>${project.build.outputDirectory}</instrumentedClassesDirectory>
</properties>

because if you do, even though the value of the property is set by your source plugin, your destination plugin will not see the value. You can ignore this last caveat if your using a property passed into the default-value of the source plugin, but as said in my case that would not work because I did not want to change the value of project.build.outputDirectory.

违心° 2024-10-06 18:33:06

我想 Maven 的方法是在第一个 Mojo 中设置一个属性,然后从另一个 Mojo 访问它。

I guess the maven way would be to set a property in the first Mojo and to access it from the other Mojo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文