Maven is a build tool, but not only. It’s able to manage to whole lifecycle of your library/application.

I’ll use the project describe in a previous post and the source is available on github.

 

 1 : Work online / offline

One of the common usage of maven, is to work most of the time connected. Before going deeper, let explain quickly how maven work. when you run the following command to display the project hierarchy :

mvn dependency:tree

It will check on you build repository for new snapshots, or dependencies update to display it. There’s lot of command that will lead to an update of either your project dependencies, or the plug-in used by maven to build your project.

In order to control when you check update or not, you have the –offline to disable all remote access, and the -U as example, to force verification of updates. The maven –help command list all the options possible at Maven level, it’s independent of the content of your pom, or of any setting you defined.

mvn –offline dependency:tree

 

2: Use several thread to work

By default, maven will use One thread to work. Since maven3, you can use several thread to do the job.

You have 2 options to define the number of threads to use

  • The first one is by explicitly specifying the number of threads to use (2 in the example)

mvn -T2 compile

  • The second one is by specifying the number of thread relatively to the number of core you have on the PC (2 thread per core in the example)

mvn -T2C compile

The second option is very interesting on a heterogeneous build farm or in script, where you can adapt you number of thread regarding the number of cores available at run time.

 

3: Build partially your project

You can also optimise your build time , when running  :

mvn compile

Maven will build the list of project/module to build, and will determine a build order.  For each module, it will check if building it make sense (if no sources, tests or ressources change, there’s no point to rebuild the project).

Even if the verification is efficient, doing it on large project make no sense when, as a developper, you know where you change stuff, and want to do only a partial build.

Let’s see some running examples :

mvn compile

will generate a full build

[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Reactor Build Order:
[INFO]
[INFO] module1
[INFO] module2
[INFO] project1
[INFO] P2module1
[INFO] project2
[INFO] Root
[INFO]
[INFO] ————————————————————————
[INFO] Building module1 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO] Compiling 1 source file to C:\Dev\GITHUB\mavenTraining\Project1\module1\target\classes
[INFO]
[INFO] ————————————————————————
[INFO] Building module2 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ module2 —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] ————————————————————————
[INFO] Building project1 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] ————————————————————————
[INFO] Building P2module1 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ module1 —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] ————————————————————————
[INFO] Building project2 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO] ————————————————————————
[INFO] Building Root 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO] ————————————————————————
[INFO] Reactor Summary:
[INFO]
[INFO] module1 ……………………………………. SUCCESS [  1.020 s]
[INFO] module2 ……………………………………. SUCCESS [  0.017 s]
[INFO] project1 …………………………………… SUCCESS [  0.001 s]
[INFO] P2module1 ………………………………….. SUCCESS [  0.013 s]
[INFO] project2 …………………………………… SUCCESS [  0.001 s]
[INFO] Root ………………………………………. SUCCESS [  0.001 s]
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 1.240 s
[INFO] Finished at: 2014-10-07T10:00:38+01:00
[INFO] Final Memory: 11M/214M
[INFO] ————————————————————————

With such small project, optimizing build time in a intellectual game, but can save lot of time on large project 🙂 So, let’s optimize it.

You can launch the build from a module level (ex: module2). We are going to use the maven “-rf”  parameter to do so (resume from).

[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Reactor Build Order:
[INFO]
[INFO] module2
[INFO] project1
[INFO] P2module1
[INFO] project2
[INFO] Root
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.
count of 1
[INFO]
[INFO] ————————————————————————
[INFO] Building module2 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ module2 —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] ————————————————————————
[INFO] Building project1 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] ————————————————————————
[INFO] Building P2module1 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO][INFO]
[INFO] — maven-compiler-plugin:2.5.1:compile (default-compile) @ module1 —
[INFO] Nothing to compile – all classes are up to date
[INFO]
[INFO] ————————————————————————
[INFO] Building project2 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] ————————————————————————
[INFO] Building Root 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO] ————————————————————————
[INFO] Reactor Summary:
[INFO]
[INFO] module2 ……………………………………. SUCCESS [  0.689 s]
[INFO] project1 …………………………………… SUCCESS [  0.002 s]
[INFO] P2module1 ………………………………….. SUCCESS [  0.016 s]
[INFO] project2 …………………………………… SUCCESS [  0.002 s]
[INFO] Root ………………………………………. SUCCESS [  0.003 s]
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 0.900 s
[INFO] Finished at: 2014-10-07T10:04:05+01:00
[INFO] Final Memory: 7M/214M
[INFO] ————————————————————————

 

or even directly from a project :

[INFO] Scanning for projects…
[INFO] ————————————————————————
[INFO] Reactor Build Order:
[INFO]
[INFO] project2
[INFO] Root
[INFO]
[INFO] ————————————————————————
[INFO] Building project2 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] ————————————————————————
[INFO] Building Root 1.9-SNAPSHOT
[INFO] ————————————————————————
[INFO] ————————————————————————
[INFO] Reactor Summary:
[INFO]
[INFO] project2 …………………………………… SUCCESS [  0.003 s]
[INFO] Root ………………………………………. SUCCESS [  0.068 s]
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 0.252 s
[INFO] Finished at: 2014-10-07T10:04:53+01:00
[INFO] Final Memory: 6M/214M
[INFO] ————————————————————————

 

Not so bad, we reduce build time by 80% 🙂

As you see, maven will scan for project,  construct the ordered list of projects, and will start build at the module or project.

You have several option to control more precisely the resume, let see some :

Options:
-am,–also-make                         If project list is specified, also build projects required by the  list
-amd,–also-make-dependents            If project list is specified, also build projects that depend on  projects on the list
-B,–batch-mode                         Run in non-interactive (batch)  mode
-fae,–fail-at-end                         Only fail the build afterwards;  allow all non-impacted builds to  continue
-ff,–fail-fast                                   Stop at first failure in reactorized builds
-N,–non-recursive                     Do not recurse into sub-projects
-pl,–projects <arg>                   Comma-delimited list of specified  reactor projects to build instead  of all projects.
A project can be  specified by [groupId]:artifactId  or by its relative path.
-rf,–resume-from <arg>          Resume reactor from specified project

Advertisements