Welcome to our tutorial on Apache Maven FAQs! Apache Maven is a popular build automation tool used for Java projects. It helps developers manage project dependencies, build, test, and deploy projects. In this tutorial, we will cover some frequently asked questions about Maven and provide helpful answers and explanations.
Overview of Apache Maven
Apache Maven is an open-source build automation tool used primarily for Java projects. It helps developers manage the build process and project dependencies more effectively. Maven uses a Project Object Model (POM) to manage project builds and dependencies, allowing developers to easily manage and automate the build process.
Why do we use Maven? There are several benefits to using Maven for build automation. Maven simplifies the build process and makes it easier for developers to manage project dependencies. It also provides a standard format for building projects, making it easier for developers to understand and work with each other’s projects. Additionally, Maven integrates well with other tools and IDEs, such as Eclipse and IntelliJ IDEA.
How can I create a maven project quickstart archetype in non-interactive way?
To create a Maven quickstart archetype in a non-interactive way, you can use the following command:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.example -DartifactId=my-project -DinteractiveMode=false
This will create a new Maven project with the maven-archetype-quickstart archetype, with the group ID com.example, the artifact ID my-project, and the interactive mode disabled. The project will be created in a directory with the same name as the artifact ID (my-project in this case).
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
Keep in mind that the local repository location in the local settings file or on the command line will only apply for the current project. To set the default local repository location for all projects, you will need to modify the global Maven settings.xml file.
How do you build a project using a specific JDK version in Maven?
To build a Maven project using a specific JDK version, you can specify the version in the project’s pom.xml file using the Maven Compiler Plugin. For example, to build the project using JDK 11, you can add the following snippet to your pom.xml file:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> </configuration> </plugin>
Please note that you can also specify a custom location for the java executable either by configuration or through the command line. For example:
-Dmaven.compiler.executable=/path/to/jdk11/bin/javac
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>3.1.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
To learn more about Java Main class execution in Maven check this tutorial: Run a Java Class from Maven made simple
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"
How to create a runnable JAR file with dependencies?
This configuration specifies that the Assembly Plugin should create a JAR file with dependencies and set the main class to be executed in the manifest.
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.sample.MyClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
To build it execute:
mvn clean compile assembly:single
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>
How to skip Test execution
The simplest way to skip Test execution is by adding the -DskipTests=true option in the Command Line:
mvn clean verify -DskipTests=true
Alternatively, by using the maven-surefire-plugin in pom.xml:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M3</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build>
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.
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
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>
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:
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
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:
Conclusion
This article was a walk through some common Apache Maven Faqs. We hope that this article has answered some of your questions about Maven and provided you with the information you need to get started with it.