Maven is a one-stop service for project construction, compilation, testing, packaging, and installation based on the Project Object Model (POM). It is a management tool for the current mainstream project development, and I will learn it in detail today.
1. Maven download and environment construction
Maven download: http://maven.apache.org/download
Download the compressed package and extract it to the desired directory on the D drive.
Configure environment variables:
M2_HOME:D:\apache-maven-3.1.1\
Path add: ;%M2_HOME%\bin;
Check whether maven is installed successfully, open cmd: mvn -v
If you can see the maven version information and jdk information, it means the installation is successful.
2. The skeleton structure of the directory agreed by Maven
src
-main
-java
-package
-test
-java
-package
-resources
3. Common build commands
mvn -v View version information
compile compile After successful compilation, the target directory will be generated
test test After the test is successful, a test report will also be generated in the target directory
After the package is packaged successfully, a jar package will also be generated in the target directory
clean clean target
install installs the jar package to the local repository
The above build command needs to be run on the jdk, and the jre will report that tools.jar cannot be found
Fourth, automatically create a directory skeleton
When not integrated into eclipse, you can use the command to create a project directory:
1.archetype:generate Follow the prompts to create step by step
2.archetype:generate -DgroupId=organization name (usually the company URL reversed + project name)
-DartifactId=project name-module name
-Dversion=version number (SNAPSHOT is the snapshot version, release is the release version)
-Dpackage=package name where the code is located
5. Coordinates and warehouses
Coordinates: Each maven project is uniquely positioned by coordinates, which have three elements:
groupId
artifaceId
version
Through these three elements, you can accurately find a maven project
Repository: All jar packages of projects managed by maven are in the repository
The warehouse is divided into:
local warehouse
Remote warehouse (also called central warehouse)
mirror repository
The jar package we use will first be found in the local warehouse, and if it is not found, it will be downloaded from the remote warehouse to the local warehouse.
1. Local warehouse
Generally, after we install maven, it will be in the user path of the C drive: C:\Users\Yuwl\.m2 has this m2 folder, which is our local warehouse, and there is a folder under it to store all jar packages repository, there is also a settings.xml configuration file, which can configure the path of our local repository:
<localRepository>D:/.m2/repository</localRepository>
For example, mine is stored in the D drive, because the C drive reinstalls the system and it is gone.
2. Remote warehouse
It is also called the central warehouse. It is the maven warehouse. If the jar package used is not found in the local warehouse, it will be downloaded from the remote warehouse and used in the local warehouse.
In the maven installation directory, mine is D:\apache-maven-3.1.1\lib, the next jar package: maven-model-builder-3.1.1.jar, open it in decompression mode and you can see that there is a pom- 4.0.0.xml file, which has the url of the central warehouse:
<repositories> <repository> <id>central</id> <name>Central Repository</name> <url>http://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
http://repo.maven.apache.org/maven2 This is the central repository of maven
If snapshots is false, it means that the downloaded version is a non-snapshot version, and the snapshot version is generally the development version.
3. Mirror warehouse
The so-called mirror provides the same functions as the central warehouse, but sometimes the central warehouse cannot be accessed, because after all, it is foreign, and the mirror warehouse is provided by many domestic manufacturers, which is also very convenient for us to use. The specific usage is as follows:
Modify the settings.xml file under m2:
Add the following configuration under the mirrors node
<mirror> <id>maven.net.cn</id> <mirrorOf>central</mirrorOf> <name>central mirror in china</name> <url>http://maven.oschina.net/content/groups/public/</url> </mirror>
mirrorOf indicates the central warehouse to be replaced. With this configuration, the mirror warehouse will replace our central warehouse.
6. Install the maven plugin in eclipse and create a maven project
Generally, the maven plug-in is integrated in the higher version of eclipse, and there is no need to install it separately. If not, the installation is also very simple, so I won't say more here.
Create a java project with maven:
new->maven project->select maven-artifact-qickstart->enter groupId,artifactId,package
Create a web project with maven:
new->maven project->select maven-artifact-webapp->enter groupId,artifactId,package
We can see that the project directory structure that maven generates for us starts with:
src
-main
-java
-package
-test
-java
-package
-resources
If the webapp project does not generate such a directory, you can manually create it yourself
You can also see the pom.xml in the project root directory, which is the configuration file that maven uses to build the project
Seven, pom.xml parsing
<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> <groupId>com.yuwl.maven</groupId> <artifactId>maven-demo01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>maven-demo01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
<project></project> project start and end tags
<modelVersion> maven version, the fixed value is 4.0.0
<groupId> organization name: usually the company URL reversed + project name
<artifaceId> module name: project name + module name
<verison> version: 0.0.1-SNAPSHOT is the snapshot version
<packing> packaging method, there are jar, war, pom, etc., the default is jar
<name><url> has little effect
<properties> is configuration information
The following version 3.8.1 of junit can be configured as:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.verison>3.8.1</junit.version> </properties>
It can be written like this:
<version>${junit.verison}</version>
In this way, the version numbers of all jar packages can be managed uniformly, and it is also convenient to change
<dependencies> is the project dependency, that is, the referenced external jar package
Eight, Maven life cycle and plug-ins
The life cycle generally includes: cleanup, compilation, testing, packaging, installation
即clean compile test package install
We can clean to any stage when we use it, such as
clean compile 包含test
clean package 包含test,compile
clean install 包含test,compile,package
Maven plugin:
Managed by <buid><plugins> tags in pom.xml
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>package</phase><!-- phase to run --> <goals> <goal>jar-no-fork</goal><!-- 目标 --> </goals> </execution> </executions> </plugin> </plugins> </build>
The above is a plug-in that generates source code packaging. Maven provides many rich plug-ins, and many of its functions are actually implemented by plug-ins.
Maven's own plugin: http://maven.apache.org/plugins/index.html
There are also many third-party plug-ins, such as jetty, tomcat, etc.
9. Dependency transfer
That is, B depends on AC on B, then C depends on A
If you don't want C to depend on A, you can depend on B, and you can do it, as follows:
<dependency> <groupId>com.yuwl.maven</groupId> <artifactId>maven-demo02</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>com.yuwl.maven</groupId> <artifactId>maven-demo01</artifactId><!-- exclude the passed maven-demo01 --> </exclusion> </exclusions> </dependency>
This pom is demo03, depends on demo02, and demo02 depends on demo01
Add <exclusions> to exclude the dependencies of demo01
10. Dependency conflict
Two principles:
1. Short circuit priority
A->B->C->X.jar
A->D->X.jar (preferred)
2. Declare first, use first, in order
11. Aggregation and Inheritance
1. Aggregate use <modules> one-time compilation, testing, packaging, installation
<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> <groupId>com.yuwl.maven</groupId> <artifactId>maven-aggreation</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>maven-aggreation</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Aggregate unified compilation, testing, packaging, installation--> <modules> <module>../maven-demo01</module> <module>../maven-demo02</module> <module>../maven-demo03</module> </modules> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Note that when using aggregation, the packing of the current pom must be the pom.
2. Inheritance, similar to java inheritance, has a parent-child relationship
First build a parent's pom
<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> <groupId>com.yuwl.maven</groupId> <artifactId>maven-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>maven-parent</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>3.8.1</junit.version> </properties> <!-- will not work in this project--> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>
The junit dependency is defined in this maven-parent, but this dependency will not be used in this project, it will be used in inherited sub-projects
<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> <artifactId>maven-demo01</artifactId> <packaging>jar</packaging> <name>maven-demo01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>com.yuwl.maven</groupId> <artifactId>maven-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> </project>
Using <parent> maven-demo01 inherits from maven-parent and can inherit from junit dependencies, so we can put the common jar package into parent pom for management.
12. Maven creates a web project and publishes it to jeety and tomcat through plug-ins
Create a new maven web project maven-webdemo, 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.yuwl.maven</groupId> <artifactId>maven-webdemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>maven-webdemo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId><!-- 解决index.jsp报错 --> <version>3.0.1</version> <scope>provided</scope><!-- only run when compiling and testing --> </dependency> </dependencies> <build> <finalName>maven-webdemo</finalName> <plugins> <plugin> <!-- <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.16.v20140903</version> --> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <!-- Use jetty:run to run the service after the package is successful --> <phase>package</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Use jetty:run to run, or bind to the package command to run. The tomcat plugin also works.
Summarize
Only the most basic usage of Maven is learned here. Although it is very basic, it is very important, which brings a lot of convenience to daily development projects, and will be further supplemented in the future.