Apache Maven
Maven Introduction
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.
Maven’s main functions
- manage project dependencies
- build project artifacts
Maven’s Mechanism
Dependency
Dependency Management
Dependency management is one of Maven’s most powerful features. It allows developers to declare the libraries their project depends on, and Maven will automatically download these libraries and their dependencies from remote repositories.
In a Maven project, dependencies are declared in the pom.xml file. Together, the combination of groupId, artifactId, and version forms a unique coordinate that Maven uses to locate, download, and manage dependencies. This coordinate system is critical for resolving dependencies during the build process and ensuring that the correct versions of libraries are used in the project.
groupId
: represents the organization or company that produces the project. It typically follows a reverse domain name convention to ensure uniqueness across the entire Maven ecosystem.artifactId
: identifies the specific project or module within the scope of the groupId. It is akin to the project’s name and should be unique within the groupId.version
: indicates the release version of the artifact. It follows semantic versioning rules and is crucial for dependency management.
1 | <dependency> |
POM
POM (Project Object Model) is the core configuration file used by Maven to manage a project. It is an XML file that contains information about the project, its dependencies, and other settings.
1 |
|
Maven enforces a standardized directory layout for several key reasons, which contribute significantly to the efficiency, maintainability, and consistency of projects managed by Maven.
Convention Over Configuration
Configuration Over Code
Project Structure
JAR(Java ARchive) project structure
1 | . |
WAR(Web ARchive) project structure
1 | . |
Dependency Scope
Maven uses dependency scopes to control how dependencies are included during different stages of the build process. The most common scopes include:
- compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects. - provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive. - runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath. - test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive. - system
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository. - import (only available in Maven 2.0.9 or later)
This scope is only supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
scope | valid range | transitive | example |
---|---|---|---|
compile | all | 是 | spring-core |
provided | compile, test | 否 | servlet-api |
runtime | runtime, test | 是 | JDBC |
test | test | 否 | JUnit |
system | compile, test | 是 |
Exclude Dependencies
Maven allows you to exclude dependencies from a project by adding them to the <exclusions>
tag in the <dependency>
tag. This can be useful when you want to use a different version of a dependency than the one specified in the parent project.
Maven Lifecycle
The Maven lifecycle is a series of phases that define the order in which tasks are executed. The most commonly used lifecycle is the default lifecycle, which includes the following phases:
validate
Validates that the project is correct and all necessary information is available.compile
Compiles the source code of the project.test
Runs tests for the project.package
Takes the compiled code and packages it in its distributable format, such as a JAR.integration
test: Processes and deploys the package if necessary into an environment where integration tests can be run.verify
Runs any checks to verify the package is valid and meets quality criteria.install
Installs the package into the local repository, for use as a dependency in other projects locally.deploy
Copies the final package to the remote repository for sharing with other developers and projects.
Apache Maven