26

I need to build 3 independent maven projects using a build.bat file (because of tycho aggregation is not an option - see comments on romaintaz answer). I have tried (executed from the build folder - see below):

cd ../projectA
mvn clean install -U
cd ..
cd ../projectB
mvn clean install -U
cd ..
cd ../projectC
mvn clean install -U

where the folder structure of the projects are:

build
  |--> build.bat

projectA
  |--> pom.xml

projectB
  |--> pom.xml

projectC
  |--> pom.xml

but only projectA is build projectB and projectC are skipped. Any ideas on how to modify the above batfile so the following project is build if the previous was build successfully?

Jan
  • 930
  • 9
  • 25
u123
  • 15,603
  • 58
  • 186
  • 303
  • are you checking for errors returned from maven in the .bat file? Why do you cd up each time? Are these projects nested inside each other? – mkro May 25 '11 at 20:19
  • I have updated the example. Checking for errors is secondary - first I need to figure out how to execute a sequence of projects. – u123 May 25 '11 at 20:46
  • I think maven does `exit`. That's why the batch file finishes after the first `mvn` command and doesn't continue to the next. – AlikElzin-kilaka Jan 06 '15 at 13:07

7 Answers7

50

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
Roland
  • 7,525
  • 13
  • 61
  • 124
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • 1
    Thanks that what I am looking for. Now I just need to figure out how to break when one of the projects fails. Is it possible to pipe some output from the maven job to the bat script? – u123 May 30 '11 at 09:23
  • @tul Maybe you can go with a solution described [here](http://stackoverflow.com/questions/2323292/windows-batch-assign-output-of-a-program-to-a-variable/2340018#2340018) – FrVaBe May 30 '11 at 10:36
  • Does anyone know the exact reasoning this is required? I know 'call' is used for calling other batch processes which implies to me maven is executing it's commands in a batch as well, is that right? – Leigh McCulloch Sep 19 '13 at 03:09
  • 1
    @leighmcc Indeed - executing `mvn` means calling the bacht file `apache-maven-x.x.x\bin\mvn.bat` – FrVaBe Sep 19 '13 at 06:39
  • What's the use of `-U`? – user2918640 May 23 '16 at 06:11
  • @user2918640 -U _"Forces a check for missing releases and updated snapshots on remote repositories"_ (see `mvn -help`) – FrVaBe May 24 '16 at 04:16
8

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.

Martin Modrák
  • 746
  • 8
  • 17
3

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.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • Have tried that. The problem is that a 'target' must be specified in the parent which is resolved for all submodules. Since one of the submodules updates this target it not an option to aggregate. – u123 May 26 '11 at 07:47
  • I don't see why it would make sense NOT to run 'mvn clean install' from the root - unless of course you just want to build one of the submodules only. But this is not case. I need to build all projects and all submodules for each project. – u123 May 26 '11 at 07:59
  • @tul I don't understand your problem with the `target` directory. And again, I did not talk about the **parent** feature, but only the **aggregation**. So basically, all the modules are still independent, but the aggregated project will help you to run a command among all your sub-modules. Having an aggregated project does not force you to run all Maven command in this root directory. If you just need to build `projectA`, you can run the Maven command only in this module. – Romain Linsolas May 26 '11 at 08:23
  • Its not the target folder but a target platform: http://tycho.sonatype.org/targetplatformmanagement.html – u123 May 26 '11 at 08:32
1

Use 'call' when you want to invoke another batch file in the parent file ,so that control will be returned to the parent batch file and it will continue execution.

e.g call mvn clean install

saurav
  • 3,424
  • 1
  • 22
  • 33
1

I think that using %ERRORLEVEL% will complete +FrVaBe's answer. Originally from Kunal

 call mvn -f ProjectA\pom.xml clean install  if not "%ERRORLEVEL%" == "0" goto error

 call mvn -DskipTests=true -f ProjectC\pom.xml clean install if not "%ERRORLEVEL%" == "0" goto error

 exit

 :error @echo Build Failed pause
Olga
  • 111
  • 7
0

FrVaBe had answered very good. If you would like to keep your code structure, because of some other activities like copying artifacts the the solution would be:

cd ../projectA
call mvn clean install -U
echo Do some stuff with artifacts
cd ..
cd ../projectB
call mvn clean install -U
echo Do some stuff with artifacts
cd ..
cd ../projectC
call mvn clean install -U
echo Do some stuff with artifacts

Here the most important is call function which is used to execute maven build as a kind of subroutine, which ends back into your bat file andd do not exits completely.

0

Since your projects are on the same directory level, you shouldn't navigate up twice. You're going up via cd .. and then again you're going up one level to enter the next project by doing cd ../projectX.

Either remove the cd ..'s in between or change the cd into your project folder into cd ./projectX (current directory instead of parent directory)

By the way, do you really need to use a bat-file for this? Have you considered a multimodule pom?

wjans
  • 10,009
  • 5
  • 32
  • 43
  • So far I have not be able to do this with a common parent - see above comment. All the projects are submodule projects, so basically I just need to call the parents in the right order - and break if one of the projects fail. – u123 May 26 '11 at 07:50