What is Maven?

Most devs don’t take the time to learn about the technologies they use everyday. We’re busy people and once we’ve beaten the tool or technology into submission then why look any further? It does what you need it to do. However, not taking the time to learn about your most often used tools can have hidden costs. Maven is one such tool.

If you’re interested in learning more about what exactly Maven is then check out this post. If you want the spark notes, you’re in the right place.

How do I speed up my Maven build?

Maven performs differently per project but the tips below can usually help with build times.

If you’re reading this post then you probably only use Maven to build your project. You don’t know much about how it works but you know that if you launch a terminal in the root directory of your project and type: mvn clean install your project will eventually build. It works. Why mess with it? What more do you need to know? It was there when you started this job, so who cares?

The classic: mvn clean install command has hidden costs. mvn makes a call to maven. clean & install are arguments passed to Maven. Maven works in phases, all the phases together form the Maven Lifecycle.

The ‘clean’ Phase

clean is an explicit extra call to the clean phase of the lifecycle. The clean phase removes all temporary/build files generated by the previous build. Unless you’re having weird caching issues or inconsistent behavior when running/debugging your application you may be wasting time with every build. For small projects the impact of running clean is negligible but if you’ve got large build files, running the clean phase when you don’t have to is slowing you down. To test it try running “mvn install” and see if your build goes faster. **

**

The ‘install’ Phase

install is the meat of the classic ‘mvn clean install’ Maven command combo. As I stated earlier, Maven works in phases. What I didn’t tell you is that each phase is composed of all the phases that come before it in the Maven Lifecycle. When you call the ‘install’ phase you’re actually running the following phases:

  • validate
  • initialize
  • generate-sources
  • process-sources
  • generate-resources
  • process-resources
  • compile
  • process-classes
  • generate-test-sources
  • process-test-sources
  • generate-test-resources
  • process-test-resources
  • test-compile
  • process-test-classes
  • test
  • prepare-package
  • package
  • pre-integration-test
  • integration-test
  • post-integration-test
  • verify
  • install

Each phase has its own set of steps. For more info on each phase and what it does check out the Maven page. Depending on what you’re doing then you may not need to run all those phases. For example, if you just want to see if your unit tests pass after your recent changes then you could run mvn test instead of install. If you’re wanting to see if your code compiles then you could run mvn compile. If you know everything works and you just want to get your jar then running mvn package will work. No one expects you to remember all these phases. Take note of the ones that seem relevant to you and test them to see if your build speed improves.

CLI Arguments

Maven handles many arguments. The arguments I think will reduce your build time are listed below. For a complete listing, check out Maven’s CLI Reference.

-DskipTests

You most likely have the SureFire plugin configured for project. If you don’t need to run your unit tests but still would like to cover all the other parts of your normal build phase then adding -DskipTests to the end of your command will usually reduce your build time. Using  -DskipTests with your normal command would look something like this:

mvn clean install -DskipTests

-T [#of threads]

Maven can be run on a multiple threads. Adding -T [#of threads] to the end of your build command instructs maven to run your build in a multi-threaded way. You generally want to select 1 thread per core of your processor. So if you have an 4 core processor and a run the classic build command it would look something like this:

mvn clean install -T 4

Note: depending on the configuration of your project multiple threads may not work.  Give it a try, the worst outcome would be that the build fails.

-pl -am

If your build is slow then it’s likely that your app is made up of more than one project. However the work you’re doing most likely only involved one of those projects. The classic build command builds all of them by default. Why waste all that time building all of them when you only changed one. -pl stands for projects. It’s the argument you use to call out the specific project that you’d like to build.  Projects tend to be interdependent which means that sometimes only building the project you changed means that your build fails or successfully builds broken code. -am stands for ‘also make’. You don’t always need it but when you’re unsure add it to be safe. Combined with the classic build command it looks like this:

mvn clean install -pl YourProjectNameHere -am

-offline

Maven stores the binaries your project needs in a local repo. Regardless, Maven has a tendency to re-download binaries. This wastes time and bandwidth especially when you consider that the binaries haven’t changed and you’re just waiting for Maven to overwrite your local copies. To prevent this there’s the -offline command. It requires no explanation. When combined with a classic build command it looks like this:

mvn clean install -offline

The ideal build command

Piece by piece we’ve built a build command that will reduce our build time. If you bring all your CLI options together, you’ll get a build command like this one:

mvn install -pl YourProjectNameHere -am -offline -T 4

Your command may vary if you read the entire post and picked the options that are right for you.