Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind plugin mojos (goals) to few phases of default lifecycle?

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
like image 728
MariuszS Avatar asked Apr 29 '16 18:04

MariuszS


People also ask

What are the 3 build lifecycle of Maven?

There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's web site.

How many phases does Maven lifecycle has 2 4 6 8?

These 6 phases are part of the default Maven lifecycle. The lifecycle orders the phases, from compile to deploy. When you pass a phase to the Maven command, first all previous phases in the lifecycle get executed, followed by the phase you specified.

What is Maven default lifecycle?

Maven Lifecycle: Below is a representation of the default Maven lifecycle and its 8 steps: Validate, Compile, Test, Package, Integration test, Verify, Install and Deploy.

What are the different phases of Maven?

compiler:compile – the compile goal from the compiler plugin is bound to the compile phase. compiler:testCompile is bound to the test-compile phase. surefire:test is bound to the test phase. install:install is bound to the install phase.


1 Answers

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>

like image 180
Jakub Kubrynski Avatar answered Oct 06 '22 22:10

Jakub Kubrynski