How to Set Default Maven and Java settings per Project


Nearly all Maven projects require setting specific Maven options or Java options to build correctly. In this tutorial, you will be shown how configure default options at the project level.

Maven now supports the creations of a .mvn directory in your project’s root. Within this directory two files can be placed, one for configuring Maven and another for Java.

What this allows us to accomplish is to set defaults for the project. This is use case is especially desirable when running multiple projects through a build sever, each requiring different settings.

Create the .mvn Directory

Within the root directory of your project, create a new directory called .mvn.

mkdir /path/to/project/.mvn


Inside of the .mvn directory, create a new file named maven.config. Within this file we can specify any argument that is passed to the mvn command.

For example, if there is a required system parameter that must be set during a mvn package execution, we set the property inside of the maven.config file.

Profiles can also be set. Typically, you would set a default active profile within your project’s pom.xml file. However, the default profile may not be the appropriate profile in all circumstances, so a different profile would be set using the mvn -P flag. Rather than specifying it from the command-line, we can set it in our maven.config file.

Create new file called maven.config in the .mvn directory in your project.

touch /path/to/project/.mvn/maven.config

The following is an example of a maven.config file. Within it, we set two profiles named cloud-build and aws. It will also skip the test phase of a build.

-Dskiptests=true -P cloud-build,aws

With the .mvn/maven.config file in place, when we run mvn clean package from the command-line, Maven will automatically pull these options in.


More useful, we can also set the Java options for our project. By including this file we can save onboarded developers the hassle of knowing the JVM settings. It also simplifies build pipelines, as we will no longer be required to set JAVA_OPTS environment variable parameters for our project.

Create the java.config file in the .mvn directory.

touch /path/to/project/.mvn/java.config 

Add the desired Java options to the file, as if you were setting the options via the export command for creating the JAVA_OPTS environment variable.

For example, a project may need to adjust the Xms and Xmx settings to build correctly. We set these values in the java.config file as we would have by export JAVA_OPTS.

-Xms256m -Xmx512m

Like the maven.config file, Maven will automatically detect the presence of the java.config file, and then use its contents to set the Java environment.