詳解maven配置多倉庫的方法示例
剛接觸maven就是在公司里配置好的,所以一直以來使用都沒毛病,所以一直沒有去動這些固有的東西。
但是,后來把公司的電腦拿回家之后,發(fā)現(xiàn)有的東西就搞不起來了。原因也看一下就明白了,因為在公司的時候用的是公司的maven私服,所以回家后,用不了也是正常。
但是,真的脫離了公司,自己就不能工作了嗎?不可能吧。 難道一下開源工具都必須要依賴于公司的網(wǎng)絡? 這明顯是不合理的。
那么,就扯出本次文章的意義了,在家里,自然是要公有的maven倉庫了,那么,怎樣配置maven倉庫才能讓自己用起來順心呢?
1. 改掉原有的maven倉庫地址,讓maven從公網(wǎng)上摘取jar包下載,方便、快捷。
原私有配置示例如下:
<?xml version="1.0" encoding="UTF-8"?> <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"> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository --> <localRepository>${user.home}/.m2/repository</localRepository> <!--pluginGroups></pluginGroups--> <!--proxies></proxies--> <servers> <server> <id>releases</id> <username>admin</username> <password>123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>123</password> </server> </servers> <pluginRepositories> <pluginRepository> <id>mypublic</id> <name>Public</name> <url>http://test.nexus.com/nexus/content/groups/public/</url> </pluginRepository> </pluginRepositories> <mirrors> <mirror> <id>central</id> <name>internal</name> <url>http://test.nexus.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </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> <updatePolicy>always</updatePolicy> </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>
如果想直接把私有的地方干掉,那么,這是最快的,直接把mirror的url改掉就行了,如:
<mirrors> <mirror> <id>central</id> <name>internal</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <!-- <url>http://test.nexus.com/nexus/content/groups/public/</url> --> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
當然了,到需要的地方,再把這個地址改回來就可以了,這可能是改動最小的方法了。但是也很惱火的一種配置方式,因為你要不時地記得切換(誰有那閑心)!?。?/p>
2. 添加一個類似結構的倉庫配置,這樣的話就不切來切去的了,一勞永逸。
相當于添加了多倉庫,如下:
<mirrors> <!-- 再添加一個mirror, 注意mirrorsOf 為 * --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> <repositories> <!-- 添加一個 repository --> <repository> <id>alimaven</id> <url>http://alimaven</url> <releases><enabled>true</enabled></releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories>
這樣的話,就不用再切換了。但是,這會導致一種情況,在某環(huán)境不可用時,maven下載jar將會很慢,嚴重影響心情,所以,其實是不建議這么干的。
3. 按照最簡單的方式,新增一個倉庫地址,隨時切換。
不用去添加mirror了,直接以url的形式,配置到reponsitory里即可,如下:
<repository> <!-- 直接添加一個 repository,在 activeProfiles 里加上此id 即可 --> <id>repo1</id> <name>org.maven.repo1</name> <layout>default</layout> <url>https://repo1.maven.org/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository>
<activeProfiles> <activeProfile>nexus</activeProfile> <!-- 添加此屬性,以便激活repo1的配置 --> <activeProfile>repo1</activeProfile> </activeProfiles>
這樣,既不影響原來的結構,也不影響現(xiàn)在使用,在家的時候,可以將私有倉庫注釋掉,以提高訪問速度。
注意: 最后一個 activeProfiles 的屬性是必須的,否則你可能發(fā)現(xiàn)你改了配置,然而并沒有什么卵用!
<activeProfiles> <!-- 放心,此處的 nexus 是多個倉庫的配置 --> <activeProfile>nexus</activeProfile> </activeProfiles>
4. 無法拉取包的困惑?你可能發(fā)現(xiàn),你的maven無法拉取 SNAPSHOT 包,然而包明明就在那里!
是的,出于安全的考慮,maven 默認是不會去使用 snapshot 包的,所以,如果你有需要使用 snapshot 包(很多公司可能大量使用),那么你就需要配置 SNAPSHOT 為允許了!
<repository> <id>central</id> <url>http://central</url> <releases> <enabled>true</enabled> </releases> <!-- 如下開啟 snapshots 功能 --> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository>
5.一個完整的配置樣例參考
前面說的基本是從解決問題的思路去講解,可能還是不夠形象,或者每個人的理解總是有些差異,下面來一個完整的配置參考。你可以在這上面繼續(xù)任意發(fā)揮:
<?xml version="1.0" encoding="UTF-8"?> <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"> <localRepository>${user.home}/.m2/repository</localRepository> <servers> <!-- 賬號信息保留,私服使用 --> <server> <id>sec@public-snapshots</id> <username>snapshot_user</username> <password>snapshot123</password> </server> <server> <id>mirror-sec</id> <username>snapshot_user</username> <password>snapshot123</password> </server> </servers> <!-- 注釋掉該mirror 配置項 --> <!-- <mirrors> <mirror> <id>mirror-sec</id> <mirrorOf>sec@public-snapshots,mirror-sec</mirrorOf> <url>http://maven.sec-ins.com.cn/repository/maven-public/</url> </mirror> </mirrors> --> <profiles> <profile> <id>sec</id> <!-- 注釋掉該默認激活,統(tǒng)一在 activeProfiles 中配置--> <!-- <activation> <activeByDefault>true</activeByDefault> </activation> --> <repositories> <!-- 直接添加一個 repository, 運行maven更新時,就會先嘗試使用該repo進行拉取了 可添加任意多個倉庫,但如果網(wǎng)絡不通,這樣會導致很長時間的切換重試,可在ide中查看maven正在嘗試哪個repository --> <repository> <id>repo1</id> <name>org.maven.repo1</name> <layout>default</layout> <url>https://repo1.maven.org/</url> <!-- <url>http://maven.aliyun.com/nexus/content/groups/public</url> --> <snapshots> <enabled>false</enabled> </snapshots> </repository> <!-- 公司的配置保留,添加自定義的倉庫即可 --> <repository> <id>sec@public-snapshots</id> <name>sec-snapshots</name> <url>http://maven.sec-ins.com.cn/repository/maven-public</url> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile> <!-- 注釋掉該plugin 配置 --> <!-- <pluginRepositories> <pluginRepository> <id>sec@public-snapshots</id> <name>sec_plugin</name> <url>http://maven.sec-ins.com.cn/repository/maven-public</url> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> --> <!-- 你也可以另開一個 profile, 保存自定義的 repositories, 在 activeProfiles 里加上此id 即可遍歷以下 repository --> <profile> <id>my-profile</id> <repositories> <repository> <id>alirepo1</id> <name>org.maven.repo1</name> <layout>default</layout> <!-- <url>https://repo1.maven.org/</url> --> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>repository.springframework.maven.release</id> <name>Spring Framework Maven Release Repository</name> <url>http://maven.springframework.org/milestone/</url> </repository> <repository> <id>org.springframework</id> <url> http://maven.springframework.org/snapshot</url> </repository> <repository> <id>spring-milestone</id> <name>Spring Maven MILESTONE Repository</name> <url>http://repo.spring.io/libs-milestone</url> </repository> <repository> <id>spring-release</id> <name>Spring Maven RELEASE Repository</name> <url>http://repo.spring.io/libs-release</url> </repository> </repositories> </profile> </profiles> <activeProfiles> <!-- 添加此屬性,需要激活自定義的repo 的profile配置 --> <activeProfile>sec</activeProfile> <activeProfile>my-profile</activeProfile> </activeProfiles> </settings>
6. 另附一個idea maven配置的方法
maven作為基礎輔助工具,雖不需去深入的理解,也沒有高深的技巧,但是作為沒有處理過幾個相關問題的同學,還是很有必要了解的。
到此這篇關于詳解maven配置多倉庫的方法示例的文章就介紹到這了,更多相關maven配置多倉庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot實現(xiàn)文件的上傳、下載和預覽功能
在Spring Boot項目中實現(xiàn)文件的上傳、下載和預覽功能,可以通過使用Spring MVC的MultipartFile接口來處理文件上傳,并使用HttpServletResponse或Resource來實現(xiàn)文件下載和預覽,下面是如何實現(xiàn)這些功能的完整示例,需要的朋友可以參考下2024-08-08Spring?Boot整合Log4j2.xml的問題及解決方法
這篇文章主要介紹了Spring?Boot整合Log4j2.xml的問題,本文給大家分享解決方案,需要的朋友可以參考下2023-09-09spring boot中xalan引入報錯系統(tǒng)找不到指定的文件原因分析
這篇文章主要介紹了spring boot中xalan引入報錯系統(tǒng)找不到指定的文件,主要原因是內(nèi)嵌的tomcat9.0.36,本文給大家分享最新解決方法,需要的朋友可以參考下2023-08-08關于request.getRequestDispatcher().forward()的妙用及DispatcherType
這篇文章主要介紹了關于request.getRequestDispatcher().forward()的妙用及DispatcherType對Filter配置的影響,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01