Do you really know maven? (Project structure, life cycle, unified version, inheritance management)

basic concept:

# Dependent coordinates

Dependence: project up and running required jar package

Coordinates: pack a lot of the same jar, which you want.
(Designated dependent companies, id, version)

# Warehouse

Central warehouse, third-party warehouse, local warehouse

# Project structure

The main concern, pom.xmlandtarget

Here Insert Picture Description

Here Insert Picture Description

# Life cycle

Here Insert Picture Description

Of life cycle strange 冒险

Form the command line, quickly experience the entire life cycle maven

Prerequisite: configure maven
Here Insert Picture Description

1. Create: mvn archetype: generate

mvn archetype:generate -DgroupId=org.test -DartifactId=a-maven-test -DarchetypeArtifactId=maven-archetype-quickstart
  • mvn - Core Command
  • archetype:generate - Create Project
  • -DgroupId=org.test - the wording of the package name
  • -DartifactId=a-maven-test --project name
  • -DarchetypeArtifactId=maven-archetype-quickstart - represents the creation of a [maven] java project
    Here Insert Picture Description

You can use the DOS command: tree, view the project structure

Here Insert Picture Description

2. Compile: compile

The file is compiled under src / main / java output to the next target directory for the class files

Execute the code:mvn compile

Here Insert Picture Description

3, test: test

Performing unit under test class src / test / java, test report generation

Execute the code:mvn tset

Here Insert Picture Description
Test report (below), a record run success, failure, the situation is skipped, the test environment.

Here Insert Picture Description

4, clear: clean

Clear target directory

Execute the code:mvn clean

"Back to the past - the original"

Here Insert Picture Description

5, Package: package

For java project packaged into jar package for web project packaged into war package

Execute the code:mvn package

Generating the target directory, structurally equal to compile + test

Here Insert Picture Description

However, to the next target directory, it will be more out of a jar / war files
Here Insert Picture Description

6, release: install

Execute the code:mvn install

This information is printed to look after install:
Here Insert Picture Description

indeed:

  • The jar deploy the package to a C:\\User\\uu\\.m2\\repository\\..............directory (that is, at my local warehouse)
  • The pom.xml packaged into a .pomfile with the extension, but also on my local repository.

Then, after

  • Other projects depend on this project, you can rely on to get from a local warehouse -
  • We rely on local warehouse published online, other people can also use the ~

Here Insert Picture Description

Unified version, inheritance management

Establish a project maven pom type
of unified management and version-dependent

Create an empty parent project(Like the src directory deleted)
Here Insert Picture Description
Create a sub-module
Here Insert Picture Description

Father modules written statement of pom and child

    ...
    
    <!--父模块声明-->
    <packaging>pom</packaging>
    <!--子模块-->
    <modules>
        <module>demo-child</module>
    </modules>
	...

At last,
Father modules unified version
Sub-module version inherited

Parent module

<?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.spring</groupId>
    <!--项目名-->
    <artifactId>demo</artifactId>
    <!--项目版本-->
    <version>1.0-SNAPSHOT</version>

    <!--父模块声明:不能进行任何的代码逻辑-->
    <packaging>pom</packaging>
    <!--子模块-->
    <modules>
        <module>demo-child</module>
    </modules>

    <!--统一版本号-->
    <properties>
        <!--版本(标签名自定义)-->
        <spring-boot>2.2.5.RELEASE</spring-boot>
    </properties>

    <!--依赖们的管理-->
    <dependencyManagement>
        <!--一堆依赖-->
        <dependencies>

            <!--一个依赖-->
            <dependency>
                <!--组织-->
                <groupId>org.springframework.boot</groupId>
                <!--坐标名称-->
                <artifactId>spring-boot-starter</artifactId>
                <!--版本:交给properties统一管理-->
                <version>${spring-boot}</version>
            </dependency>

            <!--按住Alt+Insert插入依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring-boot}</version>
            </dependency>
            
			<!--推荐:idea的 maven-helper 好用到怀疑eclipse-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <!--也能直接直接版本-->
                <version>3.8.1</version>
                <!--仅test生命周期使用-->
                <scope>test</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

</project>

Sub-module

<?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">
    <!--父模块的坐标。如果项目中没有归档某个元素的值,那么就从父模块中找值-->
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.spring</groupId>
        <version>1.0-SNAPSHOT</version>
        <!--模块间的相对位置,闭合标签默认为上一级文件夹内-->
        <relativePath/>
    </parent>
    <!--POM文件的模型版本。很少改变,必不可少。设计的初衷是为了引入Maven新特性和稳定性-->
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-child</artifactId>

    <!--项目打包时候的类型。例如:(这里使用的)jar/war/(父项目的)pom/ear...-->
    <packaging>jar</packaging>

    <!--项目的名称,Maven产生的文档用-->
    <name>child-demo</name>
    <!--项目主页的URL,Maven产生的文档用-->
    <url>https://www.baidu.com</url>
    <!--项目的描述。Maven产生的文档用-->
    <description>A maven project to study maven.</description>


    <dependencies>
        <!--一个依赖:引用父组件的版本-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <!--不需要指定版本-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

Here Insert Picture Description

Published 501 original articles · won praise 112 · views 20000 +

Guess you like

Origin blog.csdn.net/LawssssCat/article/details/104901174