📜 ⬆️ ⬇️

Accelerate the build process with maven

Probably many of you work with Maven. If so, then I suppose each of you collects your project every day several times, especially if you are now in the active development phase and changes affect many modules. At a certain point in time, the project becomes quite large and the build starts every day to be executed longer and longer ... And now comes the time when it's time to do something about it.

Migrate from Maven 2 to Maven 3

Are you still using maven2? Strange. Migration to version 3 alone can significantly speed up the build process. In my project, the transition to Maven 3 gave an increase in assembly speed by 10%. Most likely due to some fixes and optimizations that were made in the new version (as the developers say, the amount of code was significantly reduced and greatly refactored). Migration will take you a few minutes and in most cases will be painless. Although there is a chance that some configuration files will have to be corrected.

We use kernels and additional threads.

Maven 3 has a great option:
mvn -T 4 clean intall mvn -T 2C clean install 

In the first case, we explicitly indicate that we want to start the build process on 4 threads. In the second case, we indicate that 2 threads should be allocated to each core for the build process. This is one of the new features of the new mavena - Parallel Builds. This feature analyzes the dependency graph of your project.
and shoves the modules in different threads, the assembly of which can be performed in parallel.
For my current project, the build speed with the second parameter (-T 2C) accelerated by 20%. The truth is there is one drawback. The amount of resources that will be consumed for the build can grow significantly. In my case, it is + 30% of consumed memory.
I want to immediately pay attention - if the connectivity of the modules in your project is very low - then
assembly speed of this option can be increased by an order of magnitude.
This, by the way, is a reason to think about your project architecture. After all, if the build takes a lot of time, a small refactoring will help reduce this figure. Although, of course, this is all very individual.
In general, the developers say that the increase in assembly speed can be 20-50%.

Divide and rule

Now I will say a commonplace thing, but - if you have a solid project, it is better to break it down into logical modules. Fragmentation should not become an end in itself. But the more splitting and less dependency the faster the build process. Distributing tests across modules will also increase performance, and in principle it is convenient.
')
mvnsh

Maven Shell is, in essence, just an active maven command line. The whole trick is that it hangs constantly in memory, therefore with each new build there is no need to run on a new JVM. In addition, all configuration files are cached and all plugins are already preloaded. You can read more here and here .

Plugins

Make sure that you do not use extra plugins and do not use additional ones where you can do without them. Maven is a whole eco-system, so it is important to understand that each element of this system affects the overall performance.
It is also worth mentioning that during the build with the "-T" parameter you can find this in the execution log:
 [WARNING] ***************************************************************** [WARNING] * Your build is requesting parallel execution, but project * [WARNING] * contains the following plugin(s) that are not marked as * [WARNING] * @threadSafe to support parallel building. * [WARNING] * While this /may/ work fine, please look for plugin updates * [WARNING] * and/or request plugins be made thread-safe. * [WARNING] * If reporting an issue, report it against the plugin in * [WARNING] * question, not against maven-core * [WARNING] ***************************************************************** [WARNING] The following plugins are not marked @threadSafe in eridanus-supervoid-module: [WARNING] org.codehaus.mojo:jboss-maven-plugin:1.5.0 

Therefore, in fact, it is desirable to use the latest versions of plug-ins, because they are likely to be thread-safe. As you understand, if a plugin is not thread-safe, then all its tasks cannot be distributed between threads with the "-T" option.

I would be grateful for any additional information that will help speed up the build.

Source: https://habr.com/ru/post/141393/


All Articles