Article directory
1. Maven Overview
1. Maven introduction: maven
is a project management tool that abstracts the project development and management process into a project object model (POM
).
Note:
POM(Project Object Model)
: Project Object Model
The maven management project process is as follows:
2.The role of Maven
- Project construction: Provides a standard, cross-platform automated project construction method
- Dependency management: Conveniently and quickly manage the resources (jar packages) that the project depends on to avoid version conflicts between resources.
- Unified development structure: Provide a standard and unified project structure
3.Maven repository
- Warehouse: used to store resources, including various jar packages
- Warehouse classification:
- Local warehouse: A warehouse that stores resources on your own computer. Connect to a remote warehouse to obtain resources.
- Remote warehouse: a warehouse on a non-local computer that provides resources for the local warehouse
- Central warehouse: maintained by the Maven team, a warehouse that stores all resources
- Private server: a department/company-wide warehouse that stores resources and obtains resources from the central warehouse
- The role of private servers:
- Save copyrighted resources, including purchased or self-developed jars (the jars in the central warehouse are all open source, and copyrighted resources cannot be stored)
- Sharing resources within a certain range is only open to internal users and not shared externally.
4.Maven coordinates
- Coordinate concept: Coordinates in Maven are used to describe the location of resources in the warehouse
- The main components of coordinates
groupId
: Organization id, defines the name of the organization to which the current Maven project belongs (usually the domain name written in reverse, for example: org.mybatis)artifactld
: Project id, defines the current Maven project name (usually the module name, such as CRM, SMS)version
:Version number, defines the current project version number
- The role of coordinates: Use a unique identifier to uniquely locate the resource location. Through this identifier, the identification and downloading of resources can be completed by the machine.
2. Download and installation of Maven
2.1 Download
Official website download:
- Latest version of book:https://maven.apache.org/download.cgi
- Kishi version:https://archive.apache.org/dist/maven/maven-3/
The latest version of:
historic version:
2.2 Installation
Download the latest installation package here apache-maven-3.9.3-bin.zip
, place it in any folder, right-click and unzip it to the current folder.
Here I unzipped it toE:\Applications\apache-maven-3.9.3
Configure environment variables:
1. Create a new MAVEN_HOME
environment variable, the variable value is the maven installation path
- variable name:
MAVEN_HOME
- variable:
E:\Applications\apache-maven-3.9.3
2.Repair Path
Change amount
After selecting the Path variable in System Variables, click the "Edit" button, then click "New" and enter %MAVEN_HOME%\bin
3. Enter the command to test whether the installation is successful.
mvn -v
The following interface appears, indicating that the installation is successful:
3. Maven warehouse configuration
3.1 Modify local warehouse configuration
Local warehouse: After maven is started, the downloaded resources will be automatically saved to the local warehouse.
- Default location: In the
.m2
folder of the directory where the login username is located, such asC:\Users\Admin\.m2\repository
(saved to C drive by default)
<localRepository>${user.name}\.m2\repository</localRepository>
- Custom location: Modify the local warehouse location to the specified path, such as
D:\repository
<localRepository>D:\repository</localRepository>
Since it is saved to the C drive by default, you need to modify the local warehouse configuration. The steps are as follows:
1. Find the file in the conf
folder of the installation package and use or other tools to open it settings.xml
vscode
2.Ctrl+F
Search localRepository
and copy the custom configuration just here
3.2 Modify remote warehouse configuration
Remote warehouse: A warehouse that provides resources for the local warehouse. Resources are downloaded from the remote warehouse to the local warehouse.
1.Default remote warehouse:
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
2. Mirror remote warehouse:
Mirror warehouse configuration:
id
: The unique identifier of the image, used to distinguish differentmirror
elementsmirrorOf
: Which warehouse to mirror, simply speaking, which warehouse is replaced. Fill in here ascentral
, which is to replace the default remote warehousename
:Image nameurl
:Mirror URL
Modify the mirror warehouse address: The purpose of modifying the mirror warehouse address is to speed up the download speed. Here it is changed to the Alibaba Cloud mirror. The address of the remote warehouse is transferred from abroad to China, and the download speed is naturally faster.
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
Still modify the files in the E:\Applications\apache-maven-3.9.3\conf
folder:settings.xml
3.3 Modified settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<localRepository>D:\repository</localRepository>
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
</settings>
4. Create a project using Maven
The directory structure of the Maven project is as follows: the Java project is on the left and the Web project is on the right.
It can be found that the Web project has one more webapp directory than the Java project.
4.1 Manually create Java projects
Since the maven created by Idea does not have settings.xml
at the beginning, every time a new maven
project is created, the default settings.xml
configuration, that is, using the default local warehouse address and remote warehouse address.
- Default local warehouse address:
${user.name}\.m2\repository
- Default remote warehouse address:
https://repo.maven.apache.org/maven2
Preparatory work: Copy the above modified apache-maven-3.9.3\conf\settings.xml
file to the ${user.name}\.m2\repository
folder, then every time you create a new maven project, it will be defaulted Using this configuration, you can solve the problem of slow downloading of jar packages when you first start the project.
1.New Project, create a new project
2. Fill in the project name and other information, and check Maven for Build System
Manually create a Java project, and the resulting Maven project structure is as follows:
3. Modify the maven configuration of the project, open Settings
and enter maven
in the search box, and modify the following content
Note: Every time you create or import a maven project, the maven configuration will use the default configuration, so you need to modify its maven configuration every time.
4.2 Prototype creation Java project
ArcheType
Selectorg.apache.maven.archetypes:maven-archetype-quickstart
to create a prototypeJava
project
Note: Using the prototype to create a maven project will download some jar packages, so it will take some time to build the project.
Use the prototype to create a Java project, and the resulting Maven project structure is as follows:
4.3 Prototype creation web project
ArcheType
: Select org.apache.maven.archetypes:maven-archetype-webapp
to create a Web
engineering project using the prototype
Note: Using the prototype to create a maven project will download some jar packages, so it will take some time to build the project.
Use the prototype to create a Web project, and the resulting Maven project structure is as follows:
Taking this web project as an example, due to its missing structure, the folders need to be manually added to make it a complete web project:
- Create a new java directory under the main directory and mark it as
Sources Root
- Create a new test directory under the src directory, and create java and resources under the test directory.
- marks the java in the test directory as
Test Sources Root
and resources asTest Resources Root
The final web project structure is as follows:
5. Tomcat starts the Web project
5.1 Start the project using the Tomcat plug-in
mvnrepository
Official website address:https://mvnrepository.com/
1. Enter tomcat maven
in the search box, click Search
to search, find Apache Tomcat Maven Plugin :: Common API
, and then click org.apache.tomcat.maven
Link to enter
Note: Click on the
org.apache.tomcat.maven
blue link instead ofApache Tomcat Maven Plugin :: Common API
in boldface. Clicking on the link in boldface will not find the tomcat plug-in.
2.Apache Tomcat Maven Plugin :: Tomcat 7.x
, click the link to enter, continue to select a version and click to enter
3. Obtained the Tomcat7 plug-in groupId、artifactId、version
and copied it to pom.xml
to complete the plug-in installation
4. The final content of pom.xml is as follows
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>maven-project</artifactId>
<packaging>war</packaging> <!--打包方式为war包-->
<version>1.0-SNAPSHOT</version>
<name>maven-project Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--构建项目需要的信息-->
<build>
<!--插件列表-->
<plugins>
<!--安装tomcat7插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
5. Open the maven sidebar, find the tomcat7 plug-in, clicktomcat7:run
, and start the tomcat server
6. After running, a link will be displayed at the end, which is the address of the local project. Click the link to view the web project.
Click http://localhost:8080/maven-project
and the following page will pop up:
The reason why the page displaysHello World!
is because when using the prototype to create a web project, a index.jsp
is automatically created with the following content:
Note: If you do not want to use the default
8080
port and default path, you can make the following configuration modifications
<!--构建项目需要的信息-->
<build>
<!--插件列表-->
<plugins>
<!--安装tomcat7插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!--配置端口号和路径-->
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
Start tomcat again and visit the following address:http://localhost:80/
In addition, you can also perform the following configuration operations to simplify the startup of the tomcat server:
The upper right corner of the idea, Current File
drop-down box selection Edit Configurations
Click +
and select Maven
Fill in the configuration name, command line and select which project to apply to
In the future, you can use the following method to start the tomcat server
5.2 Install Tomcat startup project locally
5.2.1 Download and install Tomcat
1. Download tomcat, here takes tomcat9
as an example
Government location:https://tomcat.apache.org/
Click on the left column under Download
Tomcat9
Click 64-bit Windows zip (pgp, sha512)
link to download and get apache-tomcat-9.0.78-windows-x64.zip
compressed package
2. Install tomcat, put the compressed package in any folder and unzip it, select to unzip it to the current folder, and the installation will be successful.
Here I unzipped it toE:\Applications\apache-tomcat-9.0.78
3. Configure tomcat environment variables
Create a new variable as follows:
- variable name:
CATALINA_HOME
- variable:
E:\Applications\apache-tomcat-9.0.78
Modify the path variable and add%CATALINA_HOME%\bin
4. Verify whether the configuration is successful: open cmd, enter startup.bat
Enter
Note: Before performing this step, first check whether tomcat is closed elsewhere, otherwise the port number (8080) will be occupied, causing startup failure (such as checking whether the tomcat plug-in has stopped running in Idea)
After the startup is completed, open the browser and enter http://localhost:8080
. If the image shown in the figure appears, the installation and configuration are successful
Note: The garbled code problem that occurs when running the
startup.bat
script above can be solved by modifyingapache-tomcat-9.0.78\conf\logging.properties
exploitlogging.properties
text, search java.util.logging.ConsoleHandler.encoding = UTF-8
, grasp UTF-8
changeGBK
immediately Possible:
java.util.logging.ConsoleHandler.encoding = GBK
Restart the script at this time startup.bat
, and there will be no garbled code problem:
5.2.2 Local Tomcat startup project
After the local tomcat installation and configuration is completed, follow the following three steps to start the web project:
- Packing war package
- Place the war package in the webapps directory
- Start the startup.bat script
1. Pack the war package: Open the maven sidebar, find Lifecycle
, and click package
, and wait for the build to complete. < a i=3>Generate a war package in the folder, here istarget
maven-project-1.0-SNAPSHOT.war
2. general maven-project-1.0-SNAPSHOT.war
rush apache-tomcat-9.0.78\webapps
letter article
3. Start the startup.bat
script. After the startup is completed, enter the project address in the browser to view the web project
The project address consists of: http://localhost:8080/
, followed by the war package name
Taking the project I just ran as an example, the address ishttp://localhost:8080/maven-project-1.0-SNAPSHOT
6. Maven dependency management
6.1 Dependency configuration
Dependencies: refers to the jar packages required to run the current project. A project can set multiple dependencies.
<!-- 设置当前项目所依赖的所有jar -->
<dependencies>
<!-- 设置具体的依赖 -->
<dependency>
<groupId>junit</groupId> <!-- 依赖所属群组id -->
<artifactId>junit</artifactId> <!-- 依赖所属项目id -->
<version>3.8.1</version> <!-- 依赖版本号 -->
</dependency>
</dependencies>
6.2 Dependency transfer
6.2.1 Dependency transfer demonstration
Dependencies are transitive:
- Direct dependencies: dependencies established through dependency configuration in the current project
- Indirect dependency: If the resource being relied upon depends on other resources, the current project indirectly depends on other resources.
1. Create an empty projectmaven-project
, and then create three modules under the project, named project01
, project02
,project03
Create an empty project:
Create three modules: right clickmaven-project -> New -> Module
The final project structure is as follows:
2. Make the following modifications to the pom.xml of the three modules
project01
,project02
,project03
intermediate additionlog4j
1.2.12
, 's edition book dependence1.2.13
,1.2.14
project01
Addjunit 4.12
version dependencies- Add
project03
as a dependent resource toproject02
Note: If the current project wants to use something from another project, then add the other project to the current project as a dependent resource.
project01
targetpom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project01的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加log4j依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!--添加junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
project02
targetpom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project02的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加log4j依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
<!--添加project03依赖-->
<dependency>
<groupId>com.example</groupId>
<artifactId>project03</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
project03
targetpom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project03的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project03</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加log4j依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</project>
3. After the above configuration is completed, click the Maven refresh button, and you can see the final dependency structure as follows:
- Use dependencies in red boxes
jar
Packages are direct dependencies - Use dependencies in purple boxes
jar
Packages are indirect dependencies
Explanation:
1. For theproject01
project, it directly depends onlog4j:log4j:1.2.12
and < a i=4>, indirectly depends on 2. For the project, it directly depends on and , indirectly dependent on 3. For the project, directly dependent onjunit:junit:4.12
org:hamcrest:hamcrest-core:1.3
project02
log4j:log4j:1.2.13
com.example:project03:1.0-SNAPSHOT
log4j:log4j:1.2.14
project03
log4j:log4j:1.2.14
6.2.2 Dependency transitive conflict
Dependency transfer conflict problem:
- Path priority: When the same resource appears in dependencies, the deeper the hierarchy, the lower the priority, and vice versa.
- Declaration priority: When resources are dependent on the same level, the ones in the earlier configuration order override those in the later order.
Special priority: When different versions of the same resource are configured at the same level, the one configured later overwrites the one configured first.
1. Path priority: When the same resource appears in dependencies, the deeper the hierarchy, the lower the priority, and vice versa.
Hereproject02
The project relies on both the 1.2.13 and 1.2.14 versions of log4j. Since 1.2.13 is a direct dependency, the level is shallower and the priority is higher, so Finally, the version dependency of 1.2.13
is used.
Note: The dependency color of log4j version 1.2.14 in idea is gray, indicating that it is not effective.
2. Declaration priority: When resources are dependent on the same level, the one in the earlier configuration order overrides the one in the later order.
对 project01
, project02
, project03
, 3-piece model purposepom.xml
Modifications below:
project01
targetpom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project01的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加project03依赖-->
<dependency>
<groupId>com.example</groupId>
<artifactId>project03</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--添加project02依赖-->
<dependency>
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
project02
targetpom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project02的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加log4j依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
</dependencies>
</project>
project03
targetpom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project03的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project03</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加log4j依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</project>
The final dependency structure is as follows: In the pom.xml of project01
, the dependency of project03
is added first, and then < a i=3>’s dependencies, their dependencies are at the same level. Since is added first, ’s a> dependency takes effect. project02
log4j
project03
project03
log4j 1.2.14
3. Special priority: When different versions of the same resource are configured at the same level, the one configured later overwrites the one configured first.
Make the following changes toproject02
'spom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project02的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加log4j依赖,1.2.13版本-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
<!--添加log4j依赖,1.2.14版本-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</project>
Two copies of the same dependency are written in the same pom.xml, and the dependency configured later overwrites the previous one, so the final version of log4j 1.2.14
takes effect here.
6.3 Optional dependencies
Optional dependencies: refers to hiding the resources currently relied on from the outside (opaque)
In other words, other people who use the dependent resource do not know that it is used because the dependent resource is hidden.
Addoptional
to the dependency and configure it astrue
to hide the dependency:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<optional>true</optional> <!--隐藏依赖-->
</dependency>
Demonstration: The original pom.xml configurations of project01
and project02
are as follows
project01
ofpom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project01的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加project02依赖-->
<dependency>
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
project02
ofpom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project02的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
Can be seen nowjunit
Dependencies:
Reform after modificationproject02
targetpom.xml
below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project02的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<optional>true</optional> <!--隐藏依赖-->
</dependency>
</dependencies>
</project>
After clicking the refresh button, I foundjunit
that the dependency was missing (hidden):
6.4 Exclude dependencies
Exclude dependencies: refers to resources that actively disconnect dependencies. Excluded resources do not need to specify versions (not required)
In other words, you don’t need to use other people’s dependent resources and actively exclude them.
exclusion
Add to to exclude dependencies: groupId
and dependencies, and specify artifactId
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<exclusions>
<!--排除依赖-->
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Demonstration: Modificationproject01
Japaneseproject02
Targetpom.xml
Arrangement below
project01
Target pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project01的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加project02依赖-->
<dependency>
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
project02
ofpom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project02的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
You can see nowjunit
also depends onorg.hamcrest:hamcrest-core:1.3
:
Reform after modificationproject02
targetpom.xml
below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--project02的项目坐标-->
<groupId>com.example</groupId>
<artifactId>project02</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--添加junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<exclusions>
<!--排除依赖-->
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
After clicking the refresh button, I found thatorg.hamcrest:hamcrest-core:1.3
the dependency was missing (excluded):
6.5 Dependency scope
Dependency scope: The dependent jar
package can be used anywhere by default, and its scope can be set through the scope
tag.
Scope of action:
- Valid within the scope of the main program (within the scope of the main folder)
- Valid within the scope of the test program (within the scope of the test folder)
- Whether to participate in packaging (within the scope of the package directive)
scope | main code | test code | Pack | example |
---|---|---|---|---|
compile | √ | √ | √ | log4j |
test | √ | junit | ||
provided | √ | √ | servlet-api | |
runtime | √ | jdbc |
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope> <!--测试范围-->
</dependency>
Note: When resources with dependent scopes are transferred, the scope will be affected.
7. Life cycle and plug-ins
The maven project build command is as follows:
# 编译
mvn compile
# 清理
mvn clean
# 测试
mvn test
# 打包
mvn package
# 安装到本地仓库
mvn install
The corresponding locations of these maven project build commands in Idea are as follows:
7.1 Build life cycle
A typical Maven build life cycle consists of the following sequence of stages:
stage | deal with | describe |
---|---|---|
validate |
Verification project | Verify that the project is correct and all necessary information is available |
compile |
Execute compilation | Source code compilation is completed at this stage |
test |
test | Run the tests using an appropriate unit testing framework (e.g. junit) |
package |
Pack | Create jar/war package as defined in pom.xml mentioned package |
verify |
examine | Check the results of integration tests to ensure quality standards are met |
install |
Install | Install the packaged project to the local warehouse for use by other projects |
deploy |
deploy | Copy the final project package to the remote repository to share with other developers and projects |
Maven has the following three standard life cycles:
clean
: Project cleanup processing, cleanup workdefault
: Project deployment processing, core work, such as compilation, testing, packaging, deployment, etc.site
: Processing of project site document creation, report generation, and site publishing
1.clean life cycle
life cycle stage | describe |
---|---|
pre-clean | Perform some work that needs to be done before cleaning |
clean | Remove all files generated by the previous build |
post-clean | Perform some work that needs to be completed immediately after clean |
2.default (build) life cycle
life cycle stage | describe |
---|---|
validate | Verify that the project is correct and has all the necessary information to complete the project build process |
initialize | Initialize build status, such as setting property values |
generate-sources (generate source code) | Generate any source code included in the compilation phase |
process-sources (process source code) | Process source code, e.g. filter on arbitrary values |
generate-resources (generate resource files) | Generate resource files that will be included in the project package |
process-resources (process resource files) | Copy and process resources to the target directory, best prepared for the packaging phase |
compile | Compile the source code of the project |
process-classes (process class files) | Process files generated by compilation, such as bytecode improvement and optimization of Java class files |
generate-test-sources (generate test source code) | Generate any test source code included in the compilation phase |
process-test-sources (process test source code) | Process test source code, e.g. filter on arbitrary values |
generate-test-resources (generate test resource files) | Create resource files for tests |
process-test-resources (process test resource files) | Copy and process test resources to the target directory |
test-compile (compile test source code) | Compile the test source code to the test target directory |
process-test-classes (process test class files) | Process files generated by test source code compilation |
test | Run tests using a suitable unit testing framework (Juint is one of them) |
prepare-package (prepare to package) | Before actual packaging, perform any necessary operations to prepare for packaging |
package | Package the compiled code into a distributable format file, such as a JAR, WAR or EAR file |
pre-integration-test (before integration test) | Perform necessary actions before executing integration tests, such as setting up the required environment |
integration-test (integration test) | Process and deploy the project into an environment where integration tests can be run |
post-integration-test (after integration test) | Perform necessary actions after execution of integration tests is complete. For example, cleaning up the integration test environment |
verify | Run arbitrary checks to verify that the project package is valid and meets quality standards |
install | Install the project package to the local repository so that the project package can be used as a dependency for other local projects |
deploy | Copy the final project package to the remote repository to share with other developers and projects |
3.site life cycle
life cycle stage | describe |
---|---|
pre-site | Perform some work that needs to be done before generating site documentation |
site | Generate site documentation for the project |
post-site | Perform some work that needs to be done after generating the site documentation and preparing it for deployment |
site-deploy | Deploy the generated site documents to a specific server |
7.2 Plug-ins
Plug-ins: Each life cycle contains a series of phases. These phases are equivalent to the unified interfaces provided by Maven, and the implementation of these phases is completed by Maven plug-ins.
- The plug-in is bound to the stage in the life cycle, and the corresponding plug-in function is executed when the corresponding life cycle is executed.
- By default, maven has preset functions bound to each life cycle.
- Other functions can be customized through plug-ins
When we enter the mvn command, for examplemvn clean
, clean corresponds to the clean phase in the Clean life cycle, but the specific operation of clean is performed bymaven-clean-plugin
To achieve, so the specific implementation of each stage of the Maven life cycle is implemented by the Maven plug-in.
Plug-in type:
type | describe |
---|---|
Build plugins | Executed at build time and configured in the element of pom.xml |
Reporting plugins | Executed during website generation and configured in the element of pom.xml |
List of commonly used plugins:
plug-in | describe |
---|---|
clean | Clean target files and delete target directories after building |
compiler | Compile java source files |
surefile | Run junit unit tests and create test reports |
jar | Build jar files from the current project |
war | Build war file from current project |
javadoc | Generate javadoc for the project |
antrun | Run a collection of ant tasks from any stage of the build process |
Plugin example:
<build>
<plugins>
<!--生成源代码的插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<phase>generate-test-resources</phase> <!--到生成测试源代码阶段,执行该插件-->
</execution>
</executions>
</plugin>
</plugins>
</build>
After clicking refresh, you can find thatPlugins
has an additionalsource
plug-in, which was just addedmaven-source-plugin
plug-in
Test whether the plug-in is valid: Since the generate-test-resources
stage is before the test
stage, that is to say, it is executed to the test
stage. The generate-test-resources
stage must also be executed, so we click the button under Lifecycle
to execute the project to < a i=7> stage. test
test
As you can see, a source code jar package is generated on the left. Open the jar package and you can view the source code of the project.