下載安裝
提示:安裝之前需要先確認(rèn)好自己需要哪個(gè)版本的maven,避免浪費(fèi)時(shí)間。
官網(wǎng)下載:https://maven.apache.org/download.cgi
歷史版本下載:https://archive.apache.org/dist/maven/maven-3/
maven配置setting.xml
<localRepository\>
該值表示構(gòu)建系統(tǒng)本地倉(cāng)庫(kù)的路徑
<interactiveMode\>
表示maven是否需要和用戶交互以獲得輸入,
如果maven需要和用戶交互以獲得輸入,則設(shè)置成true,反之則應(yīng)為false。默認(rèn)為true。
<usePluginRegistry\>
作用:maven是否需要使用plugin-registry.xml文件來(lái)管理插件版本。
如果需要讓maven使用文件~/.m2/plugin-registry.xml來(lái)管理插件版本,則設(shè)為true。默認(rèn)為false。
<offline\>
作用:表示maven是否需要在離線模式下運(yùn)行。
如果構(gòu)建系統(tǒng)需要在離線模式下運(yùn)行,則為true,默認(rèn)為false。
當(dāng)由于網(wǎng)絡(luò)設(shè)置原因或者安全因素,構(gòu)建服務(wù)器不能連接遠(yuǎn)程倉(cāng)庫(kù)的時(shí)候,該配置就十分有用。
<pluginGroups\>
作用:當(dāng)插件的組織id(groupId)沒有顯式提供時(shí),供搜尋插件組織Id(groupId)的列表。
該元素包含一個(gè)pluginGroup元素列表,每個(gè)子元素包含了一個(gè)組織Id(groupId)。
當(dāng)我們使用某個(gè)插件,并且沒有在命令行為其提供組織Id(groupId)的時(shí)候,Maven就會(huì)使用該列表。
默認(rèn)情況下該列表包含了org.apache.maven.plugins和org.codehaus.mojo。
<servers\>
作用:一般,倉(cāng)庫(kù)的下載和部署是在pom.xml文件中的repositories和distributionManagement元素中定義的。
然而,一般類似用戶名、密碼(有些倉(cāng)庫(kù)訪問是需要安全認(rèn)證的)等信息不應(yīng)該在pom.xml文件中配置,
這些信息可以配置在settings.xml中。
<mirror\>
mirror可以攔截對(duì)遠(yuǎn)程倉(cāng)庫(kù)的請(qǐng)求,改變對(duì)目標(biāo)倉(cāng)庫(kù)的下載地址。 mirror就是鏡像,主要提供一個(gè)方便地切換遠(yuǎn)程倉(cāng)庫(kù)地址的途徑。
當(dāng)maven需要到的依賴jar包不在本地倉(cāng)庫(kù)時(shí),就需要到遠(yuǎn)程倉(cāng)庫(kù)下載,這個(gè)時(shí)候如果maven的setting.xml中配置了鏡像,而且鏡像配置的規(guī)則中匹配到目標(biāo)倉(cāng)庫(kù)時(shí),maven就直接去鏡像中配置的倉(cāng)庫(kù)地址進(jìn)行依賴jar的下載,而不會(huì)去其他的地方進(jìn)行下載。
鏡像攔截規(guī)則 | 說明 |
---|---|
<mirrorOf>*</mirrorOf> | 匹配所有遠(yuǎn)程倉(cāng)庫(kù)。 |
<mirrorOf>external:*</mirrorOf> | 匹配所有遠(yuǎn)程倉(cāng)庫(kù),使用localhost的除外,使用file://協(xié)議的除外。也就是說,匹配所有不在本機(jī)上的遠(yuǎn)程倉(cāng)庫(kù)。 |
<mirrorOf>repo1,repo2</mirrorOf> | 匹配倉(cāng)庫(kù)repo1和repo2,使用逗號(hào)分隔多個(gè)遠(yuǎn)程倉(cāng)庫(kù)。 |
<mirrorOf>*,!repo1</miiroOf> | 匹配所有遠(yuǎn)程倉(cāng)庫(kù),repo1除外,使用感嘆號(hào)將倉(cāng)庫(kù)從匹配中排除。 |
<profiles\>
作用:根據(jù)環(huán)境參數(shù)來(lái)調(diào)整構(gòu)建配置的列表。 settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。
它包含了id、activation、repositories、pluginRepositories和properties元素。這里的profile元素只包含這五個(gè)子元素是因?yàn)檫@里只關(guān)心構(gòu)建系統(tǒng)這個(gè)整體(這正是settings.xml文件的角色定位),而非單獨(dú)的項(xiàng)目對(duì)象模型設(shè)置。如果一個(gè)settings.xml中的profile被激活,它的值會(huì)覆蓋任何其它定義在pom.xml中帶有相同id的profile。
<profiles>
<profile>
<id>test</id> <!-- profile的唯一標(biāo)識(shí) -->
<activation /> <!-- 自動(dòng)觸發(fā)profile的條件邏輯 -->
<properties /> <!-- 擴(kuò)展屬性列表 -->
<repositories /> <!-- 遠(yuǎn)程倉(cāng)庫(kù)列表 -->
<pluginRepositories /> <!-- 插件倉(cāng)庫(kù)列表 -->
</profile>
</profiles>
<activeProfiles\>
作用:手動(dòng)激活profiles的列表,按照profile被應(yīng)用的順序定義activeProfile。
該元素包含了一組activeProfile元素,每個(gè)activeProfile都含有一個(gè)profile id。任何在activeProfile中定義的profile id,不論環(huán)境設(shè)置如何,其對(duì)應(yīng)的profile都會(huì)被激活。如果沒有匹配的profile,則什么都不會(huì)發(fā)生。
例如,env-test是一個(gè)activeProfile,則在pom.xml(或者profile.xml)中對(duì)應(yīng)id的profile會(huì)被激活。如果運(yùn)行過程中找不到這樣一個(gè)profile,Maven則會(huì)像往常一樣運(yùn)行。
<profiles\>子標(biāo)簽<activation\>
作用:自動(dòng)觸發(fā)profile的條件邏輯。
如pom.xml中的profile一樣,profile的作用在于它能夠在某些特定的環(huán)境中自動(dòng)使用某些特定的值;這些環(huán)境通過activation元素指定。
activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id。profile也可以通過在命令行,使用-P標(biāo)記和逗號(hào)分隔的列表來(lái)顯式的激活(如,-P test)。
注:
在maven工程的pom.xml所在目錄下執(zhí)行help:effective-settings,點(diǎn)擊運(yùn)行,控制臺(tái)會(huì)輸出合并、覆蓋之后最終的settings.xml文件
在maven工程的pom.xml所在目錄下執(zhí)行mvn help:active-profiles命令可以查看profile生效情況。
<profiles\>子標(biāo)簽<repositories\>
作用:遠(yuǎn)程倉(cāng)庫(kù)列表,它是maven用來(lái)填充構(gòu)建系統(tǒng)本地倉(cāng)庫(kù)所使用的一組遠(yuǎn)程倉(cāng)庫(kù)。
maven里有兩種倉(cāng)庫(kù),本地倉(cāng)庫(kù)和遠(yuǎn)程倉(cāng)庫(kù)。 遠(yuǎn)程倉(cāng)庫(kù)相當(dāng)于公共的倉(cāng)庫(kù),大家都能看到。
本地倉(cāng)庫(kù)是你本地的緩存副本,只有你看的到,主要起緩存作用。
當(dāng)你向倉(cāng)庫(kù)請(qǐng)求插件或依賴的時(shí)候,會(huì)先檢查本地倉(cāng)庫(kù)里是否有。如果有則直接返回,否則會(huì)向遠(yuǎn)程倉(cāng)庫(kù)請(qǐng)求,并被緩存到本地倉(cāng)庫(kù)。遠(yuǎn)程倉(cāng)庫(kù)既可以在setting.xml中配置也可以在工程的pom.xml文件里指定。
<profiles\>子標(biāo)簽<properties\>
作用:對(duì)應(yīng)profile的擴(kuò)展屬性列表。
maven屬性和ant中的屬性一樣,可以用來(lái)存放一些值。這些值可以在pom.xml中的任何地方使用標(biāo)記${X}來(lái)使用,這里X是指屬性的名稱。屬性有五種不同的形式,并且都能在settings.xml文件中訪問。
注:如果該profile被激活,則可以在pom.xml中使用${user.install}。
<profiles\>子標(biāo)簽<pluginRepositories\>
作用:發(fā)現(xiàn)插件的遠(yuǎn)程倉(cāng)庫(kù)列表。
和repository類似,只是repository是管理jar包依賴的倉(cāng)庫(kù),pluginRepositories則是管理插件的倉(cāng)庫(kù)。
maven插件是一種特殊類型的構(gòu)件。由于這個(gè)原因,插件倉(cāng)庫(kù)獨(dú)立于其它倉(cāng)庫(kù)。pluginRepositories元素的結(jié)構(gòu)和repositories元素的結(jié)構(gòu)類似。每個(gè)pluginRepository元素指定一個(gè)Maven可以用來(lái)尋找新插件的遠(yuǎn)程地址。
示例:
<?xml version="1.0" encoding="UTF-8"?>
<!--
| 官方文檔: https://maven.apache.org/settings.html
| Maven 提供以下兩種 level 的配置:
| 1. User Level. 當(dāng)前用戶獨(dú)享的配置, 通常在 ${user.home}/.m2/settings.xml 目錄下。
| 可在 CLI 命令行中通過以下參數(shù)設(shè)置: -s /path/to/user/settings.xml
| 2. Global Level. 同一臺(tái)計(jì)算機(jī)上的所有 Maven 用戶共享的全局配置。 通常在${maven.home}/conf/settings.xml目錄下。
| 可在 CLI 命令行中通過以下參數(shù)設(shè)置: -gs /path/to/global/settings.xml
| 備注: User Level 優(yōu)先級(jí) > Global Level
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--
| Maven 依賴搜索順序, 當(dāng)我們執(zhí)行 Maven 命令時(shí), Maven 開始按照以下順序查找依賴的庫(kù):
| 步驟 1 - 在本地倉(cāng)庫(kù)中搜索, 如果找不到, 執(zhí)行步驟 2, 如果找到了則執(zhí)行其他操作。
| 步驟 2 - 在中央倉(cāng)庫(kù)中搜索, 如果找不到, 并且有一個(gè)或多個(gè)遠(yuǎn)程倉(cāng)庫(kù)已經(jīng)設(shè)置, 則執(zhí)行步驟 4, 如果找到了則下載到本地倉(cāng)庫(kù)中已被將來(lái)引用。
| 步驟 3 - 如果遠(yuǎn)程倉(cāng)庫(kù)沒有被設(shè)置, Maven 將簡(jiǎn)單的停滯處理并拋出錯(cuò)誤(無(wú)法找到依賴的文件)。
| 步驟 4 - 在一個(gè)或多個(gè)遠(yuǎn)程倉(cāng)庫(kù)中搜索依賴的文件, 如果找到則下載到本地倉(cāng)庫(kù)已被將來(lái)引用, 否則 Maven 將停止處理并拋出錯(cuò)誤(無(wú)法找到依賴的文件)。
|-->
<!-- 地倉(cāng)庫(kù)路徑, 默認(rèn)值: ${user.home}/.m2/repository -->
<localRepository>${user.home}/workspace/env/maven/repository</localRepository>
<!-- 當(dāng) maven 需要輸入值的時(shí)候, 是否交由用戶輸入, 默認(rèn)為true;false 情況下 maven 將根據(jù)使用配置信息進(jìn)行填充 -->
<interactiveMode>true</interactiveMode>
<!-- 離線 artifact 下載、 部署等操作, 默認(rèn): false -->
<offline>false</offline>
<!--
| 搜索插件時(shí), 如果 groupId 沒有顯式提供時(shí), 則以此處配置的 groupId 為默認(rèn)值, 可以簡(jiǎn)單理解為默認(rèn)導(dǎo)入這些 groupId 下的所有 artifact(需要時(shí)才下載)
| 默認(rèn)情況下該列表包含了 org.apache.maven.plugins和org.codehaus.mojo
|
| 查看插件信息:
| mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin:3.5.1 -Ddetail
|-->
<pluginGroups>
<!-- plugin 的 groupId -->
<!--
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<!-- 進(jìn)行遠(yuǎn)程服務(wù)器訪問時(shí)所需的授權(quán)配置信息。通過系統(tǒng)唯一的 server-id 進(jìn)行唯一關(guān)聯(lián) -->
<servers>
<server>
<!-- 這是 server 的 id, 該 id 與 distributionManagement 中 repository 元素的id 相匹配 -->
<id>server_id</id>
<!-- 鑒權(quán)用戶名 -->
<username>auth_username</username>
<!-- 鑒權(quán)密碼 -->
<password>auth_pwd</password>
<!-- 鑒權(quán)時(shí)使用的私鑰位置。和前兩個(gè)元素類似, 私鑰位置和私鑰密碼指定了一個(gè)私鑰的路徑(默認(rèn)是/home/hudson/.ssh/id_dsa)以及如果需要的話, 一個(gè)密鑰 -->
<privateKey>path/to/private_key</privateKey>
<!-- 鑒權(quán)時(shí)使用的私鑰密碼, 非必要, 非必要時(shí)留空 -->
<passphrase>some_passphrase</passphrase>
<!--
| 文件被創(chuàng)建時(shí)的權(quán)限。如果在部署的時(shí)候會(huì)創(chuàng)建一個(gè)倉(cāng)庫(kù)文件或者目錄, 這時(shí)候就可以使用權(quán)限(permission)
| 這兩個(gè)元素合法的值是一個(gè)三位數(shù)字, 其對(duì)應(yīng)了unix文件系統(tǒng)的權(quán)限, 如664, 或者775
|-->
<filePermissions>664</filePermissions>
<!-- 目錄被創(chuàng)建時(shí)的權(quán)限 -->
<directoryPermissions>775</directoryPermissions>
<!-- 傳輸層額外的配置項(xiàng) -->
<configuration></configuration>
</server>
</servers>
<!--
| 從遠(yuǎn)程倉(cāng)庫(kù)下載 artifacts 時(shí), 用于替代指定遠(yuǎn)程倉(cāng)庫(kù)的鏡像服務(wù)器配置;
| 例如當(dāng)您無(wú)法連接上國(guó)外的倉(cāng)庫(kù)是, 可以指定連接到國(guó)內(nèi)的鏡像服務(wù)器;
| pom.xml 和 setting.xml 中配置的倉(cāng)庫(kù)和鏡像優(yōu)先級(jí)關(guān)系(mirror 優(yōu)先級(jí)高于 repository):
| repository(setting.xml) < repository(pom.xml) < mirror(setting.xml)
| 例如, 如果配置了 mirrorOf = *, 則 不管項(xiàng)目的 pom.xml 配置了什么倉(cāng)庫(kù), 最終都會(huì)被鏡像到 鏡像倉(cāng)庫(kù)
| 私服的配置推薦用profile配置而不是mirror
|-->
<mirrors>
<!--
| 【mirro 匹配順序】:
| 多個(gè) mirror 優(yōu)先級(jí) 按照 id字母順序進(jìn)行排列(即與編寫的順序無(wú)關(guān))
| 在第一個(gè) mirror 找不到 artifact, 不會(huì)繼續(xù)超找下一個(gè)鏡像。
| 只有當(dāng) mirror 無(wú)法鏈接的時(shí)候, 才會(huì)嘗試鏈接下一個(gè)鏡像, 類似容災(zāi)備份。
|-->
<!-- 上海交通大學(xué)反向代理 -->
<mirror>
<!-- 該鏡像的唯一標(biāo)識(shí)符, id用來(lái)區(qū)分不同的 mirror 元素, 同時(shí)會(huì)套用使用 server 中 id 相同授權(quán)配置鏈接到鏡像 -->
<id>sjtugmaven</id>
<!-- 鏡像名稱, 無(wú)特殊作用, 可視為簡(jiǎn)述 -->
<name>sjtug maven proxy</name>
<!-- 鏡像地址 -->
<url>https://mirrors.sjtug.sjtu.edu.cn/maven-central/</url>
<!-- 被鏡像的服務(wù)器的id, 必須與 repository 節(jié)點(diǎn)設(shè)置的 ID 一致。但是 This must not match the mirror id
| mirrorOf 的配置語(yǔ)法:
| * = 匹配所有遠(yuǎn)程倉(cāng)庫(kù)。 這樣所有 pom 中定義的倉(cāng)庫(kù)都不生效
| external:* = 匹配除 localhost、使用 file:// 協(xié)議外的所有遠(yuǎn)程倉(cāng)庫(kù)
| repo1,repo2 = 匹配倉(cāng)庫(kù) repo1 和 repo2
| *,!repo1 = 匹配所有遠(yuǎn)程倉(cāng)庫(kù), repo1 除外
|-->
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<!-- 用來(lái)配置不同的代理, 多代理 profiles 可以應(yīng)對(duì)筆記本或移動(dòng)設(shè)備的工作環(huán)境: 通過簡(jiǎn)單的設(shè)置 profile id 就可以很容易的更換整個(gè)代理配置 -->
<proxies>
<!-- 代理元素包含配置代理時(shí)需要的信息 -->
<proxy>
<!-- 代理的唯一定義符, 用來(lái)區(qū)分不同的代理元素 -->
<id>example_proxy</id>
<!-- 該代理是否是激活的那個(gè)。true則激活代理。當(dāng)我們聲明了一組代理, 而某個(gè)時(shí)候只需要激活一個(gè)代理的時(shí)候, 該元素就可以派上用處 -->
<active>false</active>
<!-- 代理的協(xié)議 -->
<protocol>https</protocol>
<!-- 代理的主機(jī)名 -->
<host>proxy.molo.com</host>
<!-- 代理的端口 -->
<port>443</port>
<!-- 代理服務(wù)器認(rèn)證的登錄名 -->
<username>proxy_user</username>
<!-- 代理服務(wù)器認(rèn)證登錄密碼 -->
<password>proxy_pwd</password>
<!-- 不該被代理的主機(jī)名列表。該列表的分隔符由代理服務(wù)器指定;例子中使用了豎線分隔符, 使用逗號(hào)分隔也很常見 -->
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>
<!--
| 構(gòu)建方法的配置清單, maven 將根據(jù)不同環(huán)境參數(shù)來(lái)使用這些構(gòu)建配置。
| settings.xml 中的 profile 元素是 pom.xml 中 profile 元素的裁剪版本。
| settings.xml 負(fù)責(zé)的是整體的構(gòu)建過程, pom.xml 負(fù)責(zé)單獨(dú)的項(xiàng)目對(duì)象構(gòu)建過程。
| settings.xml 只包含了id, activation, repositories, pluginRepositories 和 properties 元素。
|
| 如果 settings 中的 profile 被激活, 它的值會(huì)覆蓋任何其它定義在 pom.xml 中或 profile.xml 中的相同 id 的 profile。
|
| 查看當(dāng)前激活的 profile:
| mvn help:active-profiles
|-->
<profiles>
<profile>
<!-- 該配置的唯一標(biāo)識(shí)符 -->
<id>profile_id</id>
<!--
| profile 的激活條件配置;
| 其他激活方式:
| 1. 通過 settings.xml 文件中的 activeProfile 元素進(jìn)行指定激活。
| 2. 在命令行, 使用-P標(biāo)記和逗號(hào)分隔的列表來(lái)顯式的激活, 如: mvn clean package -P myProfile)。
|-->
<activation>
<!-- 是否默認(rèn)激活 -->
<activeByDefault>false</activeByDefault>
<!-- 內(nèi)建的 java 版本檢測(cè), 匹配規(guī)則: https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html -->
<jdk>9.9</jdk>
<!-- 內(nèi)建操作系統(tǒng)屬性檢測(cè), 配置規(guī)則: https://maven.apache.org/enforcer/enforcer-rules/requireOS.html -->
<os>
<!-- 操作系統(tǒng) -->
<name>Windows XP</name>
<!-- 操作系統(tǒng)家族 -->
<family>Windows</family>
<!-- 操作系統(tǒng) -->
<arch>x86</arch>
<!-- 操作系統(tǒng)版本 -->
<version>5.1.2600</version>
</os>
<!--
| 如果Maven檢測(cè)到某一個(gè)屬性(其值可以在POM中通過${名稱}引用), 并且其擁有對(duì)應(yīng)的名稱和值, Profile就會(huì)被激活。
| 如果值字段是空的, 那么存在屬性名稱字段就會(huì)激活profile, 否則按區(qū)分大小寫方式匹配屬性值字段
|-->
<property>
<!-- 屬性名 -->
<name>mavenVersion</name>
<!-- 屬性值 -->
<value>2.0.3</value>
</property>
<!-- 根據(jù)文件存在/不存在激活profile -->
<file>
<!-- 如果指定的文件存在, 則激活profile -->
<exists>/path/to/active_on_exists</exists>
<!-- 如果指定的文件不存在, 則激活profile -->
<missing>/path/to/active_on_missing</missing>
</file>
</activation>
<!-- 擴(kuò)展屬性設(shè)置。擴(kuò)展屬性可以在 POM 中的任何地方通過 ${擴(kuò)展屬性名} 進(jìn)行引用
|
| 屬性引用方式(包括擴(kuò)展屬性, 共 5 種屬性可以引用):
|
| env.x : 引用 shell 環(huán)境變量, 例如, "env.PATH"指代了 $path 環(huán)境變量(在 Linux / Windows 上是 %PATH% ).
| project.x : 引用 pom.xml(根元素就是 project) 中 xml 元素內(nèi)容.例如 ${project.artifactId} 可以獲取 pom.xml 中設(shè)置的 <artifactId /> 元素的內(nèi)容
| settings.x : 引用 setting.xml(根元素就是 setting) 中 xml 元素內(nèi)容, 例如 ${settings.offline}
| Java System Properties : 所有可通過 java.lang.System.getProperties() 訪問的屬性都能在通過 ${property_name} 訪問, 例如 ${java.home}
| x : 在 <properties/> 或者 外部文件 中設(shè)置的屬性, 都可以 ${someVar} 的形式使用
|
|-->
<properties>
<!-- 在當(dāng)前 profile 被激活時(shí), ${profile.property} 就可以被訪問到了 -->
<profile.property>this.property.is.accessible.when.current.profile.actived</profile.property>
</properties>
<!-- 遠(yuǎn)程倉(cāng)庫(kù)列表 -->
<repositories>
<!--
| releases vs snapshots
| maven 針對(duì) releases、snapshots 有不同的處理策略, POM 就可以在每個(gè)單獨(dú)的倉(cāng)庫(kù)中, 為每種類型的 artifact 采取不同的策略
| 例如:
| 開發(fā)環(huán)境 使用 snapshots 模式實(shí)時(shí)獲取最新的快照版本進(jìn)行構(gòu)建
| 生成環(huán)境 使用 releases 模式獲取穩(wěn)定版本進(jìn)行構(gòu)建
| 參見repositories/repository/releases元素
|-->
<!--
| 依賴包不更新問題:
| 1. Maven 在下載依賴失敗后會(huì)生成一個(gè).lastUpdated 為后綴的文件。如果這個(gè)文件存在, 那么即使換一個(gè)有資源的倉(cāng)庫(kù)后, Maven依然不會(huì)去下載新資源。
| 可以通過 -U 參數(shù)進(jìn)行強(qiáng)制更新、手動(dòng)刪除 .lastUpdated 文件:
| find . -type f -name "*.lastUpdated" -exec echo {}" found and deleted" \; -exec rm -f {} \;
|
| 2. updatePolicy 設(shè)置更新頻率不對(duì), 導(dǎo)致沒有觸發(fā) maven 檢查本地 artifact 與遠(yuǎn)程 artifact 是否一致
|-->
<repository>
<!-- 遠(yuǎn)程倉(cāng)庫(kù)唯一標(biāo)識(shí) -->
<id>maven_repository_id</id>
<!-- 遠(yuǎn)程倉(cāng)庫(kù)名稱 -->
<name>maven_repository_name</name>
<!-- 遠(yuǎn)程倉(cāng)庫(kù)URL, 按protocol://hostname/path形式 -->
<url>http://host/maven</url>
<!--
| 用于定位和排序 artifact 的倉(cāng)庫(kù)布局類型-可以是 default(默認(rèn))或者 legacy(遺留)
| Maven 2為其倉(cāng)庫(kù)提供了一個(gè)默認(rèn)的布局;然而, Maven 1.x有一種不同的布局。我們可以使用該元素指定布局是default(默認(rèn))還是legacy(遺留)
| -->
<layout>default</layout>
<!-- 如何處理遠(yuǎn)程倉(cāng)庫(kù)里發(fā)布版本的下載 -->
<releases>
<!-- 是否允許該倉(cāng)庫(kù)為 artifact 提供 發(fā)布版 / 快照版 下載功能 -->
<enabled>false</enabled>
<!--
| 每次執(zhí)行構(gòu)建命令時(shí), Maven 會(huì)比較本地 POM 和遠(yuǎn)程 POM 的時(shí)間戳, 該元素指定比較的頻率。
| 有效選項(xiàng)是:
| always(每次構(gòu)建都檢查), daily(默認(rèn), 距上次構(gòu)建檢查時(shí)間超過一天), interval: x(距上次構(gòu)建檢查超過 x 分鐘)、 never(從不)
|
| 重要:
| 設(shè)置為 daily, 如果 artifact 一天更新了幾次, 在一天之內(nèi)進(jìn)行構(gòu)建, 也不會(huì)從倉(cāng)庫(kù)中重新獲取最新版本
|-->
<updatePolicy>always</updatePolicy>
<!-- 當(dāng) Maven 驗(yàn)證 artifact 校驗(yàn)文件失敗時(shí)該怎么做: ignore(忽略), fail(失?。? 或者warn(警告) -->
<checksumPolicy>warn</checksumPolicy>
</releases>
<!-- 如何處理遠(yuǎn)程倉(cāng)庫(kù)里快照版本的下載 -->
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
</snapshots>
</repository>
<!--
國(guó)內(nèi)可用的 maven 倉(cāng)庫(kù)地址(updated @ 2019-02-08):
http://maven.aliyun.com/nexus/content/groups/public
http://maven.wso2.org/nexus/content/groups/public/
http://jcenter.bintray.com/
http://maven.springframework.org/release/
http://repository.jboss.com/maven2/
http://uk.maven.org/maven2/
http://repo1.maven.org/maven2/
http://maven.springframework.org/milestone
http://maven.jeecg.org/nexus/content/repositories/
http://repo.maven.apache.org/maven2
http://repo.spring.io/release/
http://repo.spring.io/snapshot/
http://mavensync.zkoss.org/maven2/
https://repository.apache.org/content/groups/public/
https://repository.jboss.org/nexus/content/repositories/releases/
-->
</repositories>
<!--
| maven 插件的遠(yuǎn)程倉(cāng)庫(kù)配置。maven 插件實(shí)際上是一種特殊類型的 artifact。
| 插件倉(cāng)庫(kù)獨(dú)立于 artifact 倉(cāng)庫(kù)。pluginRepositories 元素的結(jié)構(gòu)和 repositories 元素的結(jié)構(gòu)類似。
|-->
<!--
<pluginRepositories>
<pluginRepository>
<releases>
<enabled />
<updatePolicy />
<checksumPolicy />
</releases>
<snapshots>
<enabled />
<updatePolicy />
<checksumPolicy />
</snapshots>
<id />
<name />
<url />
<layout />
</pluginRepository>
</pluginRepositories>
-->
</profile>
</profiles>
<!--
| 手動(dòng)激活 profiles 的列表, 按照 profile 被應(yīng)用的順序定義 activeProfile
| 任何 activeProfile, 不論環(huán)境設(shè)置如何, 其對(duì)應(yīng)的 profile 都會(huì)被激活, maven 會(huì)忽略無(wú)效(找不到)的 profile
|-->
<!--
<activeProfiles>
<activeProfile>not-exits-profile</activeProfile>
</activeProfiles>
-->
</settings>
總結(jié):
mirror可以攔截對(duì)遠(yuǎn)程倉(cāng)庫(kù)的請(qǐng)求,改變對(duì)目標(biāo)倉(cāng)庫(kù)的下載地址
鏡像(Mirroring)是冗余的一種類型,一個(gè)磁盤上的數(shù)據(jù)在另一個(gè)磁盤上存在一個(gè)完全相同的副本即為鏡像。
為什么配置鏡像?
1.一句話,你有的我也有,你沒有的我也有。(擁有遠(yuǎn)程倉(cāng)庫(kù)的所有 jar,包括遠(yuǎn)程倉(cāng)庫(kù)沒有的 jar)
2.還是一句話,我跑的比你快。(有時(shí)候遠(yuǎn)程倉(cāng)庫(kù)獲取 jar 的速度可能比鏡像慢,這也是為什么我們一般要配置中央倉(cāng)庫(kù)的原因,外國(guó)的 maven 倉(cāng)庫(kù)一般獲取速度比較慢)
如果你配置 maven 鏡像不是為了以上兩點(diǎn),那基本就不用配置鏡像了。(私服不用配置鏡像^_^)
注意:當(dāng)遠(yuǎn)程倉(cāng)庫(kù)被鏡像匹配到的,則在 jar 包將從鏡像倉(cāng)庫(kù)獲取,而不是我們配置的 repository 倉(cāng)庫(kù), repository 將失去作用
repository 倉(cāng)庫(kù) 可以配置在pom.xml也可以配置在setting.xml的標(biāo)簽下
如果使用私服,并且涉及到密碼,就需要配置標(biāo)簽,
賬號(hào)密碼如何匹配倉(cāng)庫(kù) server的標(biāo)簽與response的
同理如果使用了私服鏡像,server的標(biāo)簽需要等于mirror的(注:一般使用了私服也不要使用鏡像來(lái)攔截遠(yuǎn)程倉(cāng)庫(kù)了,直接使用response配置的私服地址即可^_^)
私服擴(kuò)展
私服是架設(shè)在局域網(wǎng)的一種特殊的遠(yuǎn)程倉(cāng)庫(kù),目的是代理遠(yuǎn)程倉(cāng)庫(kù)及部署第三方構(gòu)件。 有了私服之后,當(dāng) Maven
需要下載依賴時(shí),直接請(qǐng)求私服,私服上存在則下載到本地倉(cāng)庫(kù);否則,私服請(qǐng)求外部的遠(yuǎn)程倉(cāng)庫(kù),將構(gòu)件下載到私服,再提供給本地倉(cāng)庫(kù)下載。
私服可以解決在企業(yè)做開發(fā)時(shí)每次需要的jar包都要在中心倉(cāng)庫(kù)下載,且每次下載完只能被自己使用,不能被其他開發(fā)人員使用
所謂私服就是一個(gè)服務(wù)器,但是不是本地層面的,是公司層面的,公司中所有的開發(fā)人員都在使用同一個(gè)私服
我們可以使用專門的 Maven 倉(cāng)庫(kù)管理軟件來(lái)搭建私服,比如:Apache Archiva,Artifactory,Sonatype Nexus。
安裝,使用可以參考https://blog.csdn.net/m0_52994499/article/details/120640869
Maven關(guān)聯(lián)私服
配置settings.xml,設(shè)置私服地址、認(rèn)證等信息
<servers>
<server>
<id>nexus-public</id> <!-- nexus的認(rèn)證id -->
<username>admin</username> <!--nexus中的用戶名密碼-->
<password>admin123</password>
</server>
</servers>
<!--使用鏡像攔截,jar包請(qǐng)求先攔截請(qǐng)求阿里云,找不到再去私有倉(cāng)庫(kù)服務(wù)器-->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*,!nexus-public</mirrorOf>
<name>阿里云公共倉(cāng)庫(kù)</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<!--私服配置-->
<repository>
<id>nexus-public</id> <!--nexus認(rèn)證id 【此處的repository的id要和 <server>的id保持一致】-->
<!--name隨便-->
<name>Nexus Release Snapshot Repository</name>
<!--地址是nexus中倉(cāng)庫(kù)組對(duì)應(yīng)的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<!--maven默認(rèn)遠(yuǎn)程倉(cāng)庫(kù)-->
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
<pluginRepositories> <!--插件倉(cāng)庫(kù)地址,各節(jié)點(diǎn)的含義和上面是一樣的-->
<pluginRepository>
<id>nexus-public</id> <!--nexus認(rèn)證id 【此處的repository的id要和 <server>的id保持一致】-->
<!--地址是nexus中倉(cāng)庫(kù)組對(duì)應(yīng)的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>your other profile</activeProfile>
<!-- 使私服配置生效 -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
Maven項(xiàng)目部署到私服
執(zhí)行 :mvn deploy 即可將項(xiàng)目部署到私服對(duì)應(yīng)的倉(cāng)庫(kù)中,此時(shí)項(xiàng)目中的打包方式多為jar
但需要提前在項(xiàng)目的pom.xml中配置部署私服倉(cāng)庫(kù)位置,如下:
...
<dependencies>
.....
</dependencies>
<!-- 在項(xiàng)目的pom.xml中 配置私服的倉(cāng)庫(kù)地址,可以將項(xiàng)目打jar包部署到私服 -->
<distributionManagement>
<repository>
<id>nexus-public</id> <!-- nexus認(rèn)證id -->
<url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>nexus-public</id> <!-- nexus認(rèn)證id -->
<url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
Maven分模塊開發(fā)
1.先創(chuàng)建父工程,pom.xml文件中,打包方式為pom
2.右鍵父工程創(chuàng)建子工程,dao工程和service工程打包方式為jar,web工程打包方式為war
3.每完成一個(gè)模塊后需要install,如果在IDEA中install時(shí)報(bào)錯(cuò)(JDK版本過低),需要在父工程的pom.xml文件中配置如下代碼
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<configuration>
<!--<encoding-->
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
如何查看依賴包到底從哪個(gè)倉(cāng)庫(kù)下載的,可以查看先詳細(xì)日志
mvn clean package -DskipTests docker:build -s /home/xxx/.m2/settings.xml -Dmaven.repo.local=/home/xxx/.m2/repository -f /xxx/.pom.xml
命令說明:
package:打包
install:保存到本地倉(cāng)庫(kù)
-DskipTests:忽略測(cè)試類
docker:build:生成docker鏡像
-s:指定settings文件
-f: 指定pom文件
-Dmaven.repo.local:指定本地倉(cāng)庫(kù)地址
參考
https://blog.csdn.net/m0_51527921/article/details/126580563
https://www.cnblogs.com/iceJava/p/10356309.html
https://blog.csdn.net/m0_52994499/article/details/120640869
https://blog.csdn.net/gaoyaopeng/article/details/114986275
https://blog.csdn.net/abu935009066/article/details/124206273文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-757592.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-757592.html
到了這里,關(guān)于一文徹底搞懂Maven配置(終結(jié)版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!