Execute multiple goals with parameters in maven

mo0206 picture mo0206 · Jan 8, 2016 · Viewed 9.5k times · Source

I am trying to execute multiple goals in maven

I have my Pom.xml like

<plugins>
    <plugin>
        <groupId>com.lazerycode.jmeter</groupId>
        <artifactId>jmeter-maven-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
            <execution>
                <id>jmeter-tests</id>
                <phase>verify</phase>
                <goals>
                    <goal>jmeter</goal>
                </goals>
                <configuration>
                    <testFilesIncluded>
                        <jMeterTestFile>${mytest}</jMeterTestFile>                       
                    </testFilesIncluded>
                    <propertiesUser>                
                        <hostName>${myhost}</hostName> 
                        <port>${myport}</port> 
                        <protocol>${myprotocol}</protocol>              
                    </propertiesUser>
                </configuration>                        
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>de.codecentric</groupId>
        <artifactId>jmeter-graph-maven-plugin</artifactId>
        <version>0.1.0</version>
        <executions>
            <execution>
                <id>create-graphs</id>
                <goals>
                    <goal>create-graph</goal>
                </goals>
                <phase>verify</phase>         
            </execution>
        </executions>       
    </plugin>

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.4.0</version>
        <executions>
            <execution>
                <id>runcommand</id>
                <goals>
                    <goal>exec</goal>
                </goals>
            </execution>
        </executions>   
        <configuration>
            <executable>mvn</executable>
            <arguments>
                <argument>**com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter@jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx**</argument>
                <argument>de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph@create-graphs</argument>        
            </arguments>
        </configuration>
    </plugin>     
</plugins>

I have two arguments mentioned in the org.codehaus plugin.

running the below command with argument1 disabled works fine.

mvn org.codehaus.mojo:exec-maven-plugin:1.4.0:exec@runcommand 

But when I run the command with both are arguments enabled gives me an error

failed to execute goal com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter@jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx

running goal1 and goal2 indivudally with parameters from cmd line works fine.

mvn de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph@create-graphs 
mvn com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter@jmeter-tests "-Dmyhost=hix.qa.com" "-Dmyport=80" "-Dmyprotocol=http" "-Dmythreads=5" "-Dmyloopcount=20" "-Dmyrampup=1" "-Dmytest=ScreenerAPI.jmx"

How to pass parameters to one goal from command line while running multiple goals?

Answer

Tunaki picture Tunaki · Jan 8, 2016

You want to execute multiple commands, so you want to have multiple executions of the plugin:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <!-- First execution executing jmeter-maven-plugin -->
        <execution>
            <id>runcommand1</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <phase> <!-- you need to write a phase here --> </phase>
            <configuration>
                <arguments>
                    <argument>com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter@jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx</argument>
                </arguments>
            </configuration>
        </execution>
        <!-- Second execution executing jmeter-graph-maven-plugin -->
        <execution>
            <id>runcommand2</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <phase> <!-- you need to write a phase here --> </phase>
            <configuration>
                <arguments>
                    <argument>de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph@create-graphs</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <executable>mvn</executable>
    </configuration>
</plugin>

This configuration does multiple things:

  • It configures 2 executions for the 2 commands you want to invoke. They are both configured with the correct <argument> attribute.
  • It factors out the common configuration of those two inside the global configuration element. In this case, this is simply the executable.

There's an important factor though: those executions need to be bound to a phase if you want to execute both of them in a single build. As as example, if you bind them to verify then invoking mvn verify will invoke the two executions.