1. What is Maven
Maven is a project construction tool developed in Java. It can automate compilation, testing, publishing and documents during the project construction process, greatly reducing the deployment burden of programmers.
2. Install Maven
Installing maven is very simple. Visit the official Maven page to download: http://maven.apache.org/download.cgi
After downloading, configure the M2_HOME environment variable, and then run mvn --version in the terminal. When you see the correct output prompt, Maven is installed.
3. Basic concepts of Maven
The core idea of Maven is POM, that is, Project Object Model. POM files describe the resources (source code, dependencies, tests, etc.) used by a Maven project in the form of an XML file. The following figure describes the structure of the POM file and how Maven calls the POM file.
When a Maven command is executed, a pom file will be passed in, and Maven will be executed on the resource described in the pom
POM file:
<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> <!-- The Basics --> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> <packaging>...</packaging> <dependencies>...</dependencies> <parent>...</parent> <dependencyManagement>...</dependencyManagement> <modules>...</modules> <properties>...</properties> <!-- Build Settings --> <build>...</build> <reporting>...</reporting> <!-- More Project Information --> <name>...</name> <description>...</description> <url>...</url> <inceptionYear>...</inceptionYear> <licenses>...</licenses> <organization>...</organization> <developers>...</developers> <contributors>...</contributors> <!-- Environment Settings --> <issueManagement>...</issueManagement> <ciManagement>...</ciManagement> <mailingLists>...</mailingLists> <scm>...</scm> <prerequisites>...</prerequisites>...</repositories> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories> <distributionManagement>...</distributionManagement> <profiles>...</profiles></project>
modelVersion is the POM model version, 4.0.0 supports Maven2 and 3
Maven Coordinates (Maven coordinates)
(1) groupId: It is the unique ID of an organization or project. In most cases, it will use the root name of the project's java package as the groupID, such as com.pastqing
(2) artifactId: It is the name of the project being built, for example, a payment system artifactId is web-pay. artifactId is the subdirectory name under the groupId directory in the Maven repository
(3) version: as the name implies the version number of the project issuance
The above three items are part of the name of the build result. After the project is built, a jar package will be generated. The path located in the Maven repository is MAVEN_REPO/com/pastqing/web-pay/1.0/web-pay-1.0.jar
4. A Hello World java project built with Maven
Build a project using the Maven plugin archetype:
Open the working directory in the terminal and run the command:
mvn archetype:generate
, The first time it runs is slower because the first time you need to download the related project prototype from the Maven central repository. After downloading, the project model will be selected, and groupId, artifactId, version, etc. will be entered. There will be a success prompt after the construction is completed.
Packaging the project: Switch to the project root directory and run mvn package. After the package is successful, the project will generate a target folder with generated jar files and class files.
Run the jar file:
java -cp target/helloWorld-1.0-SNAPSHOT.jar com.pastqing.App
At this point, a simplest java project built by Maven has been completed.
5. A java-web project built with Maven
Generating a web project is basically similar to generating a Java project, but the model used is different. I will not explain it here. Let's talk about how to use Tomcat or jetty plug-in to run a web project. Here we take tomcat as an example.
Maven Tomcat plug-in installation: http://tomcat.apache.org/maven-plugin-2.2/
We add the following plug-in information to the POM file in the web project
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.2</version></plugin><plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version></plugin>
In this way, we integrate the Maven plug-in of tomcat, and we only need one command to deploy and start the service. The command is as follows:
mvn tomcat:run
(tomcat6)
mvn tomcat7:run
(tomcat7)
Automatic deployment to external tomcat using tomcat Maven plugin
The above automatic deployment will use Tomcat embedded in Maven. Next, we modify the POM file to allow the project to be deployed to external Tomcat.
Modify the project POM file and add server configuration information
<plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration> <url>http://localhost:8080/manager/text</url> <server>tomcat7</server> <username>admin</username> <password>admin</password></configuration></plugin>
The values of server, username, and password correspond one by one to server, username, and password in setting.xml in %Maven_HOME.
Start external tomcat and run the command mvn tomcat7:redeploy
6. How to build a multi-module project with Maven
1. Parent POM
All Maven pom files are inherited from a parent POM. If no parent pom is specified, the pom file is inherited from the root POM. The integration relationship of pom files is shown in the figure below:
You can make one pom file explicitly inherit another pom file. In this way, the settings of all child pom files can be modified by modifying the settings of the public parent pom file. Here we use the parent tag to define the parent pom. Let's build a multi-module Mavne project
2. Build the project directory structure
We create a maven-web project called EducationCloud. Next, we create several folders in the project directory to divide our modules. They are Education-parent (parent module), Education-core (business), Education-entity (entity), Education-web (web service)
The directory division can be divided according to needs, my division is as above
3. Modify the pom file
We use module tags to divide modules. Open the pom file in the root directory and add the moudle tag.
<?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> <groupId>com.EducationCloud</groupId> <artifactId>EducationCloud</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>Education-parent</module> <module>Education-core</module> <module>Education-entity</module> <module>Education-web</module> </modules> <name>EducationCloud</name></project>
Here we talk about the packaging tag: the most common one is to package it as a jar, war. Any Maven project needs to define the packaged element in the pom file. If the element is not declared, it is packaged as a jar by default. If the definition value is war, it is packaged as a war package. If the value is pom, no package is generated (usually used in the parent module).
Add parent tags to each module:
The parent tag is used to define the coordinate position of the parent POM, and is defined as follows:
<parent> <groupId>com.EducationCloud</groupId> <artifactId>EducationCloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath></relativePath></parent>
groupId, aritfactId, version are elements that represent the location information of the parent pom. relativePath is the relative path that represents the location of the parent pom. The default is ../pom.xml. It should be noted here that groupId, aritfactId, version must correspond to the parent pom file.
Use dependencyManagement to manage all dependencies in the project. In order to manage dependencies uniformly, we use dependencyManagement in the pom file of the parent project module to declare all dependencies in the project. In this way, when referring to dependencies in other subprojects, the version number can be omitted, and it is also easy to modify.
Write the pom file of the parent project module **educationCloud-parent, add it as follows:
Use pluginManagement to manage the Maven plugin in the project. In order to uniformly manage the Maven plugin in the project, such as Maven compilation plugin, packaging plugin, etc. and its configuration information, add pluginManagement to the parent project pom file for management. In this way, the plugins referenced in all submodules will be processed uniformly, and the following is added:
<pluginManagement> <plugins> <!-- Complile plugin configuration--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-complier-plugin</artifactId> <version>3.3</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> <plugin> </plugins></pluginManagement>
Above we added the configuration of the maven compilation plug-in, compiled with jdk1.7. After saving, we can see the corresponding modifications in the Effective pom of each submodule.
Use properties tags to define constants:
We define the version numbers of each dependency as different constants, so that it is convenient for future modifications:
<properties> <jdkVersion>1.7</jdkVersion> <servletVersion>3.1.0</servletVersion> <mysqlVersion>5.1.34</mysqlVersion> <junitVersion>4.12</junitVersion> <defaultEncoding>UTF-8</defaultEncoding></properties>
When citing, just use the form of ${jdkVersion}.
Note, all of the above operations can be operated in the IDE, making it more convenient and simple.