一.配置從私服下載
從私服下載主要是將 central 庫的下載地址從https://repo1.maven.org/maven2/
修改為私服地址,比如http://localhost:8081/repository/maven-public/
。然后配置好訪問私服的用戶名和密碼即可。
了解settings.xml文件結構
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
- localRepository: 配置本地存儲庫的位置,默認為
${user.home}/.m2/repository
- interactiveMode: 是否與用戶開啟交互模式,默認為 true
- offline: 離線模式,默認為 false
- pluginGroups: 比如
<pluginGroup>org.eclipse.jetty</pluginGroup>
, 默認有org.apache.maven.plugins and org.codehaus.mojo
。 - servers: 配置私服的用戶名和密碼
- mirrors: mirror相當於一個攔截器,它會攔截maven對remote repository的相關請求,把請求里的remote repository地址,重定向到mirror里配置的地址。
- proxies: 代理配置
- profiles: 配置環境
- activeProfiles: 配置默認激活的環境
1-配置用戶名和密碼
<!-- 訪問私服需要的用戶名和密碼 -->
<servers>
<server>
<id>repo-releases</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>repo-snapshots</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
2-配置profile
下面的私服地址是假的。
<!-- 配置 zero-rdc-repo -->
<profile>
<id>me-repo</id>
<repositories>
<!-- 配置的順序決定了下載 jar 包的順序 -->
<!-- 阿里雲的 release 版本 -->
<repository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<!-- 私服的 release 版本 -->
<repository>
<id>repo-releases</id>
<url>https://repo.rdc.aliyun.com/repository/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<!-- 私服的 snapshot 版本 -->
<repository>
<id>repo-snapshots</id>
<url>https://repo.rdc.aliyun.com/repository/snapshot/</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<!-- 阿里雲插件的 release 版本 -->
<pluginRepository>
<id>central</id>
<url>https://maven.aliyun.com/repository/central</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
這里的 repositories 如果不配置的話,默認會有一個 Maven 中央倉庫的配置,同樣 pluginRepositories 中如果沒有配置的話,默認也是有一個 Maven 中央倉庫的配置。
還有!如果 repositories 中沒有配置 repository.id 是 central 的 repository,會自動增加一個 Maven 中央倉庫的配置,並且是以追加的方式,也就是配置在 repositories 的最后一個。所以如果只配置了私服的 repository 情況下,就會先去私服中下載,私服中下載不到時再去追加上來的 Maven 中央倉庫中下載。
3-配置 mirror
<!-- 配置攔截 repository 內的 url 進行重定向 -->
<mirrors>
<!-- 將 central 的請求重定向到阿里雲的公共 Maven 倉庫 -->
<!-- 其它的不重定向到阿里雲 -->
<mirror>
<id>Nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
</mirrors>
這里的 mirror 類似於重定向操作,改變 repository 的 url 屬性。
如果在 repositories 配置了 central 的地址,則這里不配置也可以!!!
注意,這里寫的是central
而不是*
,是因為我們只想把 Maven 中央倉庫的請求重定向到阿里雲上,而不是把所有的請求都重定向到阿里雲上。Maven 中央倉庫僅僅是一個倉庫,打開 https://mvnrepository.com/repos 發現我們經常使用的https://repo1.maven.org/maven2/
僅僅是眾多倉庫中的一個,只不過這個是比較大而全的倉庫而已。如果我們把所有的請求都重定向到這個倉庫,那么就會有依賴找不到。
二. 配置部署到私服
部署到私服就簡單了,在項目中的 pom.xml 文件中加入如下內容后,並將訪問私服的用戶名和密碼配置好即可。
在 pom.xml 中加入配置:
<distributionManagement>
<repository>
<id>local-release</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>local-snapshot</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
然后在 settings.xml 文件中加入訪問私服的用戶名和密碼:
<servers>
<server>
<id>local-release</id>
<username>snail</username>
<password>admin</password>
</server>
<server>
<id>local-snapshot</id>
<username>snail</username>
<password>admin</password>
</server>
</servers>
注意:repository.id 和 server.id 必須是一致的!!!
配置好之后,在項目中執行mvn clean deploy -Dmaven.test.skip
即可部署到私服了。
Q:還有一個問題就是我是部署到 release 庫了還是 snapshot 庫了???
A:根據<version>1.0.0</version>
中的內容是否是以-SNAPSHOT
為結尾的進行區分,如果想發布到 snapshot 庫則必須以-SNAPSHOT
為結尾,否則就發布到了 release 庫。