8

I would like to execute a plugin goal from command line but perform multiple executions of the plugin. To this end my POM looks like this:

<plugin>
    <groupId>xxx.yyy</groupId>
    <artifactId>zzz</artifactId>
    <version>1.1.6</version>
    <executions>
        <execution>
            <id>default-cli-1</id>
            <goals>
                <goal>mygoal</goal>
            </goals>
            <configuration>
                .... config1 ....
            </configuration>
        </execution>
        <execution>
            <id>default-cli-2</id>
            <goals>
                <goal>mygoal</goal>
            </goals>
            <configuration>
                .... config2 ....
            </configuration>
        </execution>
    </executions>
</plugin>

What I would like to do is something like:

mvn xxx.yyy.zzz:mygoal

and that would then execute the two executions. But I cannot figure out how.

I'm aware that I cannot use an <id> when executing from the command line. That is what the default-cli is for. However the <id> must be unique within <executions> which means I can only put the default-cli on one execution.

Maven version 3.0.5.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
peterh
  • 18,404
  • 12
  • 87
  • 115

1 Answers1

7

You can execute a goal (and its execution) from command line starting from Maven 3.3.1 on and this new feature, via the @executionId additional option.

Concerning Maven and execution ids generation, you can also check this SO question.


Before Maven 3.3.1 you could instead bind the two executions to a phase which would normally not harm (like validate) and have something like the following:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <id>execution-1</id>
            <phase>validate</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <classifier>something1</classifier>
            </configuration>
        </execution>
        <execution>
            <id>execution-2</id>
            <phase>validate</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <classifier>something2</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

Then executing:

mvn validate

You will effectively execute the two executions of the same goal of the same plugin, as part of an harmless phase.

If you don't want to have them as part of this phase by default (understandable), then you can move them to a profile and activate it as part of the execution:

mvn validate -PpluginGoalExecution

For completeness, the profile would look like:

<profile>
    <id>pluginExecution</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>execution1</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>something1</classifier>
                        </configuration>
                    </execution>
                    <execution>
                        <id>execution2</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>something2</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

And it goes without saying: the id of the profile should in this case be quite self explanatory about which plugin and which goal it would actually execute (that is, the purpose of the profile, as usual).

Update
Just cosmetic, but you could also add to the profiled build above the element:

<defaultGoal>validate</defaultGoal>

So that you would only need to run the following Maven command (only profile activation):

mvn -PpluginGoalExecution

And it would then automatically execute the validate phase and the configured plugin executions. Not a big change (as I said, cosmetic), but maybe closer to a plugin goal execution rather than a Maven phase invocation (again, just appearance).

Community
  • 1
  • 1
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • Thanks. That's a good answer. Unfortunately I'm still on Maven 3.0.5. – peterh Jan 20 '16 at 15:12
  • If you are on Maven 3.0.5 it's not possible...Only possible via profiles which can be activated via `mvn -PXXX1 ...` ... – khmarbaise Jan 20 '16 at 15:16
  • @peterh ok I will update my answer accordingly, there is trick, it may be worth for you – A_Di-Matteo Jan 20 '16 at 15:17
  • @peterh I updated the answer and tested the proposal, works perfectly if you accept the compromise of a profile and the execution of an harmless phase – A_Di-Matteo Jan 20 '16 at 15:25
  • @A.DiMatteo. Thanks. I cannot use the "harmless phase" idea as it would mean that these steps would also be executed when I do say `mvn package`. Not good. The only thing that would be "harmless" is if there was such a thing as a phase after `deploy`, but there isn't. The profile idea I believe will work. I'll accept the answer - not because I like it (not your fault, Maven's fault) but because you answer was swift and informed. – peterh Jan 20 '16 at 15:31
  • @peterh I perfectly understand your concerns, and yes, it should definitely not be part of the default execution, hence a profile would make sense (I tested locally before proposing it, just to make sure it worked fine), but indeed it's a Maven limit. Alternatively you could also consider a migration to Maven 3.3.1, but in some companies/contexts it might not be as easy as a number switch. – A_Di-Matteo Jan 20 '16 at 15:47