Update multiple maven projects with multiple dependencies by using a simple batch file

Imagine writing code for multiple projects with multiple dependencies and continuously changing SNAPSHOT versions.

I am currently writing code for six different maven projects. Each maven project consists of serval different maven modules. The maven projects have some dependencies among each other and dependencies for projects that other teams are responsible for. Let’s just say the version of the different projects may differ every 2 to 4 weeks.

The challenge

So after every 2 weeks I execute an “update to HEAD” (unfortunately we use SVN and not git) on each project, force a maven update and perform a clean install on all these projects. But I have to consider the right order when updating the projects. The figure below illustrates the dependencies amongst the different projects and thus the order how to update.

Project_Dependencies

Well it is a nuisance to update the dependencies and version for each project manually. So why not using a batch file that does all that automatically?

The solution

The batch file consists of these simple steps, which shall be done for each(!) project:

  • Go to the directory where the project is
  • Perform an svn update
  • Perform an maven update
  • Perform a maven clean install

The projects are in the same directory but they are not in alphabetical order, nor in any other order. Using a loop to go through all files within a directory wouldn’t do the trick . Therefore I wrote the path names in a .txt file. The loop just has to go through the file and pick the path names. The path name identifies the directory where the batch file has to switch. The current directory can be changed to the specified path name simply by using pushd “pathname” [2].

After having changed the directory a svn update is performed in order to get the latest changes. In order to force Maven to check for updated releases and snapshots in a remote repository use –U or –update-snapshots [3]. Following the update a clean install can be performed. And that is pretty much it .  Note that any merge conflicts when updating the projects won’t be solved and a maven clean install would result in a “BUILD FAILURE”. It depends on the circumstances, to decide if this script is appropriate for you or not. In my case, I run this bat file, after a 2-week sprint with a clean workspace. I posted the script at the end of the article. Feel free to use it.

Tips for more readability in a batch file (no need to point out why that is important) =)

A clean coder advice says [1]: “Every time you write a comment, you should grimace and feel the failure of your ability of expression.” So do comment only when it is necessary. Start the script with a comment that sums up, what it does. This will do it. And if not keep in mind, that using variables will make the script more readable. But only if you name a variable expressive enough.

Consider quotes. For example if the set directory contains a blank space (DIR c:\Programs\MY NOT FOUND FOLDER), it can be seen as a delimiter for parameters. And therefore the files will not be found. With quotes the expression seems like a unit.

@echo off
set directory=C:\Users\myUser\Desktop\projectPaths.txt
for /f "tokens=*" %%a in (%directory%) do (
echo Project to update in directory %%a
pause
pushd "%%a"
call :updateProject
)
pause
goto :eof

:updateProject
call svn st -u
echo svn status --show-updates or shorter svn status -u
pause
call svn update
call mvn -U clean install
goto :eof

 

References

[1] Robert C. Martin, The Robert C. Martin Clean Code Collection (Collection)

[2] https://technet.microsoft.com/en-us/library/bb490978.aspx

[3] http://books.sonatype.com/mvnref-book/reference/running-sect-options.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s