Apache Maven
Download Apache Maven. 2.1 Visit Maven official website, download the Maven zip file, for example: apache-maven-3.6.0-bin.zip. Welcome to Apache Maven. Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. Apache group then developed Maven which can build multiple projects together, publish projects information, deploy projects, share JARs across several projects and help in collaboration of teams. The primary goal of Maven is to provide developer with the following −. Apache Commons Lang, a package of Java utility classes for the classes that are in java.lang's hierarchy, or are considered to be so standard as to justify existence in java.lang. License Apache 2.0.
- Apache Maven Compiler Plugin
- Apache Maven Install
- Apache Maven
- Apache Maven Api
- Apache Maven Repository
- Apache Maven 3.3.9
Downloading Apache Maven 3.5.3
Apache Maven 3.5.3 is the latest release and recommended version for all users.
[if-any logo] [end] The currently selected download mirror is [preferred]. If you encounter a problem with this mirror, please select another mirror. If all mirrors are failing, there are backup mirrors (at the end of the mirrors list) that should be available. You may also consult the complete list of mirrors.
System Requirements
Java Development Kit (JDK) | Maven 3.3+ require JDK 1.7 or above to execute - they still allows you to build against 1.3 and other JDK versions by Using Toolchains |
Memory | No minimum requirement |
Disk | Approximately 10MB is required for the Maven installation itself. In addition to that, additional disk space will be used for your local Maven repository. The size of your local repository will vary depending on usage but expect at least 500MB. |
Operating System | No minimum requirement. Start up scripts are included as shell scripts and Windows batch files. |
Files
Maven is distributed in several formats for your convenience. Simply pick a ready-made binary distribution archive and follow the installation instructions. Use a source archive if you intend to build Maven yourself.
In order to guard against corrupted downloads/installations, it is highly recommended to verify the signature of the release bundles against the public KEYS used by the Apache Maven developers.
Link | Checksums | Signature |
---|---|---|
Binary tar.gz archive | apache-maven-3.5.3-bin.tar.gz | apache-maven-3.5.3-bin.tar.gz.asc |
Binary zip archive | apache-maven-3.5.3-bin.zip | apache-maven-3.5.3-bin.zip.asc |
Source tar.gz archive | apache-maven-3.5.3-src.tar.gz | apache-maven-3.5.3-src.tar.gz.asc |
Source zip archive | apache-maven-3.5.3-src.zip | apache-maven-3.5.3-src.zip.asc |
- Distributed under the Apache License, version 2.0
Previous Releases
It is strongly recommended to use the latest release version of Apache Maven to take advantage of newest features and bug fixes.
If you still want to use an old version you can find more information in the Maven Releases History and can download files from the archives for versions 3.0.4+ and legacy archives for earlier releases.
1. Introduction
Building a software project typically consists of such tasks as downloading dependencies, putting additional jars on a classpath, compiling source code into binary code, running tests, packaging compiled code into deployable artifacts such as JAR, WAR, and ZIP files, and deploying these artifacts to an application server or repository.
Apache Maven automates these tasks, minimizing the risk of humans making errors while building the software manually and separating the work of compiling and packaging our code from that of code construction.
In this tutorial, we're going to explore this powerful tool for describing, building, and managing Java software projects using a central piece of information — the Project Object Model (POM) — that is written in XML.
2. Why Use Maven?
The key features of Maven are:
- simple project setup that follows best practices: Maven tries to avoid as much configuration as possible, by supplying project templates (named archetypes)
- dependency management: it includes automatic updating, downloading and validating the compatibility, as well as reporting the dependency closures (known also as transitive dependencies)
- isolation between project dependencies and plugins: with Maven, project dependencies are retrieved from the dependency repositories while any plugin's dependencies are retrieved from the plugin repositories, resulting in fewer conflicts when plugins start to download additional dependencies
- central repository system: project dependencies can be loaded from the local file system or public repositories, such as Maven Central
3. Project Object Model
The configuration of a Maven project is done via a Project Object Model (POM), represented by a pom.xml file. The POM describes the project, manages dependencies, and configures plugins for building the software.
The POM also defines the relationships among modules of multi-module projects. Let's look at the basic structure of a typical POM file:
Let's take a closer look at these constructs.
3.1. Project Identifiers
Maven uses a set of identifiers, also called coordinates, to uniquely identify a project and specify how the project artifact should be packaged:
Apache Maven Compiler Plugin
- groupId – a unique base name of the company or group that created the project
- artifactId – a unique name of the project
- version – a version of the project
- packaging – a packaging method (e.g. WAR/JAR/ZIP)
The first three of these (groupId:artifactId:version) combine to form the unique identifier and are the mechanism by which you specify which versions of external libraries (e.g. JARs) your project will use.
3.2. Dependencies
These external libraries that a project uses are called dependencies. The dependency management feature in Maven ensures automatic download of those libraries from a central repository, so you don't have to store them locally.
This is a key feature of Maven and provides the following benefits:
- uses less storage by significantly reducing the number of downloads off remote repositories
- makes checking out a project quicker
- provides an effective platform for exchanging binary artifacts within your organization and beyond without the need for building artifact from source every time
In order to declare a dependency on an external library, you need to provide the groupId, artifactId, and the version of the library. Let's take a look at an example:
As Maven processes the dependencies, it will download Spring Core library into your local Maven repository.
3.3. Repositories
A repository in Maven is used to hold build artifacts and dependencies of varying types. The default local repository is located in the .m2/repository folder under the home directory of the user.
If an artifact or a plug-in is available in the local repository, Maven uses it. Otherwise, it is downloaded from a central repository and stored in the local repository. The default central repository is Maven Central.
Some libraries, such as JBoss server, are not available at the central repository but are available at an alternate repository. For those libraries, you need to provide the URL to the alternate repository inside pom.xml file:
Please note that you can use multiple repositories in your projects.
3.4. Properties
Custom properties can help to make your pom.xml file easier to read and maintain. In the classic use case, you would use custom properties to define versions for your project's dependencies.
Maven properties are value-placeholders and are accessible anywhere within a pom.xml by using the notation ${name}, where name is the property.
Let's see an example:
Now if you want to upgrade Spring to a newer version, you only have to change the value inside the<spring.version> property tag and all the dependencies using that property in their <version> tags will be updated.
Properties are also often used to define build path variables:
3.5. Build
The build section is also a very important section of the Maven POM. It provides information about the default Maven goal, the directory for the compiled project, and the final name of the application. The default build section looks like this: Usb mac os bootable.
The default output folder for compiled artifacts is named target, and the final name of the packaged artifact consists of the artifactId and version, but you can change it at any time.
3.6. Using Profiles
Another important feature of Maven is its support for profiles. A profile is basically a set of configuration values. By using profiles, you can customize the build for different environments such as Production/Test/Development:
As you can see in the example above, the default profile is set to development. If you want to run the production profile, you can use the following Maven command:
4. Maven Build Lifecycles
Every Maven build follows a specified lifecycle. You can execute several build lifecyclegoals, including the ones to compile the project’s code, create a package, and install the archive file in the local Maven dependency repository.
4.1. Lifecycle Phases
The following list shows the most important Maven lifecycle phases:
- validate – checks the correctness of the project
- compile – compiles the provided source code into binary artifacts
- test – executes unit tests
- package – packages compiled code into an archive file
- integration-test – executes additional tests, which require the packaging
- verify – checks if the package is valid
- install – installs the package file into the local Maven repository
- deploy – deploys the package file to a remote server or repository
4.2. Plugins and Goals
A Maven plugin is a collection of one or more goals. Goals are executed in phases, which helps to determine the order in which the goals are executed.
The rich list of plugins that are officially supported by Maven is available here. There is also an interesting article how to build an executable JAR on Baeldung using various plugins.
To gain a better understanding of which goals are run in which phases by default, take a look at the default Maven lifecycle bindings.
To go through any one of the above phases, we just have to call one command:
For example, mvn clean install will remove the previously created jar/war/zip files and compiled classes (clean) and execute all the phases necessary to install new archive (install).
Please note that goals provided by plugins can be associated with different phases of the lifecycle.
5. Your First Maven Project
In this section, we will use the command line functionality of Maven to create a Java project.
5.1. Generating a Simple Java Project
In order to build a simple Java project, let's run the following command:
The groupId is a parameter indicating the group or individual that created a project, which is often a reversed company domain name. The artifactId is the base package name used in the project, and we use the standard archetype.
Since we didn't specify the version and the packaging type, these will be set to default values — the version will be set to 1.0-SNAPSHOT, and the packaging will be set to jar.
If you don't know which parameters to provide, you can always specify interactiveMode=true, so that Maven asks for all the required parameters.
After the command completes, we have a Java project containing an App.java class, which is just a simple “Hello World” program, in the src/main/java folder.
We also have an example test class in src/test/java. The pom.xml of this project will look similar to this:
As you can see, the junit dependency is provided by default.
5.2. Compiling and Packaging a Project
The next step is to compile the project:
Apache Maven Install
Maven will run through all lifecycle phases needed by the compile phase to build the project's sources. If you want to run only the test phase, you can use:
Now let's invoke the package phase, which will produce the compiled archive jar file:
5.3. Executing an Application
Finally, we are going to execute our Java project with the exec-maven-plugin. Let's configure the necessary plugins in the pom.xml:
The first plugin, maven-compiler-plugin, is responsible for compiling the source code using Java version 1.8. The exec-maven-plugin searches for the mainClass in our project.
Apache Maven
To execute the application, we run the following command:
6. Multi-Module Projects
The mechanism in Maven that handles multi-module projects (also called aggregator projects) is called Reactor.
The Reactor collects all available modules to build, then sorts projects into the correct build order, and finally, builds them one by one.
Let's see how to create a multi-module parent project.
6.1. Create Parent Project
First of all, we need to create a parent project. In order to create a new project with the name parent-project, we use the following command:
Next, we update the packaging type inside the pom.xml file to indicate that this is a parent module:
6.2. Create Submodule Projects
In the next step, we create submodule projects from the directory of parent-project:
To verify if we created the submodules correctly, we look in the parent-project pom.xml file, where we should see three modules:
Apache Maven Api
Moreover, a parent section will be added in each submodule’s pom.xml:
6.3. Enable Dependency Management in Parent Project
Dependency management is a mechanism for centralizing the dependency information for a muti-module parent project and its children.
When you have a set of projects or modules that inherit a common parent, you can put all the required information about the dependencies in the common pom.xml file. This will simplify the references to the artifacts in the child POMs.
Let's take a look at a sample parent's pom.xml:
By declaring the spring-core version in the parent, all submodules that depend on spring-core can declare the dependency using only the groupId and artifactId, and the version will be inherited:
Moreover, you can provide exclusions for dependency management in parent's pom.xml, so that specific libraries will not be inherited by child modules:
Finally, if a child module needs to use a different version of a managed dependency, you can override the managed version in child's pom.xml file:
Please note that while child modules inherit from their parent project, a parent project does not necessarily have any modules that it aggregates. On the other hand, a parent project may also aggregate projects that do not inherit from it.
For more information on inheritance and aggregation please refer to this documentation.
6.4. Updating the Submodules and Building a Project
Apache Maven Repository
We can change the packaging type of each submodule. For example, let's change the packaging of the webapp module to WAR by updating the pom.xml file:
Now we can test the build of our project by using the mvn clean install command. The output of the Maven logs should be similar to this:
7. Conclusion
In this article, we discussed some of the more popular features of the Apache Maven build tool.
Apache Maven 3.3.9
All code examples on Baeldung are built using Maven, so you can easily check our GitHub project website to see various Maven configurations.