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:

<localRepository>custom_path</localRepository>

As an alternative, you can add to the command line

-Dmaven.repo.local=custom_path

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:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

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);
   
   @Test
   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:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

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:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4.0</version>
  <configuration>
    <mainClass>com.example.MyClass</mainClass>
  </configuration>
</plugin>

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:

<project>
  ...
  <profiles>
    <profile>
      <id>my-test-plugins</id>

      <activation>
        <property><name>!maven.test.skip</name></property>
        <property><name>!skipTests</name></property>
      </activation>
      <build>
        <plugins>

      <!-- your plugins here -->

        </plugins>
      </build>
    </profile>
  </profiles>
</project>

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 \
    -Dexpression=project.version

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:

0
0
0
s2smodern