1. What is Maven?

Maven is a project management tool that can simplify the project configuration, unify the project structure, and make the developer's work much easier! The main reason for using Maven is that it can help you download and manage dependencies (JARs) that you need in your project.

1. Download and install Maven

The official download address: http://maven.apache.org/download.cgi maven is a green software, after decompression can be used. It is recommended that the installation path should not contain Chinese characters and Spaces

2. Configure the local warehouse

2.1. What is a local repository A local repository is a directory on your local hard drive that is used to store all the JARs and plug-ins that Maven downloads for you. If not specified, the default local repository is (Windows) : c:/users/{current user}/.m2/repository 2.2. /conf/settings.xml. In line 55 of the Settings file, add the following: < LocalRepository >d:/ JavadDevelop/LocalRepo </ LocalRepository > After modification, all JARs and plug-ins downloaded via Maven will be stored in this location:  d:/javaDevelop/localRepo

3. Configure the remote warehouse

If the remote repository is not configured, it is connected to the central repository by default. That is, Maven will download the JAR from the central repository and save it to the local repository. But because the central repository is for global users, downloading JAR packages is slow! Remote repositories (mirrored repositories, private servers) : They are generally built and maintained by the company (or individual). If the remote repository is configured, Maven will download the required JAR packages from the remote repository and save them to the local repository. 3.2. How to configure the remote warehouse (1) If there is a network: it can not be configured, but it can be connected to the central warehouse by default, or it can be configured to connect to the warehouse of Ali Cloud, or it can be connected to your company's own remote warehouse. Note: If you are connected to Aliyun warehouse, you can not use the mobile hotspot network! }/conf/settings.xml. In the Settings file, add the <mirror> tag to the Settings file to configure the remote repository. Aliyun's remote warehouse configuration:  <mirror> <id>nexus-aliyun</id> <name>Nexus aliyun</name> <mirrorOf>central</mirrorOf> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror>

4. Configure the JDK version

You can configure the JDK version in {maven install directory}/conf/settings.xml. After you configure the JDK version in {maven install directory}/conf/settings.xml. When we create a Maven project later, the JDK version in the project will be the version we specify. Instead of JDK1.5. Add a <profile> tag inside the <profiles> tag of the {maven installation directory}/conf/settings. XML file. As follows: The < profile > < id > development < / id > < activation > < JDK > 1.8 < / JDK > < activeByDefault > true < / activeByDefault > < / activation > Piler < properties > < maven.com. Source > 1.8 < / maven.com piler source > < maven.com piler. Target > 1.8 < / maven.com piler. Target > The < maven.compiler.com pilerVersion > 1.8 < / maven.compiler.com pilerVersion > < / properties > < / profile >

5. Integrate Maven and Eclipse

If you have integrated Maven with Eclipse, modify the settings.xml file, and restart Eclipse if you want the changed configuration to take effect immediately.

There are two ways to build a Java project with Maven:

Create a simple Java project from Maven. Create a simple Web project from Maven. Create a simple Web project from Maven. Create a template project/project from Maven (1) Create a Java project from a template (2) Create a Web project from a template

1. Create a Java project with Maven: cgb-maven-java01

Create a Web project with Maven: cgb-maven-web01

Note: Web projects created through Maven are missing the WEB-INF directory by default, and also missing the WEB-XML file. The solution is: In the [Package Explorer] view window, on the Web project --> right-click --> select :JavaEE Tools--> select :Generate Deployment... , the Web-INF directory and the Web.xml file will be generated under the WebApp directory

3. The Maven project directory structure

CGB MAVEN - WEB01 (MAVEN project name) | - SRC/main/Java directory (source code) - to hold the main program/project Java source file | - SRC/main/resources directory (source code) - To hold the main program need configuration files (. XML /. Propertes /. Yml, etc.) | - SRC/test/Java directory (source code) - to hold test Java source file | - SRC/test/resources directory (source code) - To hold the test configuration file | - SRC/main/webapp - the root directory of the Web application, equivalent to the WebContent directory Can deposit in the future Web resource files (HTML/CSS/js/JSP/pictures etc.) | -- - SRC/main/webapp/WEB - INF/WEB. XML -- the core of the WEB application configuration file | -- target/classes / -- source directory resource file after compiling, will be output to the classes directory | -- pom. XML - Maven project configuration file, if you need to import JARs, plug-ins, can be imported in this file

