72

I execute the following code using mvn exec:java com.mycompany.FooServer.

I would like to add another server which I can execute like mvn exec:java com.mycompany.BarServer.

How do I do that within a single pom file?

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.mycompany.FooServer</mainClass>
            </configuration>
        </plugin>
 </build>  
hopper
  • 13,060
  • 7
  • 49
  • 53
diya
  • 6,938
  • 9
  • 39
  • 55

6 Answers6

119

Try this. You can have more than one execution under executions. All you need to do is move the configuration element under the execution. The plugin has configuration, but each execution can also have a separate configuration element.

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>first-execution</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mycompany.FooServer</mainClass>
                    </configuration>
                </execution>
                <execution>
                    <id>second-execution</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mycompany.BarServer</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
     </plugins>
 </build>

With Maven 3.3.1 and up, you can run an execution by its ID using

mvn exec:java@id

In this case the commands would be mvn exec:java@first-execution and mvn exec:java@second-execution. See this answer for more details.

ad3luc
  • 192
  • 1
  • 7
  • 22
Tim O'Brien
  • 9,623
  • 5
  • 30
  • 36
  • 7
    For those of you wondering how you call an execution by id, [my search](http://stackoverflow.com/questions/2192660/maven-maven-exec-plugin-multiple-execution-configurations) didn't turn up a way to do that. – Daniel Kaplan Aug 06 '13 at 23:26
  • 1
    Indeed. I'm not sure why this solution has been accepted when in fact there seems to be no way to make use of the second-execution, which renders it useless... – Petr Baudis Apr 24 '14 at 13:19
  • 1
    I also tried to do something similar to what has been explained above and it doesn't seem to work. Only the first `execution` gets triggered the latter is ignored. – victor Apr 26 '14 at 13:34
  • 1
    From maven documentation: [_Note: **Configurations inside the tag** differ from those that are outside ..._](http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag) – José Andias Sep 11 '14 at 11:57
  • 6
    @DanielKaplan Yes with maven > 3.3.1 you call an execution by id with `mvn exec:java@execId` – George Nov 06 '15 at 20:38
  • is there any way to call second main class execution ? – Muhammad Adnan Mar 11 '16 at 10:46
  • is it possible to trigger to all executions with a single command without explicitly listing the ids? Something like `mvn exec:exec@*` – Haneesh reddy Apr 18 '22 at 22:10
10

@tieTYT: You can select the execution by id using two distinct profiles:

mvn test -Pmanager

mvn test -Pproxy

<profiles> 
<profile>
    <id>proxy</id>
    <build>
    <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <phase>test</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>pt.inesc.proxy.Proxy</mainClass>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
    </build>
</profile> 
<profile>
    <id>manager</id>
    <build>
    <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <phase>test</phase>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>pt.inesc.manager.Manager</mainClass>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
    </build>
</profile> 
</profiles>
dario nascimento
  • 587
  • 5
  • 11
9

With maven > 3.3.1 it is possible to specify the execution id as:

mvn exec:java@execId
George
  • 7,206
  • 8
  • 33
  • 42
3

For me including configuration in the execution block didn't work and maven complained about main class not being set. But inspired by Dario's answer I'd answer this question as follows:

<profiles>
    <profile>
        <id>foo</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <mainClass>com.mycompany.FooServer</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>bar</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <mainClass>com.mycompany.BarServer</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Which then allows you to run one or the other server using:

mvn exec:java -Pfoo

or

mvn exec:java -Pbar

Cheers,

Jacek
  • 1,048
  • 15
  • 21
3

I find the solution: I put <configuration> in <execution>

you can use mvn clean test -Pfoo,bar

<profiles>
    <profile>
        <id>foo</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <executions>
                        <execution>
                            <id>CountContinusIntegr-execution</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.mycompany.FooServer</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>bar</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <executions>
                        <execution>
                            <id>CountContinusIntegr-execution</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.mycompany.BarServer</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
0

I'm afraid that what you want is not possible. I could not find a way to call the same exec-maven-plugin goal directly (mvn exec:java) with different configurations in .pom file.

Said that, you can however have multiple executions of exec-maven-plugin. The thing is you can not call the goals directly. You have to use multiple executions and bind them to particular build phases.

You could also make use of the following solution that fitted me. You can still call one goal directly with it's configuration in the .pom:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
        <execution>
            <id>Acceptance Tests</id>
            <phase>integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>pybot</executable>
                <arguments>
                    <!--...-->
                </arguments>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <mainClass>pt.jandias.someapp.persistence.SchemaGenerator</mainClass>
        <arguments>
            <!--...-->
        </arguments>
    </configuration>
</plugin>

One could than use mvn exec:java and mvn integration-test at will.

José Andias
  • 1,894
  • 2
  • 29
  • 31