通過Internet直接從Maven公用倉庫獲取依賴包是默認配置。不過對於中國軟件公司來講,訪問這些公用倉庫通常較慢,對於一些管理嚴格的不能直接上網的軟件公司來講,這更加是不可能的。Maven項目可以配置成僅從Maven私服上獲取依賴。
上一篇已經講述了如何安裝Nexus為Maven私服。下面就假定使用S1服務器上的Nexus服務。可以參考官方文檔:
http://www.sonatype.com/books/nexus-book/reference/config-sect-intro.html
在開發機器上有Maven項目,首先編輯~/.m2/settings.xml.
- <settings>
- <mirrors>
- <mirror>
- <!--This sends everything else to /public -->
- <id>nexus</id>
- <mirrorOf>*</mirrorOf>
- <url>http://S1:8081/nexus/content/groups/public</url>
- </mirror>
- </mirrors>
- <profiles>
- <profile>
- <id>nexus</id>
- <!--Enable snapshots for the built in central repo to direct -->
- <!--all requests to nexus via the mirror -->
- <repositories>
- <repository>
- <id>central</id>
- <url>http://central</url>
- <releases><enabled>true</enabled></releases>
- <snapshots><enabled>true</enabled></snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>central</id>
- <url>http://central</url>
- <releases><enabled>true</enabled></releases>
- <snapshots><enabled>true</enabled></snapshots>
- </pluginRepository>
- </pluginRepositories>
- </profile>
- </profiles>
- <activeProfiles>
- <!--make the profile active all the time -->
- <activeProfile>nexus</activeProfile>
- </activeProfiles>
- </settings>
只需要把S1改成自己的Nexus服務器地址就可以了。如果你的Maven項目的pom.xml中沒有自定義的repository,那么就可以使用了。比如執行mvn clean compile命令,就可從S1上下載pom.xml和jar包。
<profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://172.27.2.203:8081/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://172.27.2.203:8081/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled><updatePolicy>always</updatePolicy></snapshots> </pluginRepository> </pluginRepositories> </profile>
注意,有時候nexus上已經部署了最新的snapshot,但是mvn clean compile就是不取最新版本,這時候需要用-U 參數強制檢查。
mvn clean compile -U
如果還不行,則刪除本地緩存的目錄,在jenkins項目中可以在build之前執行一行命令,比如:
- rm -rf /usr/share/tomcat7/.m2/repository/com/esri/carrier/nginx-factory/