Apache Maven
lastmod 2025-12-08created 2014-01-0541 edits
Maven is distributed in several formats for your convenience. Use a source archive if you intend to build Maven yourself. Otherwise, simply pick a ready-made binary distribution and follow the installation instructions given at the end of this document. You will be prompted for a mirror - if the file is not found on yours, please be patient, as it may take 24 hours to reach all mirrors.
Maven is a tool that emerged to support the tasks that have repeatedly been carried out to develop applications. The tasks Maven supports are as follows.
- Builds
- Documentation
- Reporting
- Dependencies
- SCMs
- Releases
- Distribution
Some of the tasks above (Builds, Reporting, etc.) have been done with Ant until now, but since there was no consistent guideline, most of the work had to be repeated for every project. Maven, however, abstracts all the tasks needed for project management and implements them so that the tool supports them. Developers who move from Ant to Maven may feel some resistance to Maven's constraints. It is not as flexible as Ant (although integrating with Ant build scripts is of course possible), but it cuts down much of the work that had to be repeated when using Ant. Because this makes it possible to manage, deploy, and operate every project with a consistent structure, I think it is a suitable tool these days, when project complexity keeps growing.
Categories
Creating a Maven project
Once installation is done, let's create a Maven project. Run the command below at the command prompt. (The first time you run the command below, it takes quite a long time, because Maven downloads the plugins and modules it needs. The Maven distribution includes only the modules needed to use Maven for the first time, and does not include other modules needed to use Maven, such as the archetype plugin and the compiler plugin. These modules are loaded from the Maven central repository when they are actually needed.)
- groupId: Identifier of the group the project belongs to. A value representing a company, division, or organization goes here, with the hierarchy expressed in package format.
- artifactId: Identifier of the project's output. A value representing the project or module goes here.
- version: Enter the version of the output.
- package: Enter the package to create by default. If not entered, a package with the same structure as the groupId is created.
Previously, projects were created with the archetype:create goal. However, as maven-archetype-plugin was updated, the archetype:generate goal was added and the archetype:create goal was deprecated. Still, the archetype:create goal remains supported for backward compatibility.
Compile/test/package
To compile the source code, run the following command.
The compiled output is created in the target/classes directory.
Default directory structure of a Maven project
There is a phrase that comes up when talking about Maven: "Convention Over Configuration". It means "convention takes precedence over configuration". Rather than the developer configuring everything, just follow the rules set by default.
Maven_project_basic_structure.jpeg
When the archetype:generate goal runs successfully, a directory with the same name as the value entered for artifactId is created. In the case above, a subdirectory named simple-app is created in the current directory. The archetype selected in the process above is maven-archetype-quickstart, and the directory structure created when this archetype is selected is as follows.
- src/main/java - Contains Java source files.
- src/main/resources - Contains resource files such as properties and XML. Included in the classpath.
- src/main/webapp - Contains web application files. (WEB-INF directory, JSP files, etc.)
- src/test/java - Contains test Java source files.
- src/test/resources - Contains resource files used during testing. Included in the classpath used for tests.
Trying compile/running tests/packaging
To compile the source code, run the following command.
The compiled output is created in the target/classes directory.
Maven Lifecycle
Maven's predefined build order is called the lifecycle, and each build step in the lifecycle is called a page. A lifecycle is divided into several steps (phases), and each phase has dependencies on others.
- Default lifecycle
- validate : Validates the project
- compile : Compiles the source code
- test : Runs unit tests (by default, a failed unit test is treated as a build failure)
- package : Packages the compiled class files and resource files into a file such as a war or jar
- verify : Verifies integration tests
- install : Deploys the packaged file to the local repository (USER_HOEM/.m2/)
- deploy : Deploys the packaged file to a remote repository (nexus or the maven central repository)
- clean : Deletes all artifacts produced by the Maven build.
- site : Generates a documentation site for the project using the information in the Maven configuration file.
- site-deploy : Deploys the generated documentation site to the configured server
Maven Version Numbers
The version number of the artifact defined in the POM file is the same as the version number of the released product, for example 12.1.2.0.0, expressed using five digits, as described in the following:
In x.x.x-y-z:
- x.x.x is the release version number, for example 12.1.2.
- y is the PatchSet number, for example 0,1,2,3,… with no leading zeros.
- z is the Bundle Patch number, for example 0,1,2,3,… with no leading zeros.
- The periods and hyphens are literals.
Introducing Maven to an existing project
- How do I apply Maven to a web project developed without Maven? (with eclipse)
- Introducing Maven to an existing project 1
Selecting this brings up the process of creating the POM.xml file, as shown above. Group Id is the identifier of the project's group. Put simply, you can think of it as the project's package name. For Artifact Id, write a name that represents this project. Almost all libraries already registered in Maven Repositories seem to use lowercase letters and dashes (-) for their Group Id and Artifact Id. Do whatever suits you.
What are the benefits of using Maven?
The benefits of using Maven can be found in the Benefits of using Maven document. I also think using Maven brings so many benefits that it would be hard to list them all. Still, if I had to, these are the items I would mention first.
- Convenient dependent library management - Dependency Management
- The build process can be kept consistent across all projects
- The many plugins Maven provides can be used. In particular, the ability to easily convert a Maven project into an Eclipse-based project
- Setting up a new project is really easy and fast. Maven's archetype feature is truly satisfying.
Adding a specific local JAR file as a dependency
- IntelliJ IDEA#Adding a specific local JAR file as a dependency
- Apache Ant -> build.xml#Adding a specific local JAR file as a dependency
- Gradle -> build.gradle#Adding a specific local JAR file as a dependency
See pom.xml#Adding a specific local JAR file as a dependency.
Apache Ant vs Apache Maven vs Gradle
| Feature | |||
| Type | Task-based build tool | Declarative build and project management tool | Declarative build tool with configuration language support |
| Configuration language | XML | XML (POM file) | Groovy or Kotlin (DSL) |
| Build script | Written as tasks | Declarative, via the POM file | Flexible script-based, extensible with plugins |
| Dependency management | Requires a separate plugin | Built-in dependency management system | Built-in dependency management system |
| Performance | Processes tasks in a fixed sequence | Processes tasks in a fixed sequence | High performance through incremental builds and parallel task execution |
| Learning curve | Relatively easy | Moderate | Somewhat complex but flexible |
| Extensibility and flexibility | Plugins available but limited | Relatively limited | Strong extensibility and flexibility |
| Community and support | Widely used but declining | Popular and widely supported | Growing fast with plenty of support |
| Best suited for | Simple builds, simple projects | Java projects with a standardized structure | Large, complex projects and multi-language builds |
- Apache Ant is simple and suits projects that need customization.
- Apache Maven is useful for structured Java projects and helps with dependency management and project builds in a standardized way.
- Gradle is modern and highly flexible, and suits large projects that demand high performance.
Troubleshooting
An error occured while filtering resources
If Eclipse prints the error message below, select Alt + F5 or Maven > Update Project ....
Encoding error
An encoding-related error like the one below may occur.
[WARNING] File encoding has not been set, using platform encoding MS949, i.e. build is platform dependent!
In this case, configure it as follows.
<properties>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
</properties>
See also
Favorite site
- Apache Maven web site
- Maven Getting Started Guide
- Maven repository
- [Recommended] Setting up an Android development environment with Android Maven.
- [Recommended] Creating a web project with Maven in Eclipse (using Struts2) 2
- Introduction to Maven tool features 3
- About Maven 4
- Wikipedia (en) article on Apache Maven
- Maven usage tips
- Maven summary
- [Recommended] Maven basic usage 5
- Maven Chapter 6: Integrating Maven and Eclipse
- Maven pom.xml profile configuration
- What every Maven user should know (Dependency)
- How do I tell Maven to use the latest version of a dependency?
- Dependency Mediation and Conflict Resolution
- Finding list of versions available in a Maven repository for a specific plugin?
POM
- MvnModule
- POM Syntax
- [Recommended] Understanding Maven Version Numbers
- Snapshots (SNAPSHOT) in Maven versions 6
Maven eclipse plugin (m2e)
Guide
References
- Apache
- Apache Ant
- Application.properties
- Bazel
- Build Automation
- Build.gradle
- Build.xml
- Continuous Integration
- ContinuousIntegration:ImplementationGuide
- GitLab:PackageRegistry
- Gradle
- IntelliJ IDEA
- Java
- JUnit
- Log4j
- Mill
- Mvn
- Open Source
- Package management system
- Pom.xml
- Scala
- Sonatype Nexus
- Spray-json
- Spring