POM文件

  • 解決依賴沖突
  • 引用變量的三種情況(maven命令)
  • 多環(huán)境屬性過濾
  • 各種依賴(POM文件詳解)

解決maven傳遞依賴中的版本沖突

傳遞依賴是maven最有特色的、最為方便的優(yōu)點之一,可以省了很多配置。如a 依賴 b,b 依賴c 默認 a也會依賴 c。但是 也會帶來隱患,如版本沖突。當(dāng)然maven也考慮到解決辦法,可以使用exclusions來排除相應(yīng)的重復(fù)依賴。

但是我們還會遇到一個嚴(yán)重的問題,那就是,我怎么知道是哪個包的傳遞依賴產(chǎn)生的沖突 ?那該怎么辦呢?當(dāng)然,maven也會有相應(yīng)的解決方案。

首先,你要在pom.xml中加上maven-project-info-reports-plugin插件。

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>
maven-project-info-reports-plugin
</artifactId>
</plugin>
</reporting>

然后再執(zhí)行:mvn project-info-reports:dependencies 。生成項目依賴的報表,這樣你就能夠在報表中找出你版本沖突的相關(guān)性依賴了。

最后在相應(yīng)的dependency中加上exclusions來排除相關(guān)的傳遞依賴。

例:

          <dependency>
                    <groupId>jaxen</groupId>
                    <artifactId>jaxen</artifactId>
                    <version>1.1.1</version>
                    <exclusions>
                            <exclusion>
                                    <groupId>com.ibm.icu</groupId>
                                    <artifactId>icu4j</artifactId>
                            </exclusion>
                    </exclusions>
                    <scope>runtime</scope>
            </dependency>

----------------------------------------------------------

maven中內(nèi)置隱式變量的使用:

1、引用pom.xml的project下的標(biāo)簽元素

引用pom.xml的project根標(biāo)簽下的自標(biāo)簽
例如:${project.groupId}
      ${project.artifactId}
使用maven命令查看pom.xml的完整信息
1.${project.build.directory} 構(gòu)建目錄,缺省為target

2.${project.build.outputDirectory} 構(gòu)建過程輸出目錄,缺省為target/classes

3.${project.build.finalName} 產(chǎn)出物名稱,缺省為
   ${project.artifactId}-${project.version}

4.${project.packaging} 打包類型,缺省為jar

5.${project.xxx} 當(dāng)前pom文件的任意節(jié)點的內(nèi)容

6.${basedir} 項目根目錄

maven命令

2、引用maven的settings.xml中的元素

(默認settings.xml的路徑:~/.m2/settings.xml)

使用方式:
${settings.offline}會引用~/.m2/settings.xml文件中offline元素的值。

3、引用系統(tǒng)環(huán)境變量的屬性

使用方式:${env.PATH}——會被操作系統(tǒng)中的環(huán)境變量替換

----------------------------------------------------------

profile和filtering實現(xiàn)多個環(huán)境下的屬性過濾

1、配置文件:“test.properties”,里面隨便來上一行,例如Hello ${user.name}
 2、<build>  
        <!--第一種方式,兩種方式都需要指定需要編譯的目錄 -->  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
                <!--可以在此配置過濾文件  -->  
                <includes>  
                    <include>**/*.xsd</include>  
                    <include>**/*.properties</include>  
                </includes>  
                <!--開啟filtering功能  -->  
                <filtering>true</filtering>  
            </resource>  
        </resources> 
  <build>
3、編譯我們的maven項目
 $mvn clean compile -Duser.name=tom

查看輸出文件 target/classes/test.properties 的內(nèi)容,可見原先的 
“Hello {user.name}” 已經(jīng)變成 “Hello Tom”。
   <build>  
        <!--第二種方式,兩種方式都需要指定需要編譯的目錄 -->  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
                <!--可以在此配置過濾文件 -->  
                <includes>  
                    <include>**/*.xsd</include>  
                    <include>**/*.properties</include>  
                </includes>  
                <!--開啟filtering功能 -->  
                <filtering>true</filtering>  
            </resource>  
        </resources>  
  
        <filters>  
            <!-- 是以該pom文件路徑作為參考 -->  
            <filter>project.properties</filter>  
        </filters> 
  <build>
profile功能:允許在項目文件(pom.xml)里面定義若干個profile段,然后在編譯時選擇
            其中的一個用于覆蓋項目文件原先的定義。
  <build>  
        <!--第一種方式,兩種方式都需要指定需要編譯的目錄 -->  
        <resources>  
            <resource>  
                <directory>src/main/resources</directory>  
                <!--可以在此配置過濾文件 -->  
                <includes>  
                    <include>**/*.xsd</include>  
                    <include>**/*.properties</include>  
                </includes>  
                <!--開啟filtering功能 -->  
                <filtering>true</filtering>  
            </resource>  
        </resources>  
  
        <profiles>  
            <profile>  
                <id>dev</id>  
                <activation>  
                <!--默認的編譯選項  -->  
                    <activeByDefault>true</activeByDefault>  
                </activation>  
                <build>  
                    <filters>  
                        <filter>pre.properties</filter>  
                    </filters>  
                </build>  
            </profile>  
  
            <profile>  
                <id>pre</id>  
                <build>  
                    <filters>  
                        <filter>dev.properties</filter>  
                    </filters>  
                </build>  
            </profile>  
        </profiles>  
  
  
        <plugins>  
            <plugin>  
                <artifactId>maven-war-plugin</artifactId>  
                <configuration>  
                    <version>2.5</version>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>

