使用maven快速入門

Maven 基礎知識

官網: 傳送門html

Maven 項目結構java

$ MavenProject
|-- pom.xml
|-- src
|   |-- main
|   |   `-- java
|   |   `-- resources
|   `-- test
|   |   `-- java
|   |   `-- resources
`-- README.md

POM文件

  • POM文件表明 工程對象模型(Project Object Model)它是使用Maven工做的基本組件,位於工程根目錄。
  • POM文件支持繼承
<?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>
    <packaging>pom</packaging>
    <modules>
        <module>mscx-ad-discovery</module>
        <module>mscx-ad-zuul</module>
        <module>mscx-ad-gateway</module>
        <module>mscx-ad-discovery-nacos</module>
        <module>mscx-ad-common</module>
        <module>mscx-ad-db</module>
        <module>mscx-ad-sponsor</module>
        <module>mscx-ad-search</module>
        <module>mscx-ad-feign-sdk</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.sxzhongf</groupId>
    <artifactId>mscx-ad</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>分佈式廣告系統</name>
    <description>基於Spring Cloud Alibaba 實現的分佈式廣告系統</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--定義遠程maven倉庫-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>alibaba</id>
            <name>ali Milestones</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Maven 座標

UTOOLS1563873610326.png

Maven Dependency

Maven思想 是 約定大於配置,默認依賴中,scope 是compile.spring

Scope類型

  • compile(會被打包到當前project)apache

    表示被依賴的package參與當前project的編譯,包含後續的測試,運行週期都會參與,是一個強依賴。json

  • testmaven

    表示被依賴的jar 僅參與測試相關的處理,包裹測試代碼的編譯,執行。(如junit)分佈式

  • runtimeide

    表示被依賴的jar不須要參與項目的編譯,可是後期的測試和運行週期須要參與。spring-boot

  • provided測試

    打包的時候不須要包含進去,其餘的Container會提供該依賴支持,理論上該依賴能夠參與編譯、測試運行等週期 ,至關於compile,可是在打包階段作了exclude命令。

  • system

    從參與環境來看,和provided相同,可是被依賴項不會從maven倉庫獲取,而是從本地文件系統獲取,必定須要配合systemPath屬性使用

  • import

    This scope is only supported on a dependency of type pom in the <dependencyManagement> section.

依賴傳遞特性

官方解釋:傳送門

  • Dependency mediation (最近依賴原則)

    "nearest definition" means that the version used will be the closest one to your project in the tree of dependencies. For example, if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0.

    根據依賴深度,選擇依賴路徑最近的package version, 若是依賴深度相同,那麼選擇前一個。Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.

  • Dependency management

    依賴管理(項目做者可直接指定依賴版本)

  • Dependency scope 如上一節所述

  • Excluded dependencies 排除依賴包中依賴項

  • Optional dependencies (至關於設置不容許將該依賴傳遞下去)

UTOOLS1563869243423.png

經常使用命令

UTOOLS1563873521089.png

相關文章
相關標籤/搜索