Apache Maven Faqs

I have included in this tutorial some common Maven Faqs that are frequently asked by developers.

Maven FAQ 1: How to override the default Maven local repository

One common strategy for overriding the defailt local repository consists in providing in your settings.xml file a new definition for localRepository as in this example:


As an alternative, you can add to the command line


Maven FAQ 2: How to override the default Maven encoding

The simplest way to do it is via Properties as in this example where we are setting UTF-8 as source encoding:


Maven FAQ 3: How to debug your Unit tests

Let’s say you have the following JUnit test in your Maven project:

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestJunit {

   String message = "Hello";	
   MessageUtil messageUtil = new MessageUtil(message);
   public void testPrintMessage() {	
      assertEquals(message, messageUtil.printMessage());     

First of all, you must enable debugging by setting the following parameter on the Command Line:    

$ mvn -Dmaven.surefire.debug test

When you start the test goal, you will notice that the dt_socket transport has been activated:

 T E S T S
Listening for transport dt_socket at address: 5005

Now, as last step, you have to setup your IDE (where our project source is) to debug the tests remotely.

Maven FAQ 4: How to pass command line parameters to Java execution

The simplest way to pass command line arguments to Java execution is via the -Dexec.args parameter as in this example:

$ mvn compile exec:java -Dexec.mainClass="com.example.Myclass" -Dexec.args="arg1 arg2"

Maven FAQ 5: How to package all dependencies in a fat JAR file

In order to package all the dependencies of your project in a single JAR file, the trick is to use the maven-assemply-plugin. See this example:


Next, you can run it with:

$ mvn clean compile assembly:single

Maven FAQ 6: How to execute a Java class from a Maven project:

You can use the exec-maven-plugin and specify the mainClass attribute in it:


Now to execute your com.example.MyClass, simply invoke the exec:java from the command line:

$ mvn exec:java

Maven FAQ 7: How to execute a Maven plugin on a condition

You can use profile with special activation conditions like this:



      <!-- your plugins here -->


Maven FAQ 8: How to execute a Maven plugin from the Command Line

So you want to execute a Maven plugin which is not included in your pom.xml file? the most direct means of executing your maven plugin is to specify the plugin goal directly on the command line:

$ mvn groupId:artifactId:version:goal

Getting Maven project version to the bash command line:

Maven FAQ 9: How to get Maven project version from the Command Line

Here is how you would invoke it on the command line to get the ${project.version}:

$ mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate \

Maven FAQ 10: How to find the latest version of a dependency

You can leverage the Maven Repository Search at https://search.maven.org/classic/#search adding the GAV information in the URL. For example, if you wanted to find the latest version for the Artifact “kie-ci” in the Group “org.kie”, you could query the Central Repository as follows: