Maven: execution from command line and multiple ex

2019-04-28 06:46发布

问题:

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.

回答1:

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).