Maven

還沒(méi)有maven的時(shí)候,我們?cè)陧?xiàng)目添加依賴(lài)包的時(shí)候,需要自己下載添加到lib,但在項(xiàng)目中引入的依賴(lài)包其又依賴(lài)別的依賴(lài)包,并且可能存在版本沖突的問(wèn)題。

一、認(rèn)識(shí)maven

解決復(fù)雜依賴(lài)傳遞問(wèn)題,打包項(xiàng)目

  • 約定優(yōu)于配置
  • 簡(jiǎn)單易用
  • 支持測(cè)試
  • 插件豐富

二、安裝和配置

  • MVN_HOME
  • 配置setting.xml


    加載配置的順序
1、阿里鏡像
<mirror>  
  <id>alimaven</id>  
  <name>aliyun maven</name>  
   <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
  <mirrorOf>central</mirrorOf>          
</mirror> 
2、profies可以配置開(kāi)發(fā)、測(cè)試、生產(chǎn)不同環(huán)境時(shí)的配置
image.png

三、maven項(xiàng)目結(jié)構(gòu)

image.png

1、pom.xml

(1)、groupId 域名反過(guò)來(lái)
(2)、artfactId 功能命名
(3)、version 版本號(hào)
  • 主版本號(hào).次版本號(hào).增量版本號(hào)-<里程碑版本>
  • mvn clean package -U (每次上線(xiàn)版本release強(qiáng)制從遠(yuǎn)端倉(cāng)庫(kù)重新拉一次)
(4)、packaging 打包方式 默認(rèn)是jar
(5)、dependencyManagement

a)、只能出現(xiàn)在父pom
b)、統(tǒng)一版本號(hào)
c)、聲明 (子POM里用到再引)
dependencies即使在子項(xiàng)目中不寫(xiě)該依賴(lài)項(xiàng),那么子項(xiàng)目仍然會(huì)從父項(xiàng)目中繼承該依賴(lài)項(xiàng)(全部繼承)
dependencyManagement里只是聲明依賴(lài),并不實(shí)現(xiàn)引入,因此子項(xiàng)目需要顯示的聲明需要用的依賴(lài)。如果不在子項(xiàng)目中聲明依賴(lài),是不會(huì)從父項(xiàng)目中繼承下來(lái)的;只有在子項(xiàng)目中寫(xiě)了該依賴(lài)項(xiàng),并且沒(méi)有指定具體版本,才會(huì)從父項(xiàng)目中繼承該項(xiàng),并且version和scope都讀取自父pom;另外如果子項(xiàng)目中指定了版本號(hào),那么會(huì)使用子項(xiàng)目中指定的jar版本。

(6)、dependency
  • Type 默認(rèn)jar
  • scope 定義依賴(lài)包在什么時(shí)候,并且會(huì)不會(huì)在打包上(優(yōu)化pom文件)
    a)compile 編譯 (默認(rèn))
    b)test 測(cè)試
    c)provided 例如 tomcat已經(jīng)提供servlet的依賴(lài)包,這個(gè)依賴(lài)在編譯時(shí)就不會(huì)被打包上
    d)runtime運(yùn)行時(shí) 例如JDBC驅(qū)動(dòng)實(shí)現(xiàn)在運(yùn)行時(shí)才會(huì)用到,編譯時(shí)不會(huì)用到。
    e)system 本地一些jar 在maven中央倉(cāng)庫(kù)沒(méi)有的依賴(lài)包,例如短信jar
  • 依賴(lài)傳遞
    第一列表示直接依賴(lài)的scope,第一行表示間接依賴(lài)的scope


    image.png
  • 依賴(lài)仲裁
    a)、最短路徑原則
    b)、加載先后原則,當(dāng)最短路徑相等時(shí),則是按書(shū)寫(xiě)順序的先后


    image.png
  • exclusions
    排除包 例如spring內(nèi)引用common-logging,在項(xiàng)目并不會(huì)用來(lái),可用來(lái)排除這個(gè)依賴(lài)包。

四、生命周期

image.png

五、常用命令

  • compile
  • clean 刪除target/ 生成的包
  • test test case junit/testNG
  • package 打包
  • install 把項(xiàng)目install到local repo
  • deploy 發(fā)本地jar發(fā)布到remote

六、實(shí)現(xiàn)plugin

七、搭建私服

<distributionManagement>
    <repository>
        <id>xiaoyuan-nexus-release</id>
        <name>Nexus Release Repository</name>
        <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>xiaoyuan-nexus-snapshot</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>
  • d)、setting.xml配置
<servers>
    <server>
        <id>xiaoyuan-nexus-release</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>xiaoyuan-nexus-snapshot</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
</servers>

八、自定義archetype(腳手架)

  • 生成一個(gè)archetype
    a)、mvn archetype:create-from-project
    b)、cd /target/generated-sources/archetype
    c)、mvn install
  • 從archetype創(chuàng)建項(xiàng)目 mvn archetype:generate -DarchetypeCatalog=local

九、實(shí)現(xiàn)dev、test、pro不同環(huán)境下的配置

  • 不同環(huán)境下的配置文件


    image.png
  • pom.xml配置
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <profiles.active>test</profiles.active>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <properties>
            <profiles.active>pro</profiles.active>
        </properties>
    </profile>
</profiles>

<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>conf/**</exclude>
                </excludes>
        </resource>
        <resource>
            <directory>src/main/resources/conf/${profiles.active}</directory>
        </resource>
    </resources>
</bulid>
  • 在打包時(shí)則使用 mvn clean package -P pro,生成對(duì)應(yīng)的配置文件


    image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容