在編譯項目時,可以使用 -P 參數(shù)指定需要使用的 profile 的 id,比如下面命令將會使用 
dev profile:$mvn clean compile -P dev

如果想使用pre,只需要改為以下即可
$mvn clean compile -Ppre

假如不指定 -P 參數(shù)的話,則會使用 activeByDefault=true 的一項(即 pre)。

----------------------------------------------------------

<modelVersion>4.0.0</modelVersion>
<!-- 模型版本。maven2.0必須是這樣寫,現(xiàn)在是maven2唯一支持的版本 -->
<groupId>com.ali.aa</groupId>
<!-- 公司或者組織的唯一標(biāo)志,并且配置時生成的路徑也是由此生成, 如com.ali.aa,
     maven會將該項目打成的jar包放本地路徑:/com/ali/aa --> 
<artifactId>core</artifactId>
<!-- 本項目的唯一ID,一個groupId下面可能多個項目,就是靠artifactId來區(qū)分的 --> 
<version>1.0.0-SNAPSHOT</version>
<!-- 本項目目前所處的版本號 --> 
<packaging>jar</packaging>
<!-- 打包的機制,如pom,jar, maven-plugin, ejb, war, ear, rar, par,
     默認為jar -->

POM文件詳解


<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.3</version>
</dependency>

java_Date與 Joda Time


commons類庫詳解

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>${commons-io.version}</version>
</dependency>

Commons io--IOUtils
Commons之Commons-io


<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>${commons-lang3.version}</version>
</dependency>

commons-lang3方法應(yīng)用
commons-lang3工具類的學(xué)習(xí)


<dependency>  
   <groupId>org.apache.httpcomponents</groupId>  
   <artifactId>httpclient</artifactId>  
   <version>4.5.2</version>  
</dependency> 

httpcomponents介紹和使用
學(xué)習(xí)筆記1
學(xué)習(xí)筆記2
httpcomponents之httpclient發(fā)送http請求


<dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>${httpclient.version}</version>
      <exclusions>
        <exclusion>
            <artifactId>commons-codec</artifactId>
            <groupId>commons-codec</groupId>
        </exclusion>
       </exclusions>
  </dependency>

commons codec基本使用
使用Commons codec 加密
Commons codec使用介紹


<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>${commons-collections.version}</version>
</dependency>

集合工具類的使用
commons-collections之Map

----------------------------------------------------------

build部分

lifecycle:生命周期,這是maven最高級別的的控制單元,它是一系列的phase組成,也就
是說,一個生命周期,就是一個大任務(wù)的總稱,不管它里面分成多少個子任務(wù),反正就是運
行一個lifecycle,就是交待了一個任務(wù),運行完后,就得到了一個結(jié)果,中間的過程,是
phase完成的,自己可以定義自己的lifecycle,包含自己想要的phase

phase:可以理解為任務(wù)單元,lifecycle是總?cè)蝿?wù),phase就是總?cè)蝿?wù)分出來的一個個子任
務(wù),但是這些子任務(wù)是被規(guī)格化的,它可以同時被多個lifecycle所包含,一個lifecycle可
以包含任意個phase,phase的執(zhí)行是按順序的,一個phase可以綁定很多個goal,至少為一
個,沒有g(shù)oal的phase是沒有意義的

goal: 這是執(zhí)行任務(wù)的最小單元,它可以綁定到任意個phase中,一個phase有一個或多個
goal,goal也是按順序執(zhí)行的,一個phase被執(zhí)行時,綁定到phase里的goal會按綁定的時
間被順序執(zhí)行,不管phase己經(jīng)綁定了多少個goal,你自己定義的goal都可以繼續(xù)綁到
phase中。 

官方文檔:
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.
html
http://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html

mojo: lifecycle與phase與goal都是概念上的東西,mojo才是做具體事情的,可以簡單理
解mojo為goal的實現(xiàn)類,它繼承于AbstractMojo,有一個execute方法,goal等的定義都
是通過在mojo里定義一些注釋的anotation來實現(xiàn)的,maven會在打包時,自動根據(jù)這些
anotation生成一些xml文件,放在plugin的jar包里

詳情

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

相關(guān)閱讀更多精彩內(nèi)容

  • 簡介 概述 Maven 是一個項目管理和整合工具 Maven 為開發(fā)者提供了一套完整的構(gòu)建生命周期框架 Maven...
    閩越布衣閱讀 4,545評論 6 39
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • Maven命令: Maven不單止可以在Eclipse上使用,也可以在cmd中使用命令的方式操作,雖然這樣十分麻煩...
    凱哥學(xué)堂閱讀 545評論 0 0
  • 0、POM文件進階 pom文件可以繼承,以達到高效復(fù)用 maven官方制定的superpom 所有的pom都會繼承...
    拾壹北閱讀 759評論 0 2
  • 《 白鹿原》是我大學(xué)就非常喜歡的小說,前一段時間的電視劇被停播了,最近又復(fù)播,著實讓我又一次回顧了這部經(jīng)典的...
    家多寶TL閱讀 279評論 0 0

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