Apache Maven is an open-source tool used for project management and comprehension. The tool is based on the project object model (POM) concept. With Apache <ave, you can easily manage all the aspects of a project including the build, documentation and reporting from a central point. In addition to that, you can use it to create sites and upload the build results. This is a very vital tool as it automates the process of creating an initial folder of Java apps, compiling, testing, packaging and deployment of products.
Maven enables developers to comprehend a full development cycle and state in the shortest time possible. This in turn makes the build process easier, improves development practices and provides quality project information.
The amazing features associated with Apache Maven are:
- It is easy to set up and use
- It can work with multiple projects simultaneously
- Instant access to new features and improvements with minimal or no configs required.
- It is highly extensive. You can write the desired plugins in Java and other languages.
- It encourages the use of a central repository for JARs and the other involved dependencies.
- It has a large and active community and libraries to use
This guide will provide the common commands involved when Using Apache Maven with examples where necessary.
Installing Apache Maven
Apache Maven can be installed on your system by following the guides available on our page:
To verify the installation, run:
$ mvn -version
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /opt/maven
Java version: 11.0.20.1, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-71-generic", arch: "amd64", family: "unix"
Using Apache Maven – Commands with Examples
After installing Apache Maven, you can proceed and use it as desired. Here is a list of the commonly used Apache Maven commands with examples:
mvn compile
This command is used to compile the main source code of the project located in src/main/java of the project. The command compiles all the Java files in the main directory. The command does not perform any tasks beyond the compile phase.
mvn compile
Sample Output:
[INFO] Compiling 1 source file with javac [debug target 1.8] to target/classes
[WARNING] bootstrap class path not set in conjunction with -source 8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.363 s
[INFO] Finished at: 2023-09-18T13:18:32+03:00
[INFO] ------------------------------------------------------------------------
mvn compiler:compile
This command is used when compiling the Java source codes for a Maven project. It tells Maven to use the maven-compiler-plugin when compiling the code. Here, the behaviour of the compiler can be customized in the pom.xml file. You can specify options such as the source and target Java versions, encoding etc. To use it, run:
$ mvn compiler:compile
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- compiler:3.11.0:compile (default-cli) @ my-app ---
[INFO] Changes detected - recompiling the module! :input tree
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 1.8] to target/classes
[WARNING] bootstrap class path not set in conjunction with -source 8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.435 s
[INFO] Finished at: 2023-09-18T13:19:02+03:00
[INFO] ------------------------------------------------------------------------
mvn compiler:testCompile
This command is used to compile the test classes of the Maven project.
$ mvn compiler:testCompile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-cli) @ my-app ---
[INFO] Changes detected - recompiling the module! :source
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 1.8] to target/test-classes
[WARNING] bootstrap class path not set in conjunction with -source 8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.614 s
[INFO] Finished at: 2023-09-18T13:19:38+03:00
[INFO] ------------------------------------------------------------------------
mvn package
It is used when building the Maven project into a JAR, WAR etc. To use it, execute the command:
mvn package
Sample output:
[INFO] Building jar: /home/thor/mvn-projects/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.826 s
[INFO] Finished at: 2023-09-18T13:20:05+03:00
[INFO] ------------------------------------------------------------------------
Once complete, you will see the location of the JAR file printed. The command executes the compile
, testCompile
and test
before proceeding with the build.
mvn install
This command will build and install the JAR, WAR, pom.xml etc project files to the local repo. To use it, run:
mvn install
Sample Output:
[INFO] Installing /home/thor/mvn-projects/my-app/pom.xml to /home/thor/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.pom
[INFO] Installing /home/thor/mvn-projects/my-app/target/my-app-1.0-SNAPSHOT.jar to /home/thor/.m2/repository/com/mycompany/app/my-app/1.0-SNAPSHOT/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.838 s
[INFO] Finished at: 2023-09-18T13:21:06+03:00
[INFO] ------------------------------------------------------------------------
mvn deploy
It deploys the artefacts to a remote repository configured in the pom.xml
file under with distributionManagement tag. To provide authentication details, include them in the settings.xml
file.
mvn deploy
mvn validate
It is used to validate and ensure that all the settings and the required information are correct for the Maven project.
$ mvn validate
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.196 s
[INFO] Finished at: 2023-09-18T13:21:59+03:00
[INFO] ------------------------------------------------------------------------
mvn dependency:tree
Used to display the dependency tree on your Maven project.
mvn dependency:tree
Sample Output:
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] | +- org.apiguardian:apiguardian-api:jar:1.0.0:test
[INFO] | +- org.junit.platform:junit-platform-launcher:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-suite-api:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-commons:jar:1.2.0:test
[INFO] +- org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test
.....
This command can be used to locate the unused dependencies and remove them from your pom.xml file.
mvn archetype:generate
This command is useful when generating a skeleton of the project. It generates the JAR, web apps, maven sites and many others.
mvn archetype:generate
You can also use the command along with all the required flags. For example
mvn archetype:generate -DgroupId=com.mycompany.app \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
mvn site:site
This command is used to create the site of the project. To use the command, run:
mvn site:site
There will be HTML files generated with information related to your project in the site directory. To view them access the below directory on your project.
$ ls target/site/
css/ dependency-info.html index.html plugins.html summary.html
dependencies.html images/ plugin-management.html project-info.html
mvn test
This is used when running the test cases for your Maven project. For example:
$ mvn test
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.036 s -- in com.mycompany.app.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.231 s
[INFO] Finished at: 2023-09-18T13:35:08+03:00
[INFO] ------------------------------------------------------------------------
mvn verify
This command is used when you need to verify that the quality criteria are met. It runs the test cases and checks to ensure all is okay.
mvn verify
Sample Output:
INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mycompany.app.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 s -- in com.mycompany.app.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ my-app ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.564 s
[INFO] Finished at: 2023-09-18T13:35:56+03:00
[INFO] ------------------------------------------------------------------------
mvn Clean
This command is used when deleting the Maven project. It cleans and deletes the target directory. To use it, switch to the Maven directory and issue:
$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ my-app ---
[INFO] Deleting /home/thor/mvn-projects/my-app/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.554 s
[INFO] Finished at: 2023-09-18T13:36:22+03:00
[INFO] ------------------------------------------------------------------------
How to use Maven Options
Maven allows the usage of several options. To view the list of the available options use:
$ mvn -help
usage: mvn [options] [<goal(s)>] [<phase(s)>]
Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode (disables output color)
-b,--builder <arg> The id of the build strategy to
use
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
--color <arg> Defines the color mode of the
output. Supported are 'auto',
'always', 'never'.
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a user property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml)
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
......
Below are some of the most commonly used maven options:
mvn -o package
This command is used to run the maven build command in the offline mode. This command is only used when all the required JAR files are already downloaded and available in the local repo.
mvn -o package
mvn -q package
This runs the build process in a quiet mode, only the results and errors are provided for this.
mvn -q package
mvn -f dir/pom.xml package
The command is used when building a project from another path. You need to specify the location of the pom.xml file:
mvn -f dir/pom.xml package
mvn -T 4 clean install
The command is used to run parallels Maven builds with the provided count. This command can be used to reduce the build time since the modules can be built simultaneously. For example:
mvn -T 4 clean install
mvn -X package
The command is used to display the Maven version and run the build in debug mode. Here, all the messages are printed.
mvn -X package
Sample Output:
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /opt/maven
Java version: 11.0.20.1, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-71-generic", arch: "amd64", family: "unix"
...
INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.832 s
[INFO] Finished at: 2023-09-18T13:38:09+03:00
[INFO] ------------------------------------------------------------------------
[DEBUG] Shutting down adapter factory; available factories [file-lock, rwlock-local, semaphore-local, noop]; available name mappers [discriminating, file-gav, file-hgav, file-static, gav, static]
[DEBUG] Shutting down 'file-lock' factory
[DEBUG] Shutting down 'rwlock-local' factory
[DEBUG] Shutting down 'semaphore-local' factory
[DEBUG] Shutting down 'noop' factory
mvn -V package
The command prints the Maven version and then proceeds with the build process.
mvn -V package
##OR
mvn -v;mvn package
mvn -DskipTests package
The option is used to skip the tests during the build process:
mvn -DskipTests package
##OR
mvn -Dmaven.test.skip=true package
Conclusion
This guide can be used as a reference material when dealing with the Apache Maven project. We have provided some of the commonly used Maven commands and the example usage. I hope this was helpful.
See more on our page:
- Install DSpace Repository on Ubuntu
- Deploy JFrog Artifactory on Kubernetes With Ingress
- Analyze Java code using Gradle in SonarQube and Jenkins