javamavenexec-maven-plugin

Overriding exec-maven-plugin with exec.mainClass


I have started a new maven project and configured exec-maven-plugin to set a default mainClass, and I can execute using

mvn exec:java -q
Hello World!

However if I try to specify a different mainClass on the command line, this seems to get ignored:

mvn exec:java -Dexec.mainClass="com.jamesmcguigan.kdt.App2" -q
Hello World!

The above line works if I comment out the exec-maven-plugin section from the pom.xml, but it would be nice to set the default in the pom.xml for maven exec:java and have a way of overriding this on the cli for mvn exec:java -Dexec.mainClass="com.jamesmcguigan.kdt.App2".

Or am I missing something else here?

Files

pom.xml

  <build>
      <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.jamesmcguigan.kdt.App</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

src/main/java/com/jamesmcguigan/kdt/App.java

package com.jamesmcguigan.kdt;
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

src/main/java/com/jamesmcguigan/kdt/App2.java

package com.jamesmcguigan.kdt;
public class App2 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World 2!" );
    }
}

Solution

  • Use a property, i.e.:

    <properties>
            ...
            <main.class>com.jamesmcguigan.kdt.App</main.class>
    </properties>
    ...
    <configuration>
             <mainClass>${main.class}</mainClass>
    </configuration>
    

    And then: mvn exec:java -Dmain.class="com.jamesmcguigan.kdt.App2" -q