文章目錄
項目
- github 建個倉庫
- 執行 git clone 克隆項目到本地
- idea 新建項目
Maven聚合
<modules>
<module>模塊1</module>
<module>模塊2</module>
<module>模塊n</module>
</modules>
<package>pom</package>
新的項目中執行任何
mvn
命令,都會modules
中包含的所有模塊執行同樣的命令,而被包含的模塊不需要做任何特殊的配置,正常的maven
項目就行
rpc 模塊創建
不繼承父pom, parent:None
繼承
-
創建一個父maven構件,將依賴信息放在pom.xml中
<dependencies> <dependency>依賴的構件的座標信息</dependency> <dependency>依賴的構件的座標信息</dependency> <dependency>依賴的構件的座標信息</dependency> </dependencies>
-
將父構件的package元素的值置爲pom
<packaging>pom</packaging>
-
在子構件的pom.xml引入父構件的配置:
<parent> <groupId>父構件groupId</groupId> <artifactId>父構件artifactId</artifactId> <version>父構件的版本號</version> <relativePath>父構件pom.xml路徑</relativePath> </parent>
relativePath
表示父構件pom.xml相對路徑
,默認是../pom.xml
,所以一般情況下父子結構的maven
構件在目錄結構上一般也採用父子關係。
pom.xml 文 件
pom 依賴配置
mvn dependency:tree
這個插件可以根據pom.xml的配置,列出構件的依賴樹信息。
relativePath元素
父構件和子構件的目錄結構剛好符合父子關係,如果父構件和子構件的目錄不是父子關係,比如都位於同等級別的目錄或者位於更復雜的目錄的時候,此時我們需要在子pom.xml
的parent
元素中使用relativePath
元素來指定父pom.xml
相對路徑位置,這個值我們上面沒有指定,默認是../pom.xml
,表示父pom.xml
位於子pom.xml
的上一級目錄,我們的模塊剛好符合這種關係,所以這個值省略了。
正確的設置relativePath
是非常重要的,這個需要注意,子模塊中執行mvn
命令的時候,會去找父pom.xml
的配置,會先通過relativePath
指定的路徑去找,如果找不到,會嘗試通過座標在本地倉庫進行查找,如果本地找不到,會去遠程倉庫找,如果遠程倉庫也沒有,會報錯。
可以通過繼承的元素有以下這些
- groupId:項目組ID,項目座標的核心元素
- version:項目版本,項目座標的核心元素
- description:項目的描述信息
- organization:項目的組織信息
- inceptionYear:項目的創始年份
- url:項目的url地址
- developers:項目的開發者信息
- contributors:項目的貢獻者信息
- distributionManagement:項目的部署配置信息
- issueManagement:項目的缺陷跟蹤系統信息
- ciManagement:項目的持續集成系統信息
- scm:項目的版本控制系統信息
- mailingLists:項目的郵件列表信息
- properties:自定義的maven屬性配置信息
- dependencyManagement:項目的依賴管理配置
- repositories:項目的倉庫配置
- build:包括項目的源碼目錄配置、輸出目錄配置、插件管理配置等信息
- reporting:包括項目的報告輸出目錄配置、報告插件配置等信息
依賴管理(dependencyManagement)
在新增一個子構件,都會默認從父構件中繼承依賴的一批構建,父pom.xml中配置的這些依賴的構建可能是其他項目不需要的,可能某個子項目只是想使用其中一個構件,但是上面的繼承關係卻把所有的依賴都給傳遞到子構件中了,這種顯然是不合適的。
maven
提供的dependencyManagement
元素既能讓子模塊繼承到父模塊的依賴配置,又能保證子模塊依賴使用的靈活性,在dependencyManagement
元素下聲明的依賴不會引入實際的依賴,他只是聲明瞭這些依賴,不過它可以對dependencies
中使用的依賴起到一些約束作用。
子模塊如果想用到這些配置,可以dependencies
進行引用,引用之後,依賴纔會真正的起效。並且版本號可以省略。
使用dependencyManagement
來解決繼承的問題,子pom.xml
中只用寫groupId,artifactId
就可以了,其他信息都會從父dependencyManagement
中聲明的依賴關係中傳遞過來,通常我們使用這種方式將所有依賴的構建在父pom.xml
中定義好,子構件中只需要通過groupId,artifactId
就可以引入依賴的構建,而不需要寫version
,可以很好的確保多個子項目中依賴構件的版本的一致性,對應依賴構件版本的升級也非常方便,只需要在父pom.xml
中修改一下就可以了。
單繼承問題
dependencyManagement
的使用,但是有個問題,只有使用繼承的時候,dependencyManagement
中聲明的依賴纔可能被子pom.xml
用到,如果我的項目本來就有父pom.xml
了,但是我現在想使用另外一個項目dependencyManagement
中聲明的依賴,此時我們怎麼辦?這就是單繼承的問題.
當我們想在項目中使用另外一個構件中dependencyManagement
聲明的依賴,而又不想繼承這個項目的時候,可以在我們的項目中使用加入下面配置:
<dependencyManagement>
<dependencies>
<dependency>
<!-- spring cloud 依賴-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
插件管理(pluginManagement)
maven中
提供了dependencyManagement
來解決繼承的問題,同樣也提供了解決插件繼承問題的pluginManagement
元素,在父pom中可以在這個元素中聲明插件的配置信息,但是子pom.xml中不會引入此插件的配置信息,只有在子pom.xml
中使用plugins->plugin
元素正在引入這些聲明的插件的時候,插件纔會起效,子插件中只需要寫groupId
、artifactId
,其他信息都可以從父構件中傳遞過來.
子pom.xml
中寫上插件的groupId、artifactId
就可以了,其他信息會從父pom.xml
中插件的定義中傳遞過來,而子pom.xml
中也可以自定義插件的這些配置
案例
父pom.xml
<pluginManagement>
<plugins>
<!--
verify是在測試完成之後並將構件安裝到本地倉庫之前執行的階段,
在這個階段我們生成源碼
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<!-- 使用插件需要執行的任務 -->
<execution>
<!-- 任務id -->
<id>attach-source</id>
<!-- 任務中插件的目標,可以指定多個 -->
<goals>
<goal>jar-no-fork</goal>
</goals>
<!-- 綁定的階段 -->
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
子pom.xml
<!--
生成源碼
其他信息會從父pom.xml中插件的定義中傳遞過來,
但子pom.xml中也可以自定義插件的這些配置。
父子pom.xml中插件配置信息會合並。
可以通過 mvn help:effective-pom 命令解析得到這個構件最終 pom.xml 的內容。
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-source</id>
<goals>
<goal>help</goal>
</goals>
</execution>
</executions>
</plugin>
父子
pom.xml
中插件配置信息會合並。可以通過mvn help:effective-pom
命令解析得到這個構件最終pom.xml
的內容
聚合與繼承的關係
- 聚合主要是爲了方便多模塊快速構建。
- 而繼承主要是爲了重用相同的配置。
代碼編譯時文件編碼配置
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
編譯代碼的時候,涉及到資源文件和測試資源文件的拷貝,拷貝文件的時候涉及到文件的編碼,這個是設置文件的編碼爲UTF-8格式的.
執行命令查看插件目標 resources 的詳細參數:
mvn help:describe -Dplugin=resources -Dgoal=resources -Ddetail
輸出內容如下:
encoding (Default: ${project.build.sourceEncoding})
The character encoding scheme to be applied when filtering resources.
encoding
這個參數用來指定編碼的,默認值是${project.build.sourceEncoding}
,也可以通過encoding用戶屬性來設置。
所以設置編碼的共四種:,
pom.xml中2種:
<encoding>UTF-8</encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
mvn命令中2種:
mvn compile -Dencoding=UTF-8
mvn compile -Dproject.build.sourceEncoding=UTF-8
mvn test
命令
mvn test運行測試用例的時候,測試用例類名的寫法默認是有規則的,這些規則有人知道麼?從哪裏可以看到這些規則?如何自定義?
目標詳細參數
$ mvn help:describe -Dplugin=surefire -Dgoal=test -Ddetail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-application 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-application ---
[INFO] Mojo: 'surefire:test'
surefire:test
Description: Run tests using Surefire.
Implementation: org.apache.maven.plugin.surefire.SurefirePlugin
Language: java
Bound to phase: test
Available parameters:
includes
A list of <include> elements specifying the tests (by pattern) that
should be included in testing. When not specified and when the test
parameter is not specified, the default includes will be
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
Each include item may also contain a comma-separated sub-list of items,
which will be treated as multiple <include> entries.
Since 2.19 a complex syntax is supported in one parameter (JUnit 4, JUnit
4.7+, TestNG):
可以看到上面有個
includes
參數,可以用來配置需要運行的測試用例,可以配置通配符的方式。
上面還有一段信息:
Implementation: org.apache.maven.plugin.surefire.SurefirePlugin
上面這部分列出了這個目標的具體實現類是SurefirePlugin。
查看實現類:SurefirePlugin 中 的 includes 的默認值
protected String[] getDefaultIncludes() {
return new String[]{"**/Test*.java", "**/*Test.java", "**/*Tests.java", "**/*TestCase.java"};
}
這部分代碼就是我們測試用例默認需要滿足的格式,你創建的測試用例默認情況下必須滿足上面這3種格式,否則,測試用例不會被
mvn test
執行