preface

As we learn more, traditional operations like manual package guide are no longer enough for us, so we need to learn Maven. Google Maven. Emmmm. It’s a little confusing, but you get the idea that Maven is supposed to help build projects.

What is Maven and what does Maven do

1. Problems with traditional development projects without project pain points managed by Maven.

  1. Many modules, modules between the relationship, manual management of the relationship, more tedious.
  2. You need a lot of third-party functionality, you need a lot of JAR files, and you need to manually retrieve individual jars from the network
  3. To manage the jar version, you need mysql.5.1.5.jar to get something you can’t give to a mysql.4.0.jar
  4. Manage dependencies between JAR files. To use A.jar for your project, you need to use classes in B.Jar. ==ps: A. jar requires B. jar. This relationship is called a dependency, or your project needs to use mysql driver. Class a depends on class B ==

2. Use Maven to build project benefits

  1. Maven can manage JAR files
  2. Automatic download jar and his documents, source code
  3. To manage jar dependencies directly, A.jar requires B.jar, which Maven automatically downloads
  4. Manage the versions of jars you need
  5. Help you compile the program, compile Java to class
  6. Help you test your code to see if it is correct.
  7. Help you package files, form JAR files, or war files
  8. Help you deploy the project

Maven download and installation

1.Maven download can be downloaded directly from the Official website of Maven:Maven.apache.org/download.cg…Windows download as follows:2. Select a directory you want to install and decompress it. Ps: The path should not contain Chinese characters.

3. Before using the maven tool, you need to configure environment variables: In the system environment variables, specify a name of M2_HOME and its value is the directory before the maven tool installation directory. Steps: File Explorer –> This computer, right-click properties –> Advanced System Settings4. Then select the environment variable:5. Enter the name: M2_HOME Path: Download maven and decompress the path6. Select the system environment variable Path and click EditEnter %M2_HOME%\bin and click Save8. Then go to CMD and enter MVN -v. If the following information is displayed, the JAVA_HOME directory is not configured

9. Then the following steps are similar to adding M2_HOME above10. Perform the following operations11. Then enter CMD and enter MVN -v. If the following information is displayed, the configuration is successful. PS: Remember to open CMD again after configurationIf the above is displayed, our Maven configuration is complete la la la !!!!

How to use Maven

1. Build your own local repository

Warehouse can be divided into: remote warehouse is mainly divided into three categories, respectively: central warehouse (the most authoritative warehouse), private server (the company’s own private server) and other warehouses. Local repository: a folder on your PC that holds various JARS Remote repository: a repository on the Internet that can be accessed only through the network

  1. The central repository, the most authoritative, is a centralized repository shared by all developers. The central repository is located at repo.maven.apache.org
  2. A mirror image of a central warehouse: a copy of a central warehouse, mirroring important cities on every continent.
  3. Private server, inside the company, used in the LAN, not external use.

Create your own local repository step: modify the local repository directory location (set local repository) :

  1. Modify the maven configuration file, maven installation directory /conf/settings.xml
  2. Modify specify your directory (do not use Chinese directory)

When Maven is used again, the call priority is: Maven first checks if there is a jar in the local repository, then calls the jar directly in the local repository. If there is no jar in the local repository, then calls the jar directly in the company private server. If there is no JAR in the local repository, then calls the jar directly in the central repository image.

PS: When there is no local warehouse, transfer to another warehouse. And then eventually you download a copy to your local repository, so the next time you call this, you don’t have to go anywhere else.

Maven call priority: local repository > Private server > Mirror > central repository

2. Learn more about using Maven conventions than configurations

Maven has conventions for its own unique local directory structure. The conventions for the directory structure of Maven projects are as follows:

Project - / SRC / -- -- -- -- -- - / main # put your main program Java code and configuration files -- -- -- -- -- -- -- -- -- -- / Java # of your package and package Java file -- -- -- -- -- -- -- -- -- -- # / resources to use in your Java program configuration files -- -- -- -- -- - / test # put test program code and documentation (can't) -- -- -- -- -- -- -- -- -- - / Java # test package and package in the Java file -- -- -- -- -- -- -- -- -- - / resources # test Java program to use the configuration file - / pom. The XML # Maven core file (maven project must have it)Copy the code

These are the Maven conventions that require us to do this, but are not mandatory. But as a good baby, we had better follow the requirements of others, after all, it is certainly good for others to design so.

