maven的settings.xml、pom.xml配置文件使用詳解
一、配置文件
maven的配置文件主要有 settings.xml 和pom.xml 兩個文件。
- 其中在maven安裝目錄下的settings.xml,如:D:\Program Files\apache-maven-3.6.3\conf\settings.xml 是全局配置文件
- 用戶目錄的.m2子目錄下的settings.xml,如:C:\Users\chenxc.m2\settings.xml 配置只是針對當(dāng)前用戶的配置文件
- 項目根路徑下的pom.xml主要是對當(dāng)前項目的配置。
局部配置優(yōu)先于全局配置。 配置優(yōu)先級從高到低:
pom.xml> user settings > global settings
二、settings.xml 配置詳解
1、localRepository
該值是此構(gòu)建系統(tǒng)的本地存儲庫的路徑。
默認(rèn)值為 ${user.home}/.m2/repository。
此元素對于主構(gòu)建服務(wù)器特別有用,允許所有登錄用戶從公共本地存儲庫進(jìn)行構(gòu)建。
<localRepository>D:\repository</localRepository>
2、interactiveMode
表示是否可以和用戶交互以獲得輸入,默認(rèn)為true。
<interactiveMode>true</interactiveMode>
3、offline
表示此構(gòu)建系統(tǒng)是否應(yīng)在離線模式下運行,則默認(rèn)為false。
此元素對于由于網(wǎng)絡(luò)設(shè)置或安全原因而無法連接到遠(yuǎn)程存儲庫的構(gòu)建服務(wù)器非常有用。
<offline>false</offline>
4、pluginGroups
插件組,該元素包含一個元素列表pluginGroup,每個元素包含一個groupId。
當(dāng)使用插件且命令行中未提供 groupId 時,將搜索該列表。該列表默認(rèn)包含 org.apache.maven.plugins和org.codehaus.mojo。
<pluginGroups>
<pluginGroup>org.eclipse.jetty</pluginGroup>
</pluginGroups>如下,在pom文件中沒有設(shè)置<groupId>,那么將會從<pluginGroups>列表中指定的位置搜索插件
<!-- 打包跳過測試 -->
<plugin>
<!-- <groupId>org.apache.maven.plugins</groupId> -->
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>5、servers
私服服務(wù)器配置,配置私服的用戶名和密碼。
配置的私服服務(wù)器可以用來發(fā)布jar包,與pom 中 distributionManagement 標(biāo)簽中配置的倉庫ID相互對應(yīng)。
<servers>
<server>
<id>server001</id>
<username>my_login</username>
<password>my_password</password>
<privateKey>${user.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration></configuration>
</server>
</servers>- id :該id與
<distributionManagement>中<repository>元素的<id>相匹配(注意不是用戶登陸的id)。
如下,在pom文件中設(shè)置的<distributionManagement>中<repository>元素的<id>要與settings.xml文件中<server>的<id>值一致才能匹配的上。
<distributionManagement>
<repository>
<id>server001</id>
<url>http://121.***.***.51:1481/repository/maven/</url>
</repository>
</distributionManagement>- username、password:這兩元素以一對形式出現(xiàn),表示向該服務(wù)器進(jìn)行身份驗證所需的登錄名和密碼。
- privateKey、passphrase:與前兩個元素一樣,指定了一個私鑰位置(默認(rèn)是
${user.home}/.ssh/id_dsa)以及私鑰密碼(私鑰密碼可以沒有)。將來<passphrase>和<password>元素可能會被提取到外部,但目前它們必須在settings.xml文件以純文本的形式聲明。 - filePermissions、DirectoryPermissions:在部署時創(chuàng)建存儲庫文件或目錄時,這些是要使用的權(quán)限。每個的合法值都是與 unix 文件權(quán)限相對應(yīng)的三位數(shù),例如 664 或 775。
注意:如果您使用私鑰<privateKey>登錄服務(wù)器,請確保沒有填寫<password>元素。否則<privateKey>將被忽略。
密碼加密
2.1.0+ 中添加了一項新功能 - 服務(wù)器密碼和密碼加密。請參閱此頁面的詳細(xì)信息
6、mirrors
<mirrors>
<mirror>
<id>planetmirror.com</id>
<name>PlanetMirror Australia</name>
<url>http://downloads.planetmirror.com/pub/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>- id , name:此鏡像的唯一標(biāo)識符和用戶友好的名稱。id用于區(qū)分鏡像元素,并在連接到鏡像時從
<servers>部分選擇相應(yīng)的憑據(jù)。 - url:該鏡像的基本 URL。構(gòu)建系統(tǒng)將使用此 URL 連接到存儲庫,而不是原始存儲庫 URL。
- mirrorOf:被鏡像的服務(wù)器的id。例如,要指向 Mavencentral存儲庫 ( https://repo.maven.apache.org/maven2/) 的鏡像,請將此元素設(shè)置為 central。更高級的映射如下:
*匹配所有存儲庫 ID。external:*匹配除使用本地主機(jī)或基于文件的存儲庫之外的所有存儲庫。當(dāng)您想要排除為集成測試定義的重定向存儲庫時,可以使用此選項。- 從 Maven 3.8.0 開始,
external:http:*匹配除使用 localhost 之外的所有使用 HTTP 的存儲庫。 - 可以使用逗號作為分隔符來指定多個存儲庫,如
repo,repo1= repo 或 repo1 !可以與上述通配符之一結(jié)合使用以排除存儲庫 ID,如!repo1除 repo1 之外的所有內(nèi)容
注意:
給定存儲庫最多可以有一個鏡像。換句話說,您無法將單個存儲庫映射到一組全部定義相同<mirrorOf>值的鏡像。Maven 不會聚合鏡像,而只是選擇第一個匹配項。
7、proxies
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>- id:該代理的唯一標(biāo)識符。這用于區(qū)分
<proxy>元素。 - active:true 如果此代理處于活動狀態(tài)。這對于聲明一組代理很有用,但一次只能有一個代理處于活動狀態(tài)。
- protocol、host、port:
protocol://host:port分別為代理的協(xié)議,主機(jī),端口元素。 - username、password:這些元素以一對形式出現(xiàn),表示對此代理服務(wù)器進(jìn)行身份驗證所需的登錄名和密碼。
- nonProxyHosts:這是不應(yīng)代理的主機(jī)列表。該列表的分隔符由代理服務(wù)器指定;例子中使用了豎線分隔符,使用逗號分隔也很常見。
8、profiles
setting.xml文件中的<profile>是pom中的<profile>的其中一部分,<profile>里包含<activation>、 <repositories>、<pluginRepositories>、<properties>這四個主要元素。
因為它們關(guān)注的是整個構(gòu)建系統(tǒng)(這就是settings.xml文件的作用),而不是單個項目對象模型設(shè)置。如果一個settings.xml中的<profile>被激活,它的值會覆蓋任何其它定義在pom中帶有相同id的<profile>。
<profiles>
<profile>
<id>test</id>
...
</profile>
</profiles>8.1、activation
<activation>是用來確定該<profile>是否被激活使用,當(dāng)<profile>被激活的時候, <repositories>、<pluginRepositories>、<properties>這三個元素里的配置才生效。
<activation>所以這個元素要至少要和其他三個元素之一同時使用才有意義。
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>mavenVersion</name>
<value>2.0.3</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>- activeByDefault:
<profile>默認(rèn)是否激活的標(biāo)識 - jdk:當(dāng)運行的java程序的
jdk的版本與指定的版本匹配時<profile>將被激活。如:上面的例子中,當(dāng)運行的java程序的jdk版本為1.5.0_06時<profile>將被激活。jdk的版本還支持范圍配。 有關(guān)支持范圍匹配的更多詳細(xì)信息,請參閱 maven-enforcer-plugin 。 - os:該os元素可以定義上面所示的一些操作系統(tǒng)特定屬性。 當(dāng)滿足條件時
<profile>將被激活- name:操作系統(tǒng)的名字匹配則激活該
<profile>。 - family:操作系統(tǒng)所屬家族則激活該
<profile>。 - arch:操作系統(tǒng)體系結(jié)構(gòu)則激活該
<profile>。 - version:操作系統(tǒng)版本則激活該
<profile>。
- name:操作系統(tǒng)的名字匹配則激活該
- property:如果Maven檢測到對應(yīng)的name=value對的屬性(其值可以在pom 中通過
${name}引用),則<profile>將激活。如果值字段是空的,那么存在屬性名稱字段就會激活profile,如:上面例子中如果存在mavenVersion=2.0.3,那么<profile>將激活。 - file:通過給定的文件存在或丟失來激活
<profile>- exists:如果指定的文件存在,則激活
<profile>。 - missing:如果指定的文件不存在,則激活
<profile>。
- exists:如果指定的文件存在,則激活
注意:
這多個激活條件的關(guān)系是這樣的:Maven 3.2.2之前是只要滿足其中之一就可以激活該<profile>,Maven 3.2.2之后是滿足所有才可以激活該<profile>。
8.2、properties
對應(yīng)<profile>的擴(kuò)展屬性列表。可以用來存放一些值。
這些值可以在pom文件中的任何地方使用標(biāo)記${X}來使用,這里X是指屬性的名稱(如下例中的${user.install})。
<profiles>
<profile>
...
<properties>
<user.install>${user.home}/our-project</user.install>
</properties>
...
</profile>
</profiles>屬性的值有五種不同的形式
- env.X: 在一個變量前加上"env."的前綴,會返回一個shell環(huán)境變量。例如:
env.PATH指代了$path環(huán)境變量(在Windows上是%PATH%)。 - project.x:指代了pom中對應(yīng)的元素值。例如:
<project><version>1.0</version></project>通過${project.version}獲得version的值。 - settings.x: 指代了settings.xml中對應(yīng)元素的值。例如:
<settings><offline>false</offline></settings>通過${settings.offline}獲得<offline>的值。 - java系統(tǒng)屬性: 所有可通過java.lang.System.getProperties()獲取的屬性都能在pom中使用該形式獲取,例如 ${java.home}。
- x: 在
<properties/>元素中,或者外部文件中設(shè)置,以${someVar}的形式使用。
如果我們想在application.peoperties文件中引用這個user.install屬性,這里注意引用時要用占位符@
user.install=@user.install@
在springboot工程中只能使用@xxx@(xxx為自定義的屬性名)
8.3、repositories
遠(yuǎn)程倉庫列表,它是Maven用來填充構(gòu)建系統(tǒng)本地倉庫所使用的一組遠(yuǎn)程倉庫列表。
<profiles>
<profile>
...
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
...
</profile>
</profiles>- id、name:遠(yuǎn)程倉庫唯一標(biāo)識和遠(yuǎn)程倉庫名稱
- releases、snapshots:如何處理遠(yuǎn)程倉庫里發(fā)布版本(releases)和快照版本(snapshots)的下載
- enabled:true或者false表示該倉庫是否開啟下載某種類型構(gòu)件(發(fā)布版,快照版)。
- updatePolicy:該元素指定更新發(fā)生的頻率。Maven會比較本地pom 和遠(yuǎn)程pom 的時間戳。這里的選項是:always(一直),daily(默認(rèn),每日),interval:X(這里X是以分鐘為單位的時間間隔),或者never(從不)。
- checksumPolicy:當(dāng)Maven驗證構(gòu)件校驗文件失敗時該怎么做-ignore(忽略),fail(失?。?,或者warn(警告)。
- layout:在上面對存儲庫的描述中,提到它們都遵循通用布局。這基本上是正確的。Maven 2 的存儲庫有一個默認(rèn)布局;然而,Maven 1.x 有不同的布局。使用此元素指定是default(默認(rèn))還是legacy(遺留)。
8.4、pluginRepositories
插件倉庫, Maven plugins是一種特殊的依賴項,與普通的jar包依賴倉庫分開定義,結(jié)構(gòu)與repositories類似。具體說明參考如上。
<profiles>
<profile>
...
<pluginRepositories>
<pluginRepository>
<id>myPluginRepo</id>
<name>My Plugins repo</name>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>https://maven-central-eu....com/maven2/</url>
</pluginRepository>
</pluginRepositories>
...
</profile>
</profiles>9. activeProfiles
它包含一組<activeProfile>元素,每個元素的值都是一個<profile>中<id>里的值。無論任何環(huán)境設(shè)置如何,<activeProfile>定義的任何<profile>都將處于活動狀態(tài)。
如果沒有找到匹配的配置文件,則不會發(fā)生任何事情。和<activation> 配置相比 <activeProfiles> 配置比較簡單,也比較常用。
<activeProfiles> <activeProfile>env-test</activeProfile> </activeProfiles>
三、pom.xml配置文件
<parent>
<!--父項目的構(gòu)件標(biāo)識符 -->
<artifactId />
<!--父項目的唯一標(biāo)識符 -->
<groupId />
<!--父項目的版本 -->
<version />
<!-- 父項目的pom.xml文件的相對路徑。
默認(rèn)值是../pom.xml。
Maven首先在構(gòu)建當(dāng)前項目的地方尋找父項目的pom,其次在文件系統(tǒng)的這個位置(relativePath位置),然后在本地倉庫,最后在遠(yuǎn)程倉庫尋找父項目的pom。
注意:如果在父項目中通過<modules>指定了子模塊,且子模塊在父項目目錄下,則不需要指定此配置。如果子項目不在父項目的目錄下,應(yīng)該指定此配置。
-->
<relativePath>../pom.xml</relativePath>
</parent>
<!-- 模型版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 公司或者組織的唯一標(biāo)志-->
<groupId>com.companyname.project-group</groupId>
<!-- 項目的唯一ID->
<artifactId>project</artifactId>
<!-- 版本號 -->
<version>1.0</version>
<!--項目產(chǎn)生的構(gòu)件類型,例如jar、war、ear、pom -->
<packaging>jar</packaging>
<!-- 屬性配置 -->
<properties>
<!-- 編譯時的編碼 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<!-- 依賴配置 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<!-- 依賴聲明,不會真正引入包。一般在父pom中進(jìn)行聲明,在子pom中真正引入 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 編譯構(gòu)建相關(guān)配置 -->
<build>
<!-- 插件申明,一般在父pom中聲明,在子pom中真正引入 -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
<!-- 插件引入,在父pom中引入以后,所有子pom中都會引入 -->
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.6.0.1398</version>
</plugin>
</plugins>
</build>
<!-- 針對當(dāng)前項目的遠(yuǎn)程倉庫配置 -->
<repositories>
<repository>
<id>aliyun-public</id>
<url>https://maven.aliyun.com/repository/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<!-- 針對當(dāng)前項目的遠(yuǎn)程插件倉庫配置 -->
<pluginRepositories>
<pluginRepository>
<id>aliyun-public</id>
<url>https://maven.aliyun.com/repository/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
<!--jar包發(fā)布私服配置-->
<distributionManagement>
<!--發(fā)布版本-->
<repository>
<!-- 此ID和setting.xml 中server中配置的服務(wù)器進(jìn)行對應(yīng) -->
<id>maven-releases</id>
<name>releases</name>
<url>http://nexus.maven.cn/repository/maven-releases/</url>
<uniqueVersion>true</uniqueVersion>
</repository>
<!--快照版本-->
<snapshotRepository>
<id>maven-snapshots</id>
<name>snapshots</name>
<url>http://nexus.maven.cn/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<!--動態(tài)構(gòu)建配置,通過設(shè)置活動的profile,profile中的配置會作用于當(dāng)前的項目編譯構(gòu)建 -->
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>四、遠(yuǎn)程倉庫的加載
maven倉庫依賴下載順序:
- 在settings.xml文件中配置的本地倉庫中尋找依賴,沒找到則進(jìn)入第2步。
- 在settings.xml文件中配置的全局遠(yuǎn)程倉庫中尋找,沒找到則進(jìn)入第3步。
- 在當(dāng)前項目的pom.xml中配置的遠(yuǎn)程倉庫中尋找,如果沒找到則進(jìn)入第4步。
- 在中央倉庫 https://repo.maven.apache.org/maven2 中尋找,如果沒找到則拋出依賴無法加載異常。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot Maven Plugin打包異常解決方案
這篇文章主要介紹了Spring Boot Maven Plugin打包異常解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
解析Spring Boot內(nèi)嵌tomcat關(guān)于getServletContext().getRealPath獲取得到臨時
大家都很糾結(jié)這個問題在使用getServletContext().getRealPath()得到的是臨時文件的路徑,每次重啟服務(wù),這個臨時文件的路徑還好變更,下面小編通過本文給大家分享Spring Boot內(nèi)嵌tomcat關(guān)于getServletContext().getRealPath獲取得到臨時路徑的問題,一起看看吧2021-05-05
基于Failed?to?load?ApplicationContext異常的解決思路
這篇文章主要介紹了基于Failed?to?load?ApplicationContext異常的解決思路,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot使用郵箱發(fā)送驗證碼實現(xiàn)注冊功能
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用郵箱發(fā)送驗證碼實現(xiàn)注冊功能實例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02

