Maven Learning Notes
Some learning insights on Maven.
Updated:
阅读中文版Maven Basics
1. Why Use Maven
1.1 Maven Provides Convenient Jar Package Management
-- Adding third-party jar packages -- Obtaining third-party jar packages -- Automatically handles jar package dependency relationships -- Automatically resolves conflicts between jar packages
1.2 Maven Provides Modular Project Management
-- Maven can automatically split and manage projects into modules
2. What is Maven
An awesome tool for automated build and project management! It focuses on project building and dependency management for the Java platform. In the history of JavaEE development, build tools have gone through a series of evolutions and changes: Make→Ant→Maven→Gradle→others...
3. Using Maven (Key Points)
3.1 Installing Maven
-- Extract the archive -- Configure environment variables -- Naming convention: MAVEN_HOME -- Note: Make sure the system has a JAVA_HOME variable set
3.2 Configuring the Local Repository and Aliyun Mirror
<localRepository>E:\LocalRepository</localRepository>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
3.3 Integrating Maven in IDEA
-- settings related configuration -- Other Settings -- If after the above configuration, reopening IDEA still uses the default configuration, refer to the following steps to resolve it https://blog.csdn.net/feilie01/article/details/102754451
3.4 Basic Usage of Maven in IDEA
-- Maven's default project directory structure (predefined, cannot be changed arbitrarily) Hello -- src -- main -- java -- resource -- test -- java -- pom.xml
-- Basic command operations for Maven projects in IDEA -- clean Cleans up the working directory -- compile Compiles the main program code -- test Compiles test programs and executes test code -- package Packaging operation -- install Installs the packaged artifact into the local repository
4. Maven Core Concepts
4.1 POM (Project Object Model)
POM is an abstract concept of the Project Object Model, concretely manifested as a philosophy of describing an entire module using a pom.xml file.
4.2 Maven's Predefined Directory Structure
-- Maven projects have their own set of conventions with a default predefined directory structure. The ability to automatically build projects and manage jar packages depends entirely on these predefined conventions.
-- Convention > Configuration > Coding
4.3 Coordinates
-- In Maven, coordinates represent the uniqueness of the current Maven project and also serve as the unique path for referencing this jar package in the future.
-- Naming conventions for coordinates in Maven
(1) groupId: Reverse order of the company or organization's domain + current project name (2) artifactId: The module name of the current project (3) version: The version of the current module
4.4 Dependency Management
1). Concept of dependencies: Maven projects can depend on each other through coordinates
2). Dependency scopes: -- compile (this is the default scope) (1) Java code in the main directory can access dependencies in this scope (2) Java code in the test directory can access dependencies in this scope (3) When deployed to a Tomcat server, it needs to be placed in the WEB-INF/lib directory For example: dependency on Hello. Needed by the main program, test programs, and server runtime. -- test (1) Java code in the main directory cannot access dependencies in this scope (2) Java code in the test directory can access dependencies in this scope (3) When deployed to a Tomcat server, it will not be placed in the WEB-INF/lib directory For example: dependency on junit. Only needed by the test program part.
-- provided (1) Java code in the main directory can access dependencies in this scope (2) Java code in the test directory can access dependencies in this scope (3) When deployed to a Tomcat server, it will not be placed in the WEB-INF/lib directory For example: servlet-api is provided by the Servlet container when running on the server, so it is not needed during deployment.
3). Dependency transitivity -- Dependencies between multiple Maven projects are transitive, but whether they can be passed on depends on the scope of the dependency being passed.
4). Dependency resolution principles: -- Shortest path wins -- If paths are the same, the one declared first wins
4.5 Lifecycle
Maven has three independent lifecycles:
- Clean Lifecycle performs some cleanup work before the actual build.
- Default Lifecycle is the core part of the build: compile, test, package, install, deploy, etc.
- Site Lifecycle generates project reports, sites, and publishes sites. (Rarely used)
5. Inheritance
5.1 Why an Inheritance Mechanism is Needed
Since dependency information outside the compile scope cannot be passed along the "dependency chain," projects that need it must configure it individually. For example:
| Hello | |
|---|---|
| HelloFriend | |
| MakeFriend |
At this point, if the project needs to unify the junit version across all modules to 4.9, manually modifying each project would be highly undesirable. Using the inheritance mechanism, such dependency information can be uniformly extracted into a parent project module for centralized management.
5.2 Creating the Parent Project
Change the parent project's packaging type to pom
<groupId>cn.lzhgy.maven</groupId>
<artifactId>Parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
5.3 Referencing the Parent Project in Child Projects
<!--Inheritance-->
<parent>
<groupId>cn.lzhgy.maven</groupId>
<artifactId>Parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--Specify the relative path from the current pom.xml file to find the parent project's pom.xml file-->
<relativePath>../Parent/pom.xml</relativePath>
</parent>
At this point, if the child project's groupId and version duplicate those of the parent project, they can be removed.
5.4 Managing Dependencies in the Parent Project
<!--Dependency Management-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
In the child project, re-specify the required dependency, removing the scope and version number
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
Chapter 6 Aggregation
In the overall aggregation project, use the modules/module tags to combine and specify the relative paths of the module projects
<!--Aggregation-->
<modules>
<module>../MakeFriend</module>
<module>../OurFriends</module>
<module>../HelloFriend</module>
<module>../Hello</module>
</modules>
Maven can automatically determine the installation order based on the inheritance and dependency relationships between modules.
Chapter 7 Maven Useful Websites
We can search for dependency information for the jar packages we need at http://mvnrepository.com/.
Comments(0)