Execute multiple maven project from MS bat file?

As noted above, you need to use "call" to run mvn script as in:

call mvn package

In order to catch errors you need to use the ERROR_LEVEL variable as in:

call mvn clean
echo Exit Code = %ERRORLEVEL%
if not "%ERRORLEVEL%" == "0" exit /b

See http://jojovedder.blogspot.com/2009/03/executing-multiple-mvn-commands-from.html for further comments.


Use the call command to execute your mvn processes, like:

call mvn clean install -U

See online doc for call or

help call

for further explanations on the call command.

To avoid having all these cd commands you can also use the -f option to specify the path to your pom, e.g.

call mvn -f <path>/projectA/pom.xml clean install -U
call mvn -f <path>/projectB/pom.xml clean install -U
call mvn -f <path>/projectC/pom.xml clean install -U

Why would you not try to create an aggregation parent project?

You seems to have the following structure:

someDirectory
  +- projectA
      +- pom.xml
  +- projectB
      +- pom.xml
  +- projectC
      +- pom.xml

Simply create a pom.xml in the root directory (someDirectory in my example), and define the list of modules, which are the projectA, projectB and projectC. This pom will look like:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.company</groupId>
    <artifactId>my-aggregation-project</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>projectA</module>
        <module>projectB</module>
        <module>projectC</module>
    </modules>
</project>

notes:

  • Don't forget to set the <packaging>pom</packaging>, as it is not a "real" Java project.
  • The name of a module should match the name of the directory where the sub-module is hosted.

Now, by doing that, when you run a Maven command on the root directory, Maven will automatically run this command on all the modules. So if you just run mvn clean install on the root directory, it will run this command in your three modules.

Important note: I am talking here about the aggregation feature of Maven. Not inheritance. This means that it is not required that each module has the root project as parent.