Ways to make maven build faster? Ways to make maven build faster? multithreading multithreading

Ways to make maven build faster?


Note: First thing is AFAIK, No other in built options available in maven apart from the all answers here.


Running maven build with Multiple threads works for me to speed up the builds.For example :

mvn clean install -T100

where -T is for specifying how many threads you want based on your hardware.

Below are the variants from wiki

Maven 3.x has the capability to perform parallel builds. The command is as follows:

  • mvn -T 4 clean install Builds with 4 threads
  • mvn -T 1C clean install 1 thread per cpu core
  • mvn -T 1.5C clean install 1.5 thread per cpu core

How Execution is evaluated(See Parallel builds in Maven 3)?

enter image description here

Each node in the graph represents a module in a multi-module build, the "levels" simply indicate the distance to the first module in the internal reactor dependency graph. Maven calculates this graph based on declared inter-module dependencies for a multi-module build. Note that the parent maven project is also a dependency, which explains why there is a single node on top of most project graphs. Dependencies outside the reactor do not influence this graph.

Finally if you want to skip test execution you can also use -DskipTests as well.

Caution : Some of your plugins may not be compatible for multithreaded builder, it may work. but it will give below warning message. you may need to see plugin documentation for multithreading support.

[WARNING] *****************************************************************                                                                                                                                 [WARNING] * Your build is requesting parallel execution, but project      *                                                                                                                                 [WARNING] * contains the following plugin(s) that have goals not marked   *                                                                                                                                 [WARNING] * as @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 test-project:                                                                                                                          [WARNING] de.dentrassi.maven:rpm:0.9.2                                                                                                                                                                      [WARNING] Enable debug to see more precisely which goals are not marked @threadSafe.                                                                                                                        [WARNING] *****************************************************************    


On my actual project :

  1. mvn clean install [INFO] Total time: 01:05 h
  2. mvn clean install -DskipTests [INFO] Total time: 18:35 min
  3. mvn clean install -Dmaven.test.skip -DskipTests [INFO] Total time: 10:58 min
  4. mvn -T 1C clean install -Dmaven.test.skip -DskipTests [INFO] Total time: 04:00 min
  5. We can also skip the javadoc to be generated as Archmed commented by adding -Dmaven.javadoc.skip=true mvn -T 1C clean install -Dmaven.test.skip -DskipTests -Dmaven.javadoc.skip=true
  6. Dont use * imports, on IntelliJ, choose > Analyze > Run inspection by name > * imports , to find all * imports and correct it.
  7. Remove all unused imports in your project > on Intellij > Analyze > Run inspection by name > unused imports
  8. Remove all unused code (classes, variable, field, parameter, etc..), on Intellij : Analyze > run inspection by name > unused declaration.
  9. Upgrade to last JAVA VERSION
  10. I have found that the task mvn clean, is taking 2 minutes to clean the TARGET folder before building. I did create a new task called quickclean, and i am using it instead of clean, this way mvn -T 1C quickclean install -Dmaven.test.skip -DskipTests . This new task quickclean is only renaming the build folder from TARGET to TARGET-yyyy-MM-dd-HH-mm(what is VERY FAST). So now, every time you make a new mvn quickclean install..., you have a folder with the time of the build. The inconvient, it's that this may take a lot of space on the hard disk, so you have to clean all this directories sometimes. So for that i have created another task called: trashclean, to put all this folder to trash. I am running this tasks maybe on time per week. or month, depending on my work mvn trashclean.

Here is what you need to add to your pom.xml if you want to use this concept

<properties>    <timestamp>${maven.build.timestamp}</timestamp>    <maven.build.timestamp.format>yyyy-MM-dd-HH-mm</maven.build.timestamp.format>    <trashdir>trash/target-${maven.build.timestamp}</trashdir></properties>    <profile>        <id>quickclean</id>        <build>            <plugins>                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <executions>                        <execution>                            <id>rename_target</id>                            <phase>pre-clean</phase>                            <goals>                                <goal>run</goal>                            </goals>                            <configuration>                                <tasks>                                    <move todir="${trashdir}" failonerror="false">                                        <fileset dir="target/"/>                                    </move>                                </tasks>                            </configuration>                        </execution>                    </executions>                </plugin>            </plugins>        </build>    </profile>    <profile>        <id>trashclean</id>        <build>            <plugins>                <plugin>                    <groupId>org.apache.maven.plugins</groupId>                    <artifactId>maven-antrun-plugin</artifactId>                    <executions>                        <execution>                            <id>clean_trash</id>                            <phase>clean</phase>                            <goals>                                <goal>run</goal>                            </goals>                            <configuration>                                <tasks>                                    <delete dir="trash/" failonerror="false"/>                                </tasks>                            </configuration>                        </execution>                    </executions>                </plugin>            </plugins>        </build>    </profile>    


If using commandline you can check how many cores your machine has and use all of them, if you also want to skip your tests, you can add -DskipTestsFor example, I have 8 core processor:

mvn -T 8C clean install -DskipTests