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
Creating project
-
Generate project in batch mode
A couple of meaningful properties are then required:
- The
archetypeGroupId
,archetypeArtifactId
andarchetypeVersion
defines the archetype to use for project generation. - The
groupId
,artifactId
,version
andpackage
are 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
archetype
plugin that provide thearchetype:generate
goal to create a simple project based upon amaven-archetype-quickstart
archetype.Plugin is a collection of goals with a general common purpose.
To create war file, we have to use
-DarchetypeArtifactId=maven-archetype-webapp
.The meaning of some above attributes:
Name Description groupId
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. artifactId
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. packaging
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. version
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.
- The
-
Create archetype from existing project
mvn archetype:create-from-project
Build project
-
Clean project
# Clears the target directory into which Maven normally builds your project. mvn clean
-
Compile project
mvn compile
-
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
mvn deploy
Query commands
-
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:tree
goal of themaven-dependency-plugin
. -
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-plugin
that 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
With:
-pl
:-am
: means--also-make
to tells Maven to build the projects required by the list in-pl
.
-
Analyze code quality
mvn clean install -DskipTests=true mvn sonar:sonar
Wrapping up
-
By default your version of Maven might use an old version of the
maven-compiler-plugin
that 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 themaven-compiler-plugin
and set themaven.compiler.release
property 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>
Refer:
ducmanhphan.github.io/2019-01-24-Understanding-about-project-lifecycle-in-Maven
http://maven.apache.org/archetype/maven-archetype-plugin/examples/create-multi-module-project.html
http://maven.apache.org/archetype/maven-archetype-plugin/
https://loda.me/huong-dan-tao-spring-boot-voi-nhieu-modules-bang-gradle-loda1553919997213/
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
https://www.educba.com/maven-commands/
https://dzone.com/articles/10-effective-tips-on-using-maven
Build fat jar file