Java--Maven, POM, maven install, maven core, command

1. Maven

Maven is an automated build tool maintained by the Apache Software Foundation. The original meaning of the word Maven is: expert, expert. Based on the project object model ( Project Object Model , abbreviation: POM ) concept, Maven can manage a project's construction, reporting and documentation steps with a central piece of information.

Maven is a project management tool that can build and manage dependencies for Java projects

Maven official website address maven official website

maven central warehouse maven central warehouse

1. The role of Maven

(1) Maven can integrate references between multiple project modules, and can also split projects according to business and functions

(2) Maven can automatically download jar files and source code; manage jar files and jar versions (automatic version compatibility)

(3) Manage other jar packages that the jar package depends on, automatically download and import the project

(4) Compile the program, compile java into class

(5) Test code

(6) Package files to form a jar file (Java project), or a war file (web project)

(7) Deployment project

2. Build the project

(1) Cleanup: Delete previous compilation results to prepare for recompilation

(2) Compilation: Compile the Java source program (.java file) into a bytecode file (.class file)

(3) Test: Test the key points in the project to ensure the correctness of the key points in the iterative development process of the project

(4) Report: Record and display the test results in a standard format after each test

(5) Packaging: Package a project containing many files into a compressed file for installation or deployment. (The Java project corresponds to the jar package, and the Web project corresponds to the war package)

(6) Installation: In the Maven environment, it specifically refers to installing the packaged result -- jar package or war package into the local warehouse

(7) Deployment: Deploy the packaged result to the remote warehouse or deploy the war package to the server to run

Two, maven installation environment

1. Download the old version from the maven official website, you can view the document, the corresponding JDK version

maven old version download

2. Put the downloaded maven compressed package under the specified path and decompress it (do not include Chinese in the path)

There are mainly two directories as follows

bin : the execution program, mainly mvn.cmd
conf : the configuration file settings.xml of the maven tool itself

3. Configure maven environment variables

(1) In the environment variables of the system, create a new system variable name of M2_HOME, and the specified value is the maven tool installation directory, the directory before bin

如:M2_HOME=D:\JDK\apache-maven-3.6.3

 (2) Then add M2_HOME to the path, and add %M2_HOME%\bin before all paths;

In the new command line, execute the mvn -v command

mvn -v 

The first run may report an error as follows:

The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

(3) Create a JAVA_HOME system variable here 

Note: JAVA_HOME needs to be configured to specify the jdk path

If java.exe is used when maven is running, if it is not configured, maven cannot be called, but if it is configured, it can be called

At this time, close the DOS window, reopen it, and run the mvn -v command again, the following content appears, maven is installed and configured correctly

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: D:\JDK\apache-maven-3.6.3\bin\..
Java version: 1.8.0_291, vendor: Oracle Corporation, runtime: D:\JDK\java
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

4. Execute the mvn compile command

When the mvn compile command is executed for the first time, all java files in the src/main directory will be compiled

(1) Execute the download operation

The operations performed by the maven tool require many plug-ins (java classes -- jar files) to complete

(2) Download the jar package (plug-in)

(3) The storage location of the downloaded jar package

Default repository (native repository):

C:\Users\ (user name for logging in to the operating system) Administrator\.m2\repository

Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-parameter-documenter-2.0.9.pom

https://repo.maven.apache.org : the address of the central warehouse

Execute mvn compile, the result is that the target directory (result directory) is generated under the root directory of the project, the java program compiled by maven, and the final class files are placed in the target directory

5. Set maven installation home directory, setting.xml file and local warehouse location

(1) Set the location of the directory where resources are stored on this machine (set the local warehouse)

Default warehouse (local warehouse) storage location:

C:\Users\ (user name for logging in to the operating system) Administrator\.m2\repository

It can be copied to the set target path, such as copying it to the following path

D:\XXXX\maven-work\repository (personal computer storage location)

