下載地址:http://maven.apache.org/download.cgi,
添加環境變量:MAVEN_HOME
一、配置文件
maven的配置文件主要有 settings.xml 和pom.xml 兩個文件。
1.其中在maven安裝目錄,例如apache-maven-3.8.1\conf目錄下的settings.xml 文件是全局配置文件
2.用戶目錄的.m2子目錄下面的settings.xml的配置只是針對當前用戶的配置
3.項目根路徑下的pom.xml主要是對當前項目的配置。
局部配置優先於全局配置。 配置優先級從高到低:pom.xml> user settings > global settings
二、settings.xml 配置詳解
1.LocalRepository 本地倉庫配置:
<localRepository>D:\repository</localRepository>
2.InteractiveMode 用戶輸入配置:
<interactiveMode>true</interactiveMode>
3.離線模式
<offline>false</offline>
3.插件組,當我們使用某個插件,並且沒有在命令行為其提供組織Id(groupId)的時候,Maven就會使用該列表。默認情況下該列表包含了org.apache.maven.plugins
和org.codehaus.mojo
<pluginGroups> <pluginGroup>org.sonarsource.scanner.maven</pluginGroup> </pluginGroups>
4.私服服務器配置,配置私服的用戶名和密碼。配置的私服服務器可以用來發布jar包,與pom.xml 中 發布標簽distributionManagement 中配置的倉庫ID相互對應。
<servers> <server> <id>maven-releases</id> <username>developer</username> <password>123456</password> <!--文件被創建時的權限。 --> <filePermissions>664</filePermissions> <!--目錄被創建時的權限。 --> <directoryPermissions>775</directoryPermissions> </server> </servers>
5.鏡像配置
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/central</url> <!-- 被鏡像的服務器的id --> <mirrorOf>*</mirrorOf> </mirror>
6.Profiles配置。
settings.xml中的profile元素是pom.xml中profile元素的子集。只包含了id、activation、repositories、pluginRepositories和 properties元素。
如果一個settings.xml中的profile被激活,它的值會覆蓋任何其它定義在pom.xml中帶有相同id的profile。
<profiles> <profile> <id>nexus</id> <!-- 設置默認激活 --> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <!-- 配置依賴倉庫,可以配置多個倉庫,maven會按照順序進行依賴的加載 --> <repository> <id>nexus</id> <name>gwm nexus</name> <url>http://nexus.maven.cn/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 設置插件倉庫 --> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>gwm nexus</name> <url>http://nexus.maven.cn/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <!-- 配置Sonarquebe 代碼掃描插件的全局屬性--> <properties> <sonar.host.url> http://localhost:9000 </sonar.host.url> <sonar.login>admin</sonar.login> <sonar.password>admin</sonar.password> </properties> </profile> </profiles>
7. Activation配置,用來設置profile配置激活的條件邏輯。
<activation> <!--profile默認是否激活的標識 --> <activeByDefault>false</activeByDefault> <!--當匹配的jdk被檢測到,profile被激活。例如,1.4激活JDK1.4,1.4.0_2,而!1.4激活所有版本不是以1.4開頭的JDK。 --> <jdk>1.8</jdk> <!--如果Maven檢測到某一個屬性(其值可以在POM中通過${name}引用),其擁有對應的name = 值,Profile就會被激活。如果值字段是空的,那么存在屬性名稱字段就會激活profile,否則按區分大小寫方式匹配屬性值字段 --> <property> <!--激活profile的屬性的名稱 --> <name>mavenVersion</name> <!--激活profile的屬性的值 --> <value>2.0.3</value> </property> <!--提供一個文件名,通過檢測該文件的存在或不存在來激活profile。missing檢查文件是否存在,如果不存在則激活profile。另一方面,exists則會檢查文件是否存在,如果存在則激活profile。 --> <file> <!--如果指定的文件存在,則激活profile。 --> <exists>${basedir}/file2.properties</exists> <!--如果指定的文件不存在,則激活profile。 --> <missing>${basedir}/file1.properties</missing> </file> </activation>
8.properties 配置,對應profile
的擴展屬性和pom中properties的屬性列表,這些值可以在pom.xml,setting.xml
中使用標記${X}
來使用,這里X是指屬性的名稱。
<!-- 1. env.X: 在一個變量前加上"env."的前綴,會返回一個shell環境變量。例如,"env.PATH"指代了$path環境變量(在Windows上是%PATH%)。 2. project.x:指代了POM中對應的元素值。例如: <project><version>1.0</version></project>通過${project.version}獲得version的值。 3. settings.x: 指代了settings.xml中對應元素的值。例如:<settings><offline>false</offline></settings>通過 ${settings.offline}獲得offline的值。 4. Java System Properties: 所有可通過java.lang.System.getProperties()訪問的屬性都能在POM中使用該形式訪問,例如 ${java.home}。 5. x: 在<properties/>元素中,或者外部文件中設置,以${someVar}的形式使用。 --> <properties>
<project.version>1.0</project.version>
</properties>
9. Repositories 遠程倉庫配置,可以配置多個。可以配置在<settings>標簽中,也可以配置在<profile>標簽中(比較常見,配置在<profile>標簽中可以根據profile的激活情況動態選擇倉庫)。配置形式參見《6.Profiles配置》。
10.插件倉庫pluginRepositories 和repositories相同。
11. 激活profile配置 activeProfiles,用來激活配置的profile。和 activation 配置相比 activeProfiles 配置比較簡單,也比較常用。
<activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles>
三、POM.xml配置文件:
<parent> <!--父項目的構件標識符 --> <artifactId /> <!--父項目的唯一標識符 --> <groupId /> <!--父項目的版本 --> <version /> <!-- 父項目的pom.xml文件的相對路徑。 默認值是../pom.xml。 Maven首先在構建當前項目的地方尋找父項目的pom,其次在文件系統的這個位置(relativePath位置),然后在本地倉庫,最后在遠程倉庫尋找父項目的pom。 注意:如果在父項目中通過<modules>指定了子模塊,且子模塊在父項目目錄下,則不需要指定此配置。如果子項目不在父項目的目錄下,應該指定此配置。 --> <relativePath>../pom.xml</relativePath> </parent> <!-- 模型版本 --> <modelVersion>4.0.0</modelVersion> <!-- 公司或者組織的唯一標志--> <groupId>com.companyname.project-group</groupId> <!-- 項目的唯一ID-> <artifactId>project</artifactId> <!-- 版本號 --> <version>1.0</version> <!--項目產生的構件類型,例如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、test、provided、runtime、system、import;有效期間分別為:編譯測試運行期間、測試期間、編譯測試期間、測試運行期間、
編譯測試期間(jar在本地系統文件中)、引入pom依賴。-->
<scope>compile</scope>
<!-- true表示該依賴是可選的,不會被依賴傳遞 -->
<optional>true</optional>
</dependency> </dependencies> <!-- 依賴聲明,不會真正引入包。一般在父pom中進行聲明,在子pom中真正引入 --> <dependencyManagement> <dependencies> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core</artifactId> <version>${hutool.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- 編譯構建相關配置 --> <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> <!-- 針對當前項目的遠程倉庫配置 --> <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> <!-- 針對當前項目的遠程插件倉庫配置 --> <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包發布私服配置--> <distributionManagement> <repository> <!-- 此ID和setting.xml 中server中配置的服務器進行對應 --> <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> <!--動態構建配置,通過設置活動的profile,profile中的配置會作用於當前的項目編譯構建 --> <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>
四、遠程倉庫的加載
maven倉庫依賴下載順序:
1,在settings.xml文件中配置的本地倉庫中尋找依賴,沒找到則進入第2步。
2,在settings.xml文件中配置的全局遠程倉庫中尋找,沒找到則進入第3步。
3,在當前項目的pom.xml中配置的遠程倉庫中尋找,如果沒找到則進入第4步。
4,在中央倉庫 https://repo.maven.apache.org/maven2 中尋找,如果沒找到則拋出依賴無法加載異常。
鏡像替換:
1,如果在找尋的過程中,如果發現該倉庫有鏡像匹配,則直接從鏡像倉庫中加載。
2,如果倉庫的 id 設置成 <mirrorOf>central</mirrorOf>,則會覆蓋 maven 的中央倉庫配置。
3,如果鏡像 ID 設置為 <mirrorOf>*</mirrorOf> 表示匹配所有的倉庫,則所有依賴只從此鏡像倉庫中下載。
4,如果鏡像ID 設置為 <mirrorOf>repo1,repo2</mirrorOf>,則匹配倉庫repo1和repo2,使用逗號分隔多個遠程倉庫
5,如果鏡像ID設置為 <mirrorOf>*,!repo1</miiroOf>匹配所有遠程倉庫,repo1除外,使用感嘆號將倉庫從匹配中排除
建議將鏡像地址作為一個 普通倉庫repository 進行配置,這樣可以在其他 倉庫下載不了的情況下查找到此倉庫。如果配置了鏡像倉庫代替其他倉庫容易出現在鏡像中找不到依賴,導致項目無法編譯的問題。
附錄:
scope作用域:引用自 https://blog.csdn.net/qq_37269626/article/details/125647272
原文鏈接:https://www.cnblogs.com/hewei-blogs/p/15986452.html