Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How call a maven plugin explicitly and non running otherwise?

Eventually I generate some Java code with explicite mvn generate-sources which calls CXF plugin defined in pom.xml.

Q: How to configure plugin to make it running when I call it explicitly and not running otherwise?

If I set phase to generate-sources, then mvn generate-sources works, but mvn compile also triggers it (that's unwanted).

If I set phase to none, then mvn generate-sources does nothing (unwanted), but mvn compile does not trigger it (ok).

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <!--<phase>none</phase>-->
like image 636
Pavel Vlasov Avatar asked Feb 09 '16 12:02

Pavel Vlasov


1 Answers

You can run any plugin from command line and as such exclusively it (and not part of a Maven phase, in this case), simply configuring the plugin in your POM (plugins section) with a generic configuration (not nested within any execution sub-section).

As such, the given configuration will be used for any execution. Since the concerned plugin does not have any default binding through any Maven packaging (which instead happens for other plugins, i.e. Maven Compiler or Surefire plugins), then the configuration will be picked up only by your command line execution.

mvn org.apache.cxf:cxf-codegen-plugin:wsdl2java 

Would then run the plugin you configured with the required configuration, given in your POM:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf.version}</version>
    <configuration><!-- HERE YOUR CONFIG --></configuration>
</plugin>

As you can see, there is no execution (and hence no phase) described for the plugin, but a global configuration is supplied. As global, it will be used by your command line execution as well.

This approach is slightly different than what you tried:

mvn generate-sources

When executing generate-sources, you are executing a phase and as such any other preceding phase. However, the preceding phases do not affect your project and as such you would get the same behavior, plus the side effect of also having it as part of the compile phase.


If you really want to have it as part of the generate-sources phase (for whatever reason) and keep on running mvn generate-sources without the side effect described above, then you can wrap it in a profile, adding to your POM:

<profiles>
   <profile>
      <id>generate-cxf</id>
      <build>
         <defaultGoal>generate-sources</defaultGoal>
         <plugins>
           <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>
                <executions><!-- HERE YOUR EXECUTIONS--></executions>
            </plugin>
         </plugins>
      </build>
     </profile>
</profiles>

You could then execute:

mvn generate-sources -Pgenerate-cxf

And it will activate the profile above and as such your plugin and executions but not impacting (side effects) any other phase or the default build.

Moreover, via the defaultGoal element configuration, you can even simply invoke:

mvn -Pgenerate-cxf

And you will have exactly the same behavior (although a bit cryptic I would say).


Last but not least, since Maven 3.3.1, you can invoke just a simple plugin execution (keeping your binding to the none phase this time) from command line (specifying the desired execution id) as following:

    mvn org.apache.cxf:cxf-codegen-plugin:VERSION_HERE:wsdl2java:@EXEC_ID_HERE

The command above will only (and only) execute your target execution.

like image 54
A_Di-Matteo Avatar answered Oct 13 '22 00:10

A_Di-Matteo