(2) Modify the configuration file of maven, maven installation directory \conf\settings.xml (such as: D:\XXXX\apache-maven-3.6.3\conf\settings.xml (personal computer storage location)), first backup settings .xml

(3) Edit the settings.xml file, modify <localRepository> to specify your directory (do not use the Chinese directory)

        D:\XXXX\maven-work\repository (personal computer storage location)

Open the settings.xml file and find that the original configuration is as follows, in line 53

 We copy the <localRepository> tag, and change the path in the tag to the repository folder that we copied from the default C drive

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

  <localRepository>D:\JDK\maven-work\repository</localRepository>

3. Maven Core 

1. Agreed directory structure

The maven project adopts the principle of " convention over configuration "; the directory and file location of the maven project are all stipulated

In general, Conventions > Configuration > Encoding

Maven defaults to a set of directory structures. After a project is created through Maven, the directory structure of the project is created using this directory structure as a template. Although Maven provides many templates, the basic directory structure is fixed

There are three main files in the root directory

src,target,pom.xml

Hello        根目录(工程名)
|---src      源代码          
|---|---main    主程序
|---|---|---java    主程序的 java 源码
|---|---|---resources    主程序的配置文件
|---|---|---webapp  (web项目)该目录是web应用源代码目录,如html文件和web.xml等都在该目录下
|---|---|---filters 项目的资源过滤文件所在的目录
|---|---test    测试程序
|---|---|---java    测试程序的 java 源码
|---|---|---resources    测试程序的配置文件
|---|---|---filters 测试相关的资源过滤文件所在的目录
|---target    存放编译、打包后的输出文件
|---pom.xml    Maven 工程的核心配置文件

We can enter in the DOS command window or Windows PowerShell

tree 根目录文件名

As follows, tree Hello 

  

2、POM

POM ( Project Object Model, project object model ) is the basic work unit of Maven project. It is an XML file ( pom.xml ), which contains the basic information of the project, and is used to describe how the project is built, declare project dependencies, and so on.

When executing a task or goal, Maven looks for a POM in the current directory. It reads the POM, fetches the required configuration information, and executes the goal. Maven uses a project as a model. Control the process of maven building projects and manage jar dependencies

 Supplementary label:

1. <scope> tag: dependent scope

The scope values ​​are compile, test, provided , and the default is compile

(1) compile scope dependencies:

Whether it is valid for the main program: valid
Whether it is valid for the test program: valid
Whether it participates in packaging: participates
whether it participates in deployment: participates
Typical example: spring-core

(2) Provided scope depends on:

Whether it is valid for the main program: valid
Whether it is valid for the test program: valid
Whether to participate in packaging: not involved
Whether to participate in deployment: not involved
Typical example: server-api.jar

(3) Test scope depends on:

Whether it is valid for the main program: invalid
Whether it is valid for the test program: valid
Whether to participate in packaging: not involved
Whether to participate in deployment: not involved
Typical example: junit

provided is mainly for web engineering

The compilation, testing, packaging, installation, and deployment processes (phases) of maven build projects work

As follows, the dependency scope of junit is test

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
</dependency>

2. <properties> tag: attribute setting

[1] Common property settings in maven

<properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 项目构建使用的编码,避免中文乱
码
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 生成报告的编码
     <maven.compiler.source>1.8</maven.compiler.source> 源码编译 jdk 版本
     <maven.compiler.target>1.8</maven.compiler.target> 运行代码的 jdk 版本
 </properties>

[2] Set global variables

In Maven's pom.xml file, <properties> is used to define global variables

The value of the variable is referenced in the form of ${property_name} in the POM, and the label name is generally composed of the project name + field version

A single word can be directly named XX.version, and multiple words can be separated by a horizontal line XX-XX-version

define global variables

<properties>
    <dubbo.version>2.6.2</dubbo.version>
    <spring-context-version>4.3.10.RELEASE</spring-context-version>
</properties>

 refer to global variables

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>${dubbo.version}</version>
</dependency>


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-context-version}</version>
</dependency>

