Run a goal of a plugin inside a profile

Alkis Kalogeris picture Alkis Kalogeris · Jan 24, 2016 · Viewed 7.5k times · Source
    <profile>
        <id>integration-tests</id>
        <activation>
            <property>
                <name>integrations</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <executions>
                        <execution>
                            <id>script-chmod</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>chmod</executable>
                                <arguments>
                                    <argument>+x</argument>
                                    <argument>integration-test-docker/integrations.sh</argument>
                                </arguments>
                            </configuration>
                        </execution>
                        <execution>
                            <id>script-exec</id>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>integration-test-docker/integrations.sh</executable>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

This profile has more things in it, but I included only what I want to run. How can I execute this particular goal, of this particular plugin?

I tried mvn exec:exec but I get this

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (default-cli) on project ando: The parameters 'executable' for goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec are missing or invalid -> [Help 1]

Moreover, the error output indicates the version 1.3.2, but I'm using 1.4.0.

Answer

A_Di-Matteo picture A_Di-Matteo · Jan 24, 2016

You get an error on invoking the exec goal of the the exec-maven-plugin because its configuration (the one you meant) is part of a profile which is not active by default and it is not activated by your execution mvn exec:exec.

If you try to activate the profile via its property activation (as per configuration) or explicitly (via the -P option), then Maven will know which Exec Maven Plugin you mentioned:

mvn exec:exec -Pintegration-tests

or

mvn exec:exec -Dintegrations=true

As such, the profile will be active and your Exec Maven Plugin configuration will be part of the build.

However, you are configuring two executions of the plugin and there is no global configuration, hence it would also fail again.

There is a difference between the configuration element for a plugin section in or out of the execution sub-section: out, it will be applied by default to all executions and to command line executions; in, it will be applied to the specific execution to which is applied (overriding or getting merged with any default one, if provided).

If you want to run both executions, you can then just activate the profile:

mvn clean verify -Pintegration-tests

or

mvn clean verify -Dintegrations=true

However, it will execute the whole content of the profile plus the whole build till the specified phase.

If you want to execute only the plugin goal (exec) from the command line, then you need to specify a default configuration (but only one) as following:

<profile>
    <id>integration-tests</id>
    <activation>
        <property>
            <name>integrations</name>
            <value>true</value>
        </property>
    </activation>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!-- here your default configuration -->
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

The default configuration will be applied to the command line execution.

Alternatively, you could also override the default execution id (default-cli) which is used by every plugin execution from the command line and which you can see as part of the mentioned error in the question. As such, you could provide a specific configuration just for that execution and not for all other (if any) executions (given that they would not provide their own configuration). Here is an example:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <id>default-cli</id>
            <configuration>
                <!-- here your specific command line execution -->
            </configuration>
        </execution>
    </executions>
</plugin>

If you want to execute both executions from command line and only these executions (hence, not as part of a Maven phase), you can check this answer on the topic (in short: it is possible since Maven 3.3.1, otherwise a suggestion is provided for earlier versions).

As such, an execution would be as following:

mvn org.codehaus.mojo:exec-maven-plugin:1.4.0:exec@script-chmod -Dintegrations=true

Note:

  • I'm again activating the profile to have the execution as part of the build
  • This time I'm not passing the plugin alias, exec, and the desired goal, exec, but its full Maven GAV (groupId, artifactId, version) in the common Maven pattern for dependencies (: separated), so we get org.codehaus.mojo:exec-maven-plugin:1.4.0, then passing the goal, so we get the additional :exec, then using the new feature mentioned above and the new @ operator I pass the desired execution id (defined in the POM).