Execute multiple maven project from MS bat file?
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 clea开发者_开发百科n 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?
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
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.
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.
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
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
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?
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.
精彩评论