
What if the build with Maven is too slow? After all, when an assembly lasts too long, anyone, even the most patient developer, can get bored and distract.
For a quick search on Google or for bookmarks, I immediately propose a final solution:
mvn package -am -o -Dmaven.test.skip -T 1C
- to build a project
without tests .
This story began with the fact that I downloaded a fairly large Java project from our corporate repository. I think that many, like me, would immediately assemble him with a team:
mvn clean package
Well, many team:
mvn clean install
My project was assembled in 25 minutes, and each time it was going as long as the first time.
package instead of install
According to the project
lifecycle of the project after the package phase, on which we get a full jar file, there is a phase of verify and then install. Often it is necessary to get exactly the jar-file, which is not necessary to be placed in a local repository, so in this situation I stopped at the package and saved some time.
')
Parallel assembly
During normal launch, Maven does not use all the capabilities of modern processors for parallelizing calculations on different cores, collecting modules one by one. Fortunately, you can use the -T parameter, specifying Maven to build a dependency graph and assemble the modules in parallel.
The parameter can be used in different ways:
mvn -T 4 package
or
mvn -T 1C package
In the first case, you specify the number of threads to use Maven, and in the second, that you need to use one thread per CPU core.
I am going to use the second method of setting a parameter tied to the number of cores in my final team.
After experimenting with the number of threads per core, try the following options:
mvn -T 1C package mvn -T 1.5C package mvn -T 2C package
- I did not get any noticeable increase in the case of more than 1 thread per core on a compiled project on a Core i7 processor.
I also note that parallel assembly is an experimental function of Maven 3. During assembly, problems may arise when using plugins that are not @threadSafe. In particular, these are the plugins:
- Surefire with the parameter forkMode = never, surefire [2.6,) warns (assert) about this.
- maven-modello-plugin, fixed from version 1.4,
- All maven-archiver clients (EAR, EJB, JAR, WAR etc), fixed in recent versions
and libraries:
- plexus-utils 2.0.5
- maven-archiver 2.4.1
- plexus-archiver 1.0
- plexus-io 1.0
Incremental build
As we discussed above, the project is most often built by the team:
mvn clean package
The clean command is used to clean the project, but is it necessary every time you build it? No, we usually want an incremental update of the project, and Maven is capable of it with the help of the command:
mvn package -am
Maven collects the module and updates those modules on which this module depends, in case something has changed in them.
This option speeds up the assembly of projects from many modules, in which, as a rule, point changes are made to 1-2 modules.
Offline build
It often happens that artifacts on external repositories are not updated very often, especially when they are artifacts of a certain version, or these artifacts must be supported by your efforts (- Welcome to the new project!). It is quite logical in this situation to tell Maven that you do not need to re-download them every time from the repositories:
mvn package -o
or
mvn package --offline
We for brevity will focus on the first version.
Test skip
Perhaps the most controversial proposal to optimize the speed of the assembly, I left last. To those who fully share the values of TDD, I simply propose to scroll through this paragraph and remove the -Dmaven.test.skip parameter from our final command.
I will not deny that tests are certainly needed, and the programmer must understand the part of the responsibility that he assumes by disabling them. But if you suddenly come across a new gigantic project, in which someone once wrote tests, and they do not work, then you will have to turn them off first.
As for the Maven launch option, I want to note only that, as a rule, in order to skip tests, use the command:
mvn package -DskipTests
But you can also skip the compilation of tests, if you run the command in the following form:
mvn package -Dmaven.test.skip
Let's sum up
So, once again, the team that we got to speed up the build with Maven:
mvn package -am -o -Dmaven.test.skip -T 1C
The result, shown on the project I am collecting, is good: instead of 25 minutes, the build began to take place in 30 seconds.
The authors of several other articles on optimizing build speed with Maven also recommend using compiler launch optimization parameters:
MAVEN_OPTS= -XX:+TieredCompilation -XX:TieredStopAtLevel=1 mvn package
- but, firstly, I did not feel any real speeding up of the assembly, apparently because I was already using the incremental build and in this case the compilation process took not so much time, and secondly, There are a sufficient number of mentions that these parameters can cause OutOfMemory errors and other problems.
I hope that in the comments you will also share statistics on speeding up the assembly of your projects!