11

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:

MariuszS
  • 30,646
  • 12
  • 114
  • 155

2 Answers2

2

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>

Jakub Kubrynski
  • 13,724
  • 6
  • 60
  • 85
-1

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.

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • 1
    No, that's not it, OP wants the plugin to be executed without declaring any `` in the POM. A `defaultPhase` requires an execution. – Tunaki May 02 '16 at 19:33
  • Ah, got it. That would indeed mean that you have to introduce a packaging type with a lifecycle. – Robert Scholte May 02 '16 at 19:39
  • 1
    Yeah and that's the second thing.. OP wants it to works automagically with whatever packaging in the default lifecycle (not their own). :) (that's where I'm stuck, without having to copy-paste the whole `default-bindings.xml` from maven core) – Tunaki May 02 '16 at 19:40
  • 1
    Not possible. And I wouldn't advice to adjust the lifecycle, just leave that up to the packaging plugin. Simply picture this: what would happen if every build-plugin would rewrite the lifecycle? I'm pretty sure most Maven users know how to bind plugin goals to phases, especially with a prepared copy-paste example :) – Robert Scholte May 02 '16 at 19:47
  • That's what I figured yeah, but thanks for your confirmation :). – Tunaki May 02 '16 at 19:50
  • @RobertScholte Not possible, but works in my example - maven bug? :) I dont want to rewrite the lifecycle. I simply want to inject additional goals into existing one - and I think this will be really useful thing... – MariuszS May 02 '16 at 21:44
  • 1
    @MᴀʀɪᴜsᴢS In your example, you have a custom packaging with a lifecycle that involves your 3 phases (and so it works the intended way). This is different that invoking your plugin goals for all packaging of the default lifecycle. – Tunaki May 03 '16 at 07:19
  • @Tunaki Im pretty sure it works this way - invokes my plugin goal for all packaging – MariuszS May 03 '16 at 14:53
  • 1
    @MᴀʀɪᴜsᴢS It doesn't, if you test with a `jar` packaging for example and you have `jar` in your `components.xml`, you'll see they are the only one invoked and the other default phases for the normal `jar` are not invoked. – Tunaki May 03 '16 at 14:56
  • same answered here http://stackoverflow.com/questions/5213526/how-to-attach-a-maven-plugin-to-a-phase-by-default – Hisham Khalil May 03 '16 at 15:42
  • @Tunaki: Why would you want to change my `components.xml`. It works with different packaging because it has wrong `role-hint`. – MariuszS May 03 '16 at 17:20
  • @Hishamkh This is not the same, I have already binded all goals to default phases. – MariuszS May 03 '16 at 17:21
  • @Tunaki change packaging to war in my sample project, it works! https://github.com/mariuszs/gs-rest-service-accurest. So, to summarize all of this: this is bug in maven and there is no solution. yes? – MariuszS May 03 '16 at 17:25