In this article, we will learn how to use some commands in Maven to solve some problems when encountering Java project. Using Maven helps us to concentrate on source code, do not take care about libraries, … when changing their version.
Let’s get started.
Table of contents
Generate project in batch mode
A couple of meaningful properties are then required:
archetypeVersiondefines the archetype to use for project generation.
packageare the main properties to be set. Each archetype require these properties. Some archetypes define other properties; refer to the appropriate archetype’s documentation if needed.
mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.company -DartifactId=project -Dversion=1.0-SNAPSHOT -Dpackage=com.company.project
With above command, we are using
archetypeplugin that provide the
archetype:generategoal to create a simple project based upon a
Plugin is a collection of goals with a general common purpose.
To create war file, we have to use
The meaning of some above attributes:
Defines a unique base name of the organization or group that created the project. This is normally a reverse domain name. For the generation the groupId also defines the package of the main class.
Defines the unique name of the project. If you generate a new project via Maven this is also used as root folder for the project.
Defines the packaging method. This could be e.g. a jar, war or ear file. If the packaging type is pom, Maven does not create anything for this project, it is just meta-data.
This defines the version of the project.
Some common archetypes in Maven:
maven-archetype-quickstart: create Java project with sample files.
maven-archetype-simple: creates a Java project without sample files.
maven-archetype-webapp: create a web app project.
Create archetype from existing project
# Clears the target directory into which Maven normally builds your project. mvn clean
Run unit tests
# it also compile a project mvn test
Build a package
# Builds the project and packages the resulting JAR file into the target directory # also execute unit tests mvn package
Run integration test
# also build a package mvn verify
Install a package into a local repository
# Builds the project described by your Maven POM file and installs the resulting artifact (JAR) into your local Maven repository # also executes integration tests mvn install
Install an artifact into local repository
# skip integration test execution mvn -DskipITs=true install # skip unit and integration test execution mvn -DskipTests=true install # skip compiling test and test execution mvn -Dmaven.test.skip=true
Deploy artifact into enterprise repository
Show a tree of all dependencies
mvn dependency tree | less
This command is used to resolving dependency-related issues, such as using the wrong versions. It is described in the
dependency:treegoal of the
Show the relationship between dependencies
mvn dependency:analyze -DfailOnWarning=true
It is used when we want to explicitly declare dependencies, then, we can easily remove unused imports, …
For example, how to use dependency plugin in pom file
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>analyze-deps</id> <goals> <goal>analyze-only</goal> </goals> </execution> </executions> </plugin>
Skip test during a local build
# skip the running tests (recommneded) mvn package -DskipTests=true # skip the compilation and running of tests mvn package -Dmaven.test.skip=true
Running a specific test
In this case, we are using the
maven-surefire-pluginthat is responsible for running unit tests.
mvn clean package -Dtest=MyTest#testMethod
Build specific modules in multi-modules project
mvn clean install -pl <module_name> -am
--also-maketo tells Maven to build the projects required by the list in
Analyze code quality
mvn clean install -DskipTests=true mvn sonar:sonar
By default your version of Maven might use an old version of the
maven-compiler-pluginthat is not compatible with Java 9 or later versions. To target Java 9 or later, you should at least use version 3.6.0 of the
maven-compiler-pluginand set the
maven.compiler.releaseproperty to the Java release you are targetting (e.g. 9, 10, 11, 12, etc.).
For example, configure Maven project to use version 3.8.1 of maven-compiler-plugin and target Java 11.
<properties> <maven.compiler.release>11</maven.compiler.release> </properties> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> </plugins> </pluginManagement> </build>
Build fat jar file