1. Configuration of maven environment
First download the maven resource and visit the official website https://maven.apache.org/ Then click Download on the left and select the required resource in the File column.
Direct download link http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.zip,
After the download is successful, decompress it directly, and then configure the environment.
Variable name: m2_home
Variable value: D:\study\apache-maven-3.5.3-bin\apache-maven-3.5.3 (you need to configure it according to the address you downloaded and decompressed)
Then add m2_home to the Path variable value: %m2_home%\bin;
Then enter mvn -v in the cmd command box to run the test, how to display maven related information is successfully configured.
Set the location of the resource download library: modify the path in the localRepository in the settings.xml file, for example <localRepository>D:\study\mven_jar_download</localRepository>
And put the settings.xml file in the D:\study\mven_jar_download file
Set the jdk version (settings.xml in the downloaded resource file): Modify the profile tag to the jdk version you use (it doesn't matter if you don't modify it, but the default version number of the JRE System Library when you create the project is settings. Default 1.4 version in xml)
<profile>
<id>jdk-1.8</id>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
The url in the mirrors tag can be set to a domestic mirror library or aliyun's mirror library
aliyun mirror library: http://maven.aliyun.com/nexus/content/groups/public/
If not set, it will be the default address http://my.repository.com/repo/path
2. Eclipse configuration (eclipse download address: https://www.eclipse.org/downloads/)
Click helo→install new software Click Add name and fill in maven location Fill in http://m2eclipse.sonatype.org/sites/m2e
Then click next and follow the prompts to complete the installation. (Official download of the latest version of eclipse comes with the maven plugin)
Start to configure the relevant settings of maven, click Window above eclipse, then select preferences→maven→installations, click Add, select the decompression address of maven, and click Finish.
Click User Settings. Under User Setting, select settings.xml in the resource package you want to download and click ok.
3. Create a maven web project
maven web project directory structure
src
main
java
resources
test
java
resources
Right-click new→other→then select Maven Project, click next→next→select org.apache.maven.archetypes maven-archetype-webapp 1.0 and then click next, fill in Group Id (company URL fill in backwards), Artifact Id (project name) , package (package name), click Finish.
Then complete the relevant documents
The general directory structure is as follows:
projectName
src/main/java
src/main/resources
src/test/java
src/test/resources
JRE System Library[JavaSE-1.8]
Maven Dependencies
Referenced Libraries
src
main
webapp
WEB-INF
web.xml
target
pom.xml
After the project is just created, index.jsp will have a red x number, that is because the corresponding jar package is not introduced, and then add it in pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<!-- 只在编译时和测试时运行 -->
<scope>provided</scope>
</dependency>
After the project is created, test whether the project can be started normally. There are two ways:
(1)jetty
add in pom.xml
<!-- Use jetty service to test whether the project is created successfully -->
<groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId>
<version>8.1.16.v20140903</version>
<executions>
<execution>
<!-- use jetty:run to run the jetty service after successful packaging -->
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</ execution>
</executions>
Then right-click on the project Run As and select Maven build..... Enter clean package in Goals to see if the console reports an error, then visit localhost:8080 for access. If the page displays Hello World! Congratulations, you have succeeded.
(2) tomcat (tomcat and jetty can only make one valid)
Configure in pom.xml
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version> Then right-click on the project Run As and select Maven build..... Enter clean package in Goals to see if the console reports an error, then visit localhost:8080/projectName to visit, if the page displays Hello World! Congratulations, you have succeeded.
If you encounter
[ERROR] /D:/study/project/bsdapp/src/main/java/com/mobisoft/meapbase/gateway/bi/ResDealer.java:[22,13] Unmappable character encoding GBK
Solution:
add in pom.xml file
<!-- Specify the character encoding used when compiling the source code, the GBK encoding used by default when maven compiles, set the character encoding through the project.build.sourceEncoding property, and tell maven that the project uses UTF-8 to compile -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Just
4, maven common commands
mvn -v View maven version information
compile compile the project
test test
package packaging
clean delete target
install installs the jar package into the local repository
5. Maven's Dependency Scope, Dependency Transit, Dependency Conflict, Aggregation and Inheritance
maven dependency scope
(1) .complie default, all scopes
(2). provided compile and test
(3). runtime test and run
(4). test only test Junit
(5). system compile and test, poor portability
(6). import Import scope, only in utility dependencyManagement means importing dependencies from other poms
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!-- Scope of dependencies--><scope>test</scope>
</dependency>
</dependencies>
mave dependency transfer
<!-- hyue项目依赖于a项目 -->
<dependency>
<groupId>com.hy</groupId>
<artifactId>a</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
When executing the compile command, the dependent projects need to be packaged first (package a project first, that is, execute the package command, and install project a, that is, execute the install command)
Maven exclusion dependency (the hyue project depends on a, and a depends on b, but if this is the case, the hyue project depends on a and b, but b only wants to listen to a, so the exclusion of dependencies will work.)
<!-- hyue项目依赖于a项目 -->
<dependency>
<groupId>com.hy</groupId>
<artifactId>a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 排除依赖 -->
<exclusions>
<exclusion>
<groupId>com.hu</groupId>
<artifactId>b</artifactId>
</exclusion>
</exclusions>
</dependency>
mave's principle of dependency conflict
Dependency conflict: The same build that depends on
different
versions
1. Short-circuit priority If AB depends on a different version of component X (jar), and A->B->C, for C that depends on AB, which version of the component will it depend on at this time? For example, in the following cases, it will be resolved first Second, C will inherit B's dependency version 2.0
A->B->CX-2.0(jar)
A->BX-1.0(jar)
needs to be installed in the local warehouse to take effect when A->BX-1.0(jar) is tested.
2. First declaration first priority
If the path lengths are the same, whoever declares in the pom first, who parses first (parse order from top to bottom),
A->B->X-1.0(jar)
A->C-> X-2.0 (jar)
Aggregation and inheritance in maven
polymerization
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- project aggregation, set packaging to pom in the aggregated project <packaging>pom</packaging >-->
<modules>
<module>../a</module>
<module>../spring-mvc-study</module>
</modules>
inherit
<!-- The jar package in the parent class, remember to change the jar in <packaging>jar</packaging> to pom <packaging>pom</packaging>-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId> junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!-- Scope of dependencies-->
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Content in subclasses
<properties>
<javax.servlet-api.version>4.0.0</javax.servlet-api.version>
</properties>
<!-- The parent needs to fill in the relevant information in the parent in the subclass, and The junit version number and dependency range in the subclass can be deleted -->
<parent>
<groupId>com.hy</groupId>
<artifactId>hyue</artifactId>
<version>0.0.1-SNAPSHOT</version>
</ parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- <version>3.8.1</version>
<scope>test</scope> -->
< /dependency>
</dependencies>