Download MVN: Tips and Tricks for Using Apache Maven Effectively
Maven is a build-automation tool designed to provide a comprehensive and easy-to-use way of developing Java applications. It uses a POM (Project Object Model) approach to create a standardized development environment for multiple teams. It also handles project dependencies, plugins, reports, and other aspects of the build process.
Maven's primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. To achieve this goal, Maven deals with several areas of concern:
Making the build process easy: Maven simplifies the build process by providing a uniform way of building any Java-based project. It also shields developers from many details of the underlying mechanisms.
Providing a uniform build system: Maven builds a project using its POM and a set of plugins. Once you familiarize yourself with one Maven project, you know how all Maven projects build.
Providing quality project information: Maven provides useful project information that is partly taken from the POM and partly generated from the project's sources. For example, Maven can provide change logs, cross-referenced sources, mailing lists, dependencies, test reports, code analysis reports, etc.
Encouraging better development practices: Maven aims to gather current principles for best practices development and make it easy to guide a project in that direction. For example, specification, execution, and reporting of unit tests are part of the normal build cycle using Maven.
In this article, we will show you how to download and install Maven on your system, how to use some common Maven commands, and how to troubleshoot some common Maven errors.
Downloading and installing Maven
To install Maven on your system, you need the following:
A system running Windows, Linux, or Mac OS X.
A working Internet connection.
Access to an account with administrator privileges.
Access to the command prompt or terminal.
A copy of Java installed and ready to use, with the JAVA_HOME environment variable set up (learn how to set up the JAVA_HOME environment variable in our ).
To install Maven on your system, follow these steps:
How to download and install Apache Maven on Windows
Download Maven zip file and extract it
Add MAVEN_HOME system variable and directory in PATH variable
Verify Maven installation with mvn -v command
Download Apache Maven 3.9.2 latest release
Maven system requirements and supported JDK versions
How to use Maven toolchains to build against different JDKs
How to verify the signature of the Maven release bundles
How to download Maven source code from source repository
How to build Maven from source code
How to update Maven to the latest version
How to uninstall Maven from Windows
How to download and install Maven on Linux
How to download and install Maven on Mac OS X
How to configure Maven settings.xml file
How to create a Maven project using mvn archetype:generate command
How to run a Maven project using mvn package or mvn install command
How to test a Maven project using mvn test command
How to clean a Maven project using mvn clean command
How to skip tests in Maven using -DskipTests option
How to add dependencies to a Maven project using pom.xml file
How to use Maven repositories to manage dependencies
How to create a custom Maven repository using Nexus or Artifactory
How to use Maven plugins to enhance the build process
How to create a custom Maven plugin using Mojo API
How to use Maven profiles to customize the build for different environments
How to use Maven properties to parameterize the build configuration
How to use Maven filters to replace placeholders in resources files
How to use Maven resources plugin to copy and filter resources files
How to use Maven compiler plugin to specify the source and target Java version
How to use Maven surefire plugin to run unit tests and generate reports
How to use Maven failsafe plugin to run integration tests and generate reports
How to use Maven jacoco plugin to measure code coverage and generate reports
How to use Maven checkstyle plugin to enforce coding standards and generate reports
How to use Maven findbugs plugin to detect bugs and generate reports
How to use Maven PMD plugin to detect code quality issues and generate reports
How to use Maven sonar plugin to perform code analysis and generate reports
How to use Maven site plugin to generate project documentation and reports
How to use Maven deploy plugin to deploy artifacts to a remote repository or server
How to use Maven release plugin to automate the release process of a project
How to use Maven assembly plugin to create a single executable jar file with dependencies
How to use Maven shade plugin to create an uber-jar file with shaded dependencies
How to use Maven war plugin to create a web application archive file for deployment
How to use Maven ear plugin to create an enterprise application archive file for deployment
How to use Maven ejb plugin to create an enterprise Java bean archive file for deployment
How to use Maven spring boot plugin to create a self-contained executable jar file for spring boot applications
Visit the and download the version of Maven you want to install. The Files section contains the archives of the latest version. Access earlier versions using the archives link in the Previous Releases section.
Extract the distribution archive in any directory and add the bin directory with the mvn command to the PATH environment variable. Alternatively use your preferred archive extraction tool.
Add a system variable named MAVEN_HOME and point it to the root Maven directory. Also add %MAVEN_HOME%\bin to the PATH variable.
To verify that Maven is installed correctly, open a new command prompt or terminal window and run the following command:
You should see output similar to this:
Apache Maven 3.8.4 (9b656c72d54e8b2b6a976121d22377b6a53c422e) Maven home: C:\Program Files\apache-maven-3.8.4 Java version: 11.0.13, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-11.0.13 Default locale: en_US, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
If you see a message like this, congratulations! You have successfully installed Maven on your system. If you see an error message or no output at all, check your installation steps and environment variables again.
Using Maven commands
Maven provides a set of basic commands that allow you to perform common tasks such as cleaning, compiling, testing, packaging, and installing your project. Here are some of the most frequently used Maven commands:
mvn clean: This command deletes the target directory with all the build data created by Maven during the build process.
mvn compile: This command compiles the source code of your project and puts the compiled classes in the target/classes directory.
mvn test: This command runs the unit tests of your project using a suitable testing framework such as JUnit or TestNG. It also generates test reports in the target/surefire-reports directory.
mvn package: This command takes the compiled code and packages it into a distributable format such as a JAR, WAR, or EAR file. It also runs the unit tests before packaging.
mvn install: This command installs the packaged artifact into the local repository, which can be used as a dependency in other projects locally.
You can also combine these commands in a single execution by using a space as a separator. For example, mvn clean install will first clean the project and then install it.
Maven also provides some advanced commands that allow you to perform more complex tasks such as generating project skeletons, analyzing dependencies, creating documentation, and deploying artifacts to remote repositories. Here are some of the most useful Maven commands for advanced users:
mvn archetype:generate: This command creates a new project from a template called an archetype. An archetype is a predefined project structure that follows certain conventions and best practices. You can choose from a list of available archetypes or create your own.
mvn dependency:tree: This command displays the dependency tree of your project, showing all the direct and transitive dependencies that your project depends on. It also shows the scope and version of each dependency.
mvn site: This command generates a website for your project that contains various information such as project description, license, developers, mailing lists, issue tracking, source code management, reports, etc. The website is generated in the target/site directory by default.
mvn deploy: This command deploys the packaged artifact to a remote repository for sharing with other developers and projects. You need to configure the repository details in your POM or settings.xml file before using this command.
Maven also supports several command-line options that modify the