3. <resource> tag: resource; <build> tag: build

All *.java files in the src/main/java and src/test/java directories will be compiled in the comile and test-comiple phases respectively, and the compilation results will be placed in the target/classes and target/test-classes directories respectively , but other files in these two directories will be ignored. If you need to put the file package in the src directory into the target/classes directory, it will be part of the output jar. Resource file location needs to be specified. Put the following content in the <buid> tag

<build>
    <resources>
         <resource>
             <directory>src/main/java</directory><!--所在的目录-->
             <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
                 <include>**/*.properties</include>
                 <include>**/*.xml</include>
             </includes>
             <!-- filtering 选项 false 不启用过滤器, *.property 已经起到过滤的作用了 -->
             <filtering>false</filtering>
         </resource>
    </resources>
</build>

 3. Coordinates (gav)

Maven manages any plug-in as a project in the warehouse, and uses a set of (three) vector coordinates to represent the coordinates. A Maven project can be uniquely located in the warehouse

groupId: Organization name, usually the reverse order of the company or organization domain name + project name

artifactId: module name, usually the project name

version: version number

The position of the project in the warehouse is determined by coordinates: groupId, artifactId and version determine the path of the project in the warehouse, artifactId and version determine the name of the jar package

4. Dependency management (dependency)

Manage jar files that can be used by projects

Dependency scope: compile, test, provided, compile is used by default

Note:

All dependencies (jar) used in the project must be in the local warehouse; none must be downloaded through maven, including provided ones must be downloaded 

5. Warehouse management

The Maven repository can help us manage its files (mainly JAR), which is where all JARs (WAR, ZIP, POM) are placed

1. Maven warehouse storage

        [1] Maven plug-ins, plug-ins are also some jars, these jars can complete certain functions

        [2] Modules of our own development projects

        【3】Jar packages of third-party frameworks or tools

2. Warehouse classification

    Local warehouse: a folder on the personal computer, storing various jars

        Stored in ~\.m2\repository by default

    Remote warehouse, a warehouse on an Internet server that can only be used using the Internet

        [1] Central warehouse, the most authoritative, a centralized warehouse shared by all developers        

           https://repo.maven.apache.org : the address of the central warehouse

        [2] The mirror image of the central warehouse: it is the backup of the central warehouse. On all continents, important cities are mirror images

        【3】Private servers are used within the company and in the local area network, not for external use.

6. Life cycle

The process of building a project with the maven tool is the life cycle

The process of maven building a project, cleaning, compiling, testing, reporting, packaging, installation, deployment

(1)mvn compile        

Compile the java in the main/java/ directory into a class file, and copy the class to the target/classes directory,
and copy all the files in the main/resources directory to the target/classes directory

(2)mvn test-compile         

Compile the test program (a target directory will be generated in the current directory, which stores the bytecode files generated after compiling the test program)

(3)mvn test         

Test (a directory surefire-reports will be generated to save the test results)

(4)mvn package        

Package the main program (compile, compile and test, test, and package the main program to generate a jar package or war package according to the pom.xml configuration)

(5)mvn install         

Install the main program (this project will be packaged and saved in the local warehouse according to the coordinates of this project)

(6)mvn deploy         

Deploy the main program (this project will be packaged, saved to the local library according to the coordinates of this project, and will also be saved to the private server warehouse and automatically deployed to the web container)

(1) After packaging with mvn package, a project name-version jar package will be generated in the target directory of the project directory

(2) Copy it to the maven local library copied by yourself, follow the project path com.company organization name.project name

 (3) View the coordinate (gav) information under the pom.xml file of the currently packaged project

 

(4) Copy it to the project that needs to rely on references 

(5) Compile the test and pass

7. Plug-ins

When the maven command is executed, it is the plug-in that actually completes the function. The plug-in is some jar files, some classes

8. Inheritance

9. Polymerization

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/122560887