- 解決依賴沖突
- 引用變量的三種情況(maven命令)
- 多環(huán)境屬性過濾
- 各種依賴(POM文件詳解)
解決maven傳遞依賴中的版本沖突
傳遞依賴是maven最有特色的、最為方便的優(yōu)點(diǎn)之一而线,可以省了很多配置锋华。如a 依賴 b,b 依賴c 默認(rèn) 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 憾儒。生成項(xiàng)目依賴的報表询兴,這樣你就能夠在報表中找出你版本沖突的相關(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é)點(diǎn)的內(nèi)容
6.${basedir} 項(xiàng)目根目錄
maven命令
2、引用maven的settings.xml中的元素
(默認(rèn)settings.xml的路徑:~/.m2/settings.xml)
使用方式:
${settings.offline}會引用~/.m2/settings.xml文件中offline元素的值属百。
3记劝、引用系統(tǒng)環(huán)境變量的屬性
使用方式:${env.PATH}——會被操作系統(tǒng)中的環(huán)境變量替換
----------------------------------------------------------
profile和filtering實(shí)現(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項(xiàng)目
$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功能:允許在項(xiàng)目文件(pom.xml)里面定義若干個profile段,然后在編譯時選擇
其中的一個用于覆蓋項(xiàng)目文件原先的定義录豺。
<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>
<!--默認(rèn)的編譯選項(xiàng) -->
<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>
在編譯項(xiàng)目時,可以使用 -P 參數(shù)指定需要使用的 profile 的 id巩检,比如下面命令將會使用
dev profile:$mvn clean compile -P dev
如果想使用pre厚骗,只需要改為以下即可
$mvn clean compile -Ppre
假如不指定 -P 參數(shù)的話,則會使用 activeByDefault=true 的一項(xiàng)(即 pre)兢哭。
----------------------------------------------------------
<modelVersion>4.0.0</modelVersion>
<!-- 模型版本领舰。maven2.0必須是這樣寫,現(xiàn)在是maven2唯一支持的版本 -->
<groupId>com.ali.aa</groupId>
<!-- 公司或者組織的唯一標(biāo)志迟螺,并且配置時生成的路徑也是由此生成冲秽, 如com.ali.aa,
maven會將該項(xiàng)目打成的jar包放本地路徑:/com/ali/aa -->
<artifactId>core</artifactId>
<!-- 本項(xiàng)目的唯一ID矩父,一個groupId下面可能多個項(xiàng)目锉桑,就是靠artifactId來區(qū)分的 -->
<version>1.0.0-SNAPSHOT</version>
<!-- 本項(xiàng)目目前所處的版本號 -->
<packaging>jar</packaging>
<!-- 打包的機(jī)制,如pom,jar, maven-plugin, ejb, war, ear, rar, par窍株,
默認(rèn)為jar -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
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ù)微驶,反正就是運(yùn)
行一個lifecycle,就是交待了一個任務(wù),運(yùn)行完后因苹,就得到了一個結(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的實(shí)現(xiàn)類旺上,它繼承于AbstractMojo瓶蚂,有一個execute方法,goal等的定義都
是通過在mojo里定義一些注釋的anotation來實(shí)現(xiàn)的宣吱,maven會在打包時窃这,自動根據(jù)這些
anotation生成一些xml文件,放在plugin的jar包里