Maven exec:java goal on a multi-module project

Goals in a multi-module project, when run from the parent, will run against all modules. I don't think that's what you want. You can try:

mvn exec:java -pl module2 -Dexec.mainClass=MyMain

That might work? More info:

  • Running a specific Maven plugin goal from the command line in a sub-module of a multi-module reactor project

However, I think it's more intuitive to change directory to the sub-module containing the executable before running it.


  • You should bind the exec-maven-plugin to a maven lifecycle goal, say verify.
  • Since you want the plugin to be executed only for module2, define the plugin configurations in the parent pom within pluginManagement. Use the same only in module 2.
  • Then run the following:

mvn verify -Dexec.mainClass=MyMain.

parent pom

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                 <execution>
                     <phase>verify</phase>
                     <goals>
                         <goal>java</goal>
                     </goals>
                 </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

module 2

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

See this answer for a single-command alternative without mvn install:

https://stackoverflow.com/a/26448447/1235281

By using skip in the parent pom.xml, you can tell Maven to only run exec:java on a specific submodule.

GitHub: https://github.com/Oduig/mavenfiddle