I'm writing a Maven plugin (Mojo) that needs to execute a standard set of other plugin executions before it is run.
我正在编写一个Maven插件(Mojo),它需要在运行之前执行一组标准的其他插件执行。
Is there a mechanism to declare all the goals within my plugin so I don't have to rely on the user defining them all in their POM?
是否有一种机制可以在我的插件中声明所有的目标,这样我就不必依赖用户在POM中定义它们了?
1 个解决方案
#1
14
You can do this by defining a custom lifecycle and invoking that lifecycle before your Mojo is executed via the execute
annotation.
可以通过定义自定义生命周期并在通过execute注释执行Mojo之前调用该生命周期来实现这一点。
In your Mojo, declare in the Javadoc the lifecycle to be executed:
在Mojo中,在Javadoc中声明要执行的生命周期:
/**
* Invoke the custom lifecycle before executing this goal.
*
* @goal my-goal
* @execute lifecycle="my-custom-lifecycle" phase="process-resources"
*/
public class MyMojo extends AbstractMojo {
...
Then define a custom lifecycle in src/main/resources/META-INF/maven/lifecycle.xml.
然后在src/main/resources/META-INF/maven/lifecycle.xml中定义自定义生命周期。
The lifecycle is a bit like plexus' components.xml, but allows you to specify configuration for those goals.
生命周期有点像plexus的组件。但是允许您为这些目标指定配置。
Note the syntax is slightly different to plugin configurations in the pom. You define a goal using : as a separator rather than specifying separate groupId, artifactId and version elements, otherwise it is largely the same notation as the execution element of a plugin configuration in the pom. You can even use some properties in the lifecycle.xml (though possibly not all properties are supported, I'll need to check that).
注意,pom中的语法与插件配置稍有不同。您可以使用:作为分隔符来定义目标,而不是指定单独的groupId、artifactId和版本元素,否则它基本上与pom中插件配置的执行元素相同。您甚至可以在生命周期中使用一些属性。xml(虽然可能不支持所有属性,但我需要检查一下)。
The following example invokes the dependency plugin twice with different configurations in the process-resources phase:
下面的示例在流程资源阶段使用不同的配置两次调用依赖插件:
<lifecycles>
<lifecycle>
<id>download-dependencies</id>
<phases>
<phase>
<id>process-resources</id>
<executions>
<execution>
<goals>
<goal>
org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<includeTypes>war</includeTypes>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.outputDirectory}/wars
</outputDirectory>
</configuration>
</execution>
<execution>
<goals>
<goal>
org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<includeTypes>jar</includeTypes>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.outputDirectory}/jars
</outputDirectory>
</configuration>
</execution>
</executions>
</phase>
</phases>
</lifecycle>
</lifecycles>
With this approach, the dependency plugin will be invoked once with each configuration in the process-resources
phase of the forked lifecycle (all happening within the execution defined in the Mojo).
使用这种方法,依赖插件将在分叉生命周期的流程-资源阶段(所有这些都发生在Mojo定义的执行中)对每个配置调用一次。
In the lifecycle.xml, you can define multiple phases, and multiple executions per phase of the lifecycle. The available phases are defined in the Maven lifecycle.
在生命周期的。xml,您可以定义生命周期的每个阶段的多个阶段和多个执行。可用的阶段在Maven生命周期中定义。
You can find out more about lifecycles in the Creating a Custom Lifecycle section of the Maven book. It doesn't give an exhaustive list of what is allowed though. The only other reference I know if is from the Maven 2 alpha, so is possibly not that up-to-date
您可以在Maven book的创建自定义生命周期部分中了解更多关于生命周期的内容。它并没有给出一个允许的详细列表。我只知道来自Maven 2 alpha的其他引用,所以可能不是最新的。
#1
14
You can do this by defining a custom lifecycle and invoking that lifecycle before your Mojo is executed via the execute
annotation.
可以通过定义自定义生命周期并在通过execute注释执行Mojo之前调用该生命周期来实现这一点。
In your Mojo, declare in the Javadoc the lifecycle to be executed:
在Mojo中,在Javadoc中声明要执行的生命周期:
/**
* Invoke the custom lifecycle before executing this goal.
*
* @goal my-goal
* @execute lifecycle="my-custom-lifecycle" phase="process-resources"
*/
public class MyMojo extends AbstractMojo {
...
Then define a custom lifecycle in src/main/resources/META-INF/maven/lifecycle.xml.
然后在src/main/resources/META-INF/maven/lifecycle.xml中定义自定义生命周期。
The lifecycle is a bit like plexus' components.xml, but allows you to specify configuration for those goals.
生命周期有点像plexus的组件。但是允许您为这些目标指定配置。
Note the syntax is slightly different to plugin configurations in the pom. You define a goal using : as a separator rather than specifying separate groupId, artifactId and version elements, otherwise it is largely the same notation as the execution element of a plugin configuration in the pom. You can even use some properties in the lifecycle.xml (though possibly not all properties are supported, I'll need to check that).
注意,pom中的语法与插件配置稍有不同。您可以使用:作为分隔符来定义目标,而不是指定单独的groupId、artifactId和版本元素,否则它基本上与pom中插件配置的执行元素相同。您甚至可以在生命周期中使用一些属性。xml(虽然可能不支持所有属性,但我需要检查一下)。
The following example invokes the dependency plugin twice with different configurations in the process-resources phase:
下面的示例在流程资源阶段使用不同的配置两次调用依赖插件:
<lifecycles>
<lifecycle>
<id>download-dependencies</id>
<phases>
<phase>
<id>process-resources</id>
<executions>
<execution>
<goals>
<goal>
org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<includeTypes>war</includeTypes>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.outputDirectory}/wars
</outputDirectory>
</configuration>
</execution>
<execution>
<goals>
<goal>
org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies
</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<includeTypes>jar</includeTypes>
<overWrite>true</overWrite>
<outputDirectory>
${project.build.outputDirectory}/jars
</outputDirectory>
</configuration>
</execution>
</executions>
</phase>
</phases>
</lifecycle>
</lifecycles>
With this approach, the dependency plugin will be invoked once with each configuration in the process-resources
phase of the forked lifecycle (all happening within the execution defined in the Mojo).
使用这种方法,依赖插件将在分叉生命周期的流程-资源阶段(所有这些都发生在Mojo定义的执行中)对每个配置调用一次。
In the lifecycle.xml, you can define multiple phases, and multiple executions per phase of the lifecycle. The available phases are defined in the Maven lifecycle.
在生命周期的。xml,您可以定义生命周期的每个阶段的多个阶段和多个执行。可用的阶段在Maven生命周期中定义。
You can find out more about lifecycles in the Creating a Custom Lifecycle section of the Maven book. It doesn't give an exhaustive list of what is allowed though. The only other reference I know if is from the Maven 2 alpha, so is possibly not that up-to-date
您可以在Maven book的创建自定义生命周期部分中了解更多关于生命周期的内容。它并没有给出一个允许的详细列表。我只知道来自Maven 2 alpha的其他引用,所以可能不是最新的。