Maven advanced: defining profiles (with module order) to make the build faster

Submitted by Jochus on Tue, 09/02/2010 - 18:10 | Posted in: Java



Consider following setup:

Maven root POM

A simple project has 5 modules

<?xml version="1.0" encoding="UTF-8"?>
 
<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>be.jochusonline</groupId>
	<artifactId>pom-profiles-example</artifactId>
	<packaging>pom</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>PARENT</name>
 
	<modules>
		<module>module1</module>
		<module>module2</module>
		<module>module3</module>
		<module>module4</module>
		<module>module5</module>
	</modules>
</project>

Maven module POM

The maven modules are looking like this (just by example). The other modules have a similar POM structure

<?xml version="1.0" encoding="UTF-8"?>
 
<project>
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>be.jochusonline</groupId>
		<artifactId>pom-profiles-example</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>pom-profiles-example-module1</artifactId>
	<packaging>jar</packaging>
	<name>MODULE1</name>
</project>

Building

Building this project costs:

<a href="mailto:jochen@baileys">jochen@baileys</a> ~/Desktop/modules $ mvn clean install
[INFO] Scanning for projects...
[INFO] Reactor build order: 
[INFO]   PARENT
[INFO]   MODULE1
[INFO]   MODULE2
[INFO]   MODULE3
[INFO]   MODULE4
[INFO]   MODULE5
[INFO] ------------------------------------------------------------------------
8< ... >8
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] PARENT ................................................ SUCCESS [3.388s]
[INFO] MODULE1 ............................................... SUCCESS [1.129s]
[INFO] MODULE2 ............................................... SUCCESS [1.073s]
[INFO] MODULE3 ............................................... SUCCESS [0.033s]
[INFO] MODULE4 ............................................... SUCCESS [0.040s]
[INFO] MODULE5 ............................................... SUCCESS [0.042s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Tue Feb 09 18:01:54 CET 2010
[INFO] Final Memory: 16M/79M
[INFO] ------------------------------------------------------------------------

... 6 seconds

Change Maven root POM to build only single module

Sometimes, in real software development processing, it's not really needed to build the whole project. Imagine the project exists of a "processing", a "web" and a "backoffice" part. You are working on the backoffice part, so why would you rebuild the web and processing part as you didn't change them?!?!
So, the improve the build process, I'm introducing profiles:

<?xml version="1.0" encoding="UTF-8"?>
 
<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>be.jochusonline</groupId>
	<artifactId>pom-profiles-example</artifactId>
	<packaging>pom</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>PARENT</name>
 
	<profiles>
		<profile>
			<id>all</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<modules>
				<module>module1</module>
				<module>module2</module>
				<module>module3</module>
				<module>module4</module>
				<module>module5</module>
			</modules>
		</profile>
		<profile>
			<id>module1</id>
			<modules>
				<module>module1</module>
			</modules>
		</profile>
	</profiles>
</project>

I'm not changing anything to the modules. And I'm still able to build the project:

<a href="mailto:jochen@baileys">jochen@baileys</a> ~/Desktop/modules $ mvn clean install
[INFO] Scanning for projects...
[INFO] Reactor build order: 
[INFO]   PARENT
[INFO]   MODULE1
[INFO]   MODULE2
[INFO]   MODULE3
[INFO]   MODULE4
[INFO]   MODULE5
[INFO] ------------------------------------------------------------------------
8< ... >8
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] PARENT ................................................ SUCCESS [3.387s]
[INFO] MODULE1 ............................................... SUCCESS [1.129s]
[INFO] MODULE2 ............................................... SUCCESS [1.071s]
[INFO] MODULE3 ............................................... SUCCESS [1.035s]
[INFO] MODULE4 ............................................... SUCCESS [0.043s]
[INFO] MODULE5 ............................................... SUCCESS [0.039s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Tue Feb 09 18:06:37 CET 2010
[INFO] Final Memory: 16M/79M
[INFO] ------------------------------------------------------------------------

Now, imagine you only want to rebuild module1. You can say to Maven: "hey, just build module 1 again" by adding: -P module1 at the commandline.

<a href="mailto:jochen@baileys">jochen@baileys</a> ~/Desktop/modules $ mvn clean install -P module1
[INFO] Scanning for projects...
[INFO] Reactor build order: 
[INFO]   PARENT
[INFO]   MODULE1
[INFO] ------------------------------------------------------------------------
8< ... >8
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] PARENT ................................................ SUCCESS [3.104s]
[INFO] MODULE1 ............................................... SUCCESS [1.241s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Tue Feb 09 18:08:46 CET 2010
[INFO] Final Memory: 15M/79M
[INFO] ------------------------------------------------------------------------

So, you just won 2 seconds. Seems stupid! Yes, of course, it's just an example project with nothing in it. But in a real life situation, you can save up to 2 minutes in a build process. If you build 30 times a day, you just saved yourself ... one hour waiting for builds! ;-)

Comments

Submitted by Mathijs (not verified) on Wed, 05/10/2016 - 09:44
 

Maven has this built-in as the '-pl' command line option.

Submitted by Mario (not verified) on Mon, 20/02/2017 - 08:28
 

In reply to by Mathijs (not verified)

But you have to use it with the option -am to build the parent too ;)

Submitted by Jochus on Tue, 21/02/2017 - 11:27
 

In reply to by Mario (not verified)

True, correct.

Add new comment

The content of this field is kept private and will not be shown publicly.

Full HTML

  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].