Maven Core Concepts

  1. POM: A file name is pom.xml. POM translates to project Object Model. Maven uses a project as a model. Control the maven build project process and manage JAR dependencies.
  2. Convention directory structure: Directories and file locations for Maven projects are specified.
  3. Coordinates: is a unique string used to represent a resource. This concept is very important. We use Maven mainly to locate the resources we are using through coordinates. == coordinate: unique value, the == that uniquely identifies an item on the Internet
  4. Dependency management: Manage your projects using JAR files
  5. Warehouse management (understanding) : Where your resources are stored
  6. Life cycle: The process by which Maven tools build projects is called the life cycle.
  7. Plugins and Goals (understood) : The tools used to perform Maven builds are plugins
  8. inheritance
  9. The aggregation

The study of pom.xml file

The pom.xml file is one of the most important files for Maven, and it is used to introduce dependencies by adding them. Let’s look at a pom.xml file

<? The 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" > The < modelVersion > 4.0.0 < / modelVersion > <! -- Coordinates of your own project (GAV)--> <! GroupId >com.test. WWW </groupId> <! > <artifactId>test</artifactId> <! --> <version>1.0</version> <! <packaging>jar</packaging> <! --properties: Set properties --> <properties> <! - use utf-8 maven build project, avoid Chinese garbled - > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > <! Source >1.8</maven.compiler.source> <! Target >1.8</maven.compiler.target> </properties> <! --> <dependencies> <! - add what had to rely on to come here to add what coordinates, coordinates to the https://mvnrepository.com/ search is ok - > <! <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project>Copy the code

Use example: Introduce unit test dependencies

Use steps:

  1. Add dependencies. Add unit test dependencies in POM.xml
<! <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>Copy the code
  1. In the SRC /test/ Java directory of the Maven project, create the test program. 1. The name of the Test class is Test + the name of the class you want to Test 2. The method name of the Test is: Test + method name

    For example, if you want to TestHelloMaven, create the TestHelloMaven test class

@test public void testAdd(){HelloMaven add ();Copy the code
TestAdd is called a test method and its definition rule is 1. The method is public and must be 2. Method has no return value, mandatory 3. Method name is custom, Test + method name 4 is recommended. Add @test above the methodCopy the code
  1. MVN compile compile Java files from main/ Java/as class files, and copy all files from main/resources to target/classes

3. Set Maven in idea

Maven is built into IDEA. It is generally not used because it is not convenient to modify maven Settings. Using maven with your own installation, you need to override the default Settings in IDEA. Let IDEA specify maven installation location and other information

Configuration entry

File –> Settings –>Build, Excution,Deployment–>Build Tools –>Maven Maven Home directory: Maven installation directory User Settings File: maven installation directory conf/setting. XML configuration File Local Repository: Local Repository directory location

  1. Build Tools–Maven–Runner VM Options: archetypeCatalog=internal JRE: JDK for your project
  2. ArchetypeCatalog =internal, when creating maven project, you can download the template file online, it is relatively large, use -darchetypecatalog =internal, don’t need to download, create Maven project quickly.
  3. Configure the Settings for the New Project, file–other Settings –Settings for New Project
  4. Create project options using templates

1) Maven-archetype-Quickstart: common Java project 2) Maven-archetype-webApp: Web project

4. Learning depends on scope

Maven project build.

A build is process-oriented, that is, the steps that complete the compilation, testing, running, packaging, deployment, and so on of the project code. Maven supports builds that include:

1. Clean up, delete the things compiled by the previous project, and prepare for the new compiled code. Maven can compile hundreds of java-class files into classes at the same time. Javac is different. Javac compiles one file at a time. 2. Test. Maven performs test code to verify that your functionality is correct. In bulk, Maven executes multiple tests simultaneously, testing many functions at the same time. 3. Report, generate test results file, test passed or not. 4. Pack your project's class files, configuration files, and other resources into a compressed file. The zip file is the result file of the project. Usually for Java programs, the zip file has a JAR extension. ==ps: For web applications, the compressed file extension is. War == 5. Install, install the jar, war file generated in 5 into the local repository 6. Deploy, install the program can be executed.Copy the code

Dependent on scope, represented by scope.

The scope values are compile, test, and provided, and the default is compile scope: indicates the scope of dependencies that are used during those phases of a Maven build project. Maven build project compile, test, package, install, deploy process (stage) specific function.

5. Common operations of Maven

  1. Maven Properties Set common maven properties

  2. Maven global variable custom properties, 1. Declare variables via custom tags (tag name is the variable name) 2. Elsewhere in the POM.xml file, use ${tag name} to use the value of the variable

    ${var name} ${var name} ${var name} ${var name}

  3. Resource plugin

<build> <resources> <resource> <directory>src/main/java</directory><! <includes><! Properties </include> **/. Properties </include> <include>**/. XML </include> </includes> <! *. Property is used to filter --> <filtering>false</filtering> </resource> </resources> </build>Copy the code

SQL > create target/classes directory with XML files and properties files in Java