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