3

I have a Maven project and it has 2 mains (MyTestApp_A, and MyTestApp_B) inside one of the packages from the src folder.

I can run these "main" classes in Eclipse if I open them and click the run button. However, Eclipse is bugged, and so I actually need to run these two classes on a command line using Maven.

I have never used Maven before, but after asking for help and doing some research I understand that I have to change the pom.xml file.

Consequently, I have altered my pom.xml file successfully to run one of the apps using the command mvn exec:java -Dexec.mainClass="servers.MyTestApp_B":

    <plugins>
        <!-- Allows the example to be run via 'mvn compile exec:java' -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>

            <configuration>
                <mainClass>servers.MyTestApp_A</mainClass>
                <includePluginDependencies>false</includePluginDependencies>
            </configuration>


        </plugin>

    </plugins>

Happy with being able to run MyTestApp_A, I tried to add another configuration part to run MyTestApp_B:

    <plugins>
        <!-- Allows the example to be run via 'mvn compile exec:java' -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>

            <configuration>
                <mainClass>servers.MyTestApp_A</mainClass>
                <includePluginDependencies>false</includePluginDependencies>
            </configuration>
            <configuration>
                <mainClass>servers.MyTestApp_B</mainClass>
                <includePluginDependencies>false</includePluginDependencies>
            </configuration>

        </plugin>

    </plugins>

However, This file is not well formed. Apparently I am not allowed to have 2 <configuration> tags in the same pom.xml file.

So, how can I execute MyTestApp_A, and MyTestApp_B using Maven? How do I configure the pom.xml file?

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • You need to configure two or more distinct `` sections for your plugin (which may have different configurations). See http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag for guideline. – Gyro Gearless Nov 09 '13 at 20:39
  • Actually, I don't think it's a good idea since maven is a build tool, and not a java-runner tool. What's your reason to launch these files via maven? You can easily create a wrapper script which will do this job. – Andrew Logvinov Nov 09 '13 at 20:48

1 Answers1

6

Try using executions for each main class you want to execute:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <id>MyTestApp_A</id>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>servers.MyTestApp_A</mainClass>
                    <includePluginDependencies>false</includePluginDependencies>
                </configuration>
            </execution>
            <execution>
                <id>MyTestApp_B</id>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>servers.MyTestApp_B</mainClass>
                    <includePluginDependencies>false</includePluginDependencies>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
  • To exec a specific execution by ID you need: mvn exec:java@MyTestApp_A (as of Maven 3.3.1). – jarmod Oct 18 '17 at 21:13