How do I import an existing Maven project

Note: After importing the project, there is a cross (X) or an exclamation mark (!) on the project. , because Maven doesn't download all the environments/dependencies. The solution can be solved through the following two steps: 1) Open Yonghe project, find the pom.xml file in it, type a blank line in the file and save it, let Maven scan the POM file again, and download the required dependencies according to the configuration; 2) Open Yonghe project, find the pom.xml file and save it. --> maven --> Update Project --> Update Project --> Update Project In the pop-up window, check []Force Update... That is, check the Forced Update box, if not resolved, then look at Step 3!

How to introduce JARs in a Maven project

Instead of copying the JAR package into the current project, add the JAR coordinate to the pom. XML file. For example, to add the MySQL driver package, just add a tag <dependencies> in the root tag of the pom file (project). Add the coordinates of the MySQL driver package to the <dependencies> tag, as shown below: < the dependency > < groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 5.1.32 < / version > </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency>

2. Where are the JAR packages that were introduced into the Maven project?

JARs imported into a Maven project are stored in the local repository in a specified location, which is the local repository location + the current dependency coordinates. (As mentioned earlier, the local repository is the directory where all the JARs and plug-ins downloaded by Maven are stored.)

3. Where do the JAR packages in the local repository come from?

If the remote repository is not configured, the jar in the local repository is downloaded by Maven from the central repository for us. If the remote warehouse of Aliyun is configured, the jar package in the local warehouse is downloaded from Aliyun warehouse by Maven. It's a directory on your local hard drive that stores all the JARs and plugins that Maven has downloaded for you. When you introduce a dependency in your project, you go to the local repository and look for it based on the coordinates. If there is a dependency in the local repository, you import it directly into your project. If it is not available in the local repository, connect to the remote repository (if configured), or to the central repository. 2) Remote repository: Also known as mirror repository or private server, it is built by a company or individual. If the remote repository is configured in Maven, when there is no jar in the local repository, it will connect to the remote repository for downloading. If there is no remote warehouse, connect to the central warehouse to download. 3) Central Warehouse: It is built and maintained by Maven team, which includes almost all JAR packages in the world. The users are global users, so it will be slow to connect to the central warehouse.

4. How to add dependencies to POM files? 4.1. How to add dependencies:

If the added dependency package exists in the local repository (for example, if the dependency package was previously introduced in another project), Add dependency: Open the POM file, right-click on the POM file --> Maven --> Add dependency, and enter the dependency package in the pop-up window. If the local repository does not have this JAR package, it will not be searched! If you have this JAR in your Local Repository, you can't find it. Open Maven Repositories window, find Local Repository, right-click Rebuild Index, ReBuild Index, and then search for it.

4.1. The second way to add dependencies:

If the added dependency package is not in the local repository, you can search the Internet for the dependency coordinates and copy the coordinates to the POM file http://mvnrepository.com as

1. Maven’s native libraries support replication

If you cannot download the required dependencies through Maven due to network problems, you can copy the local libraries already downloaded from other computers to our local library.

2. If the project directory structure created by Maven is incomplete, or the JAR package is introduced in the later stage, the following methods can be used to solve the problem:

1) Make sure that the current network environment can connect to the configured remote warehouse, then proceed to Step 2. (If you are not in the Darnay classroom, you cannot connect to the remote warehouse of Darnay; 2) Type a blank line in the POM file of the project, and then save the file. (The purpose is to let Maven detect that the POM file has changed, and then according to the configuration in the POM file, look for the corresponding JAR in the local repository, if there is no corresponding JAR, Maven will download again.) -- Maven --> Update Project -- Maven --> Update Project In the pop-up window, check the box below "[]Force Update..." In this case, Maven will also examine the POM file and download any dependencies that were not introduced. 4) If the above operations still do not work, to the local warehouse directory, the local warehouse in all the directories are deleted, delete, because Eclipse is using the resource files in the local warehouse, so it will prevent the deletion, Eclipse will be closed, and then remove all the directories in the local warehouse, restart Eclipse. 5) After starting Eclipse, do steps (2) and (3) above again!