溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用maven快速入門

發(fā)布時(shí)間:2020-07-07 16:49:10 來源:網(wǎng)絡(luò) 閱讀:753 作者:zhangpan0614 欄目:編程語言

Maven 基礎(chǔ)知識(shí)

官網(wǎng): 傳送門

Maven 項(xiàng)目結(jié)構(gòu)

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

POM文件

  • POM文件代表 工程對(duì)象模型(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>分布式廣告系統(tǒng)</name>
    <description>基于Spring Cloud Alibaba 實(shí)現(xiàn)的分布式廣告系統(tǒng)</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>

    <!--定義遠(yuǎn)程maven倉(cāng)庫-->
    <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 坐標(biāo)

使用maven快速入門

Maven Dependency

Maven思想 是 約定大于配置,默認(rèn)依賴中,scope 是compile.

Scope類型
  • compile(會(huì)被打包到當(dāng)前project)

    表示被依賴的package參與當(dāng)前project的編譯,包含后續(xù)的測(cè)試,運(yùn)行周期都會(huì)參與,是一個(gè)強(qiáng)依賴。

  • test

    表示被依賴的jar 僅參與測(cè)試相關(guān)的處理,包裹測(cè)試代碼的編譯,執(zhí)行。(如junit)

  • runtime

    表示被依賴的jar不需要參與項(xiàng)目的編譯,但是后期的測(cè)試和運(yùn)行周期需要參與。

  • provided

    打包的時(shí)候不需要包含進(jìn)去,其他的Container會(huì)提供該依賴支持,理論上該依賴可以參與編譯、測(cè)試運(yùn)行等周期 ,相當(dāng)于compile,但是在打包階段做了exclude命令。

  • system

    從參與環(huán)境來看,和provided相同,但是被依賴項(xiàng)不會(huì)從maven倉(cāng)庫獲取,而是從本地文件系統(tǒng)獲取,一定需要配合systemPath屬性使用

  • import

    This scope is only supported on a dependency of type pom in the &lt;dependencyManagement&gt; 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.

    根據(jù)依賴深度,選擇依賴路徑最近的package version, 如果依賴深度相同,那么選擇前一個(gè)。Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.

  • Dependency management

    依賴管理(項(xiàng)目作者可直接指定依賴版本)

  • Dependency scope 如上一節(jié)所述

  • Excluded dependencies 排除依賴包中依賴項(xiàng)

  • Optional dependencies (相當(dāng)于設(shè)置不允許將該依賴傳遞下去)

使用maven快速入門

常用命令

使用maven快速入門

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI