问题
My custom maven plugin have three goals (mojos):
convert
assigned to default phase:process-test-resources
generateStubs
assigned to default phase:package
generateTests
assigned to default phase:generate-test-sources
How to bind this three mojo to default lifcycle phase, so the user can simply use plugin without special configuration and any changes to project packaging
?
User should simply add:
<plugin>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-maven-plugin</artifactId>
<extensions>true</extensions>
</plugin>
instead of
<plugin>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>convert</goal>
<goal>generateStubs</goal>
<goal>generateTests</goal>
</goals>
</execution>
</executions>
</plugin>
I can achieve this with components.xml
like below, but this requires some ugly hacks (specifing not existing phase - ugly-fix
) and I'm not sure, if this solution is working in all cases.
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.Lifecycle</role>
<implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
<role-hint>accurest</role-hint>
<configuration>
<id>accurest</id>
<phases>
<phase>ugly-fix</phase> // plugin fail without this
</phases>
<default-phases>
<process-test-resources>
io.codearte.accurest:accurest-maven-plugin:${project.version}:convert
</process-test-resources>
<generate-test-sources>
io.codearte.accurest:accurest-maven-plugin:${project.version}:generateTests
</generate-test-sources>
<package>
io.codearte.accurest:accurest-maven-plugin:${project.version}:generateStubs
</package>
</default-phases>
</configuration>
</component>
</components>
</component-set>
Is this correct? Is better way to do such configuration?
More information:
- Working components.xml.
- Sample project which is using this configuration: https://github.com/mariuszs/gs-rest-service-accurest
回答1:
You can achieve that by replacing ugly-fix
with correct goals in <phases>
tag:
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.Lifecycle</role>
<implementation>org.apache.maven.lifecycle.Lifecycle</implementation>
<role-hint>accurest</role-hint>
<configuration>
<id>accurest</id>
<phases>
<process-test-resources>
io.codearte.accurest:accurest-maven-plugin:${project.version}:convert
</process-test-resources>
<generate-test-sources>
io.codearte.accurest:accurest-maven-plugin:${project.version}:generateTests
</generate-test-sources>
<package>
io.codearte.accurest:accurest-maven-plugin:${project.version}:generateStubs
</package>
</phases>
<default-phases>
<process-test-resources>
io.codearte.accurest:accurest-maven-plugin:${project.version}:convert
</process-test-resources>
<generate-test-sources>
io.codearte.accurest:accurest-maven-plugin:${project.version}:generateTests
</generate-test-sources>
<package>
io.codearte.accurest:accurest-maven-plugin:${project.version}:generateStubs
</package>
</default-phases>
</configuration>
</component>
</components>
回答2:
I think what you are looking for is the defaultPhase
attribute of the Mojo
annotation, see https://maven.apache.org/components/plugin-tools/maven-plugin-tools-annotations/ for all the details.
来源:https://stackoverflow.com/questions/36945610/how-to-bind-plugin-mojos-goals-to-few-phases-of-default-lifecycle