前言
搭建私服后,利用maven進(jìn)行jar包的上傳下載是必不可少的環(huán)節(jié),本文將通過講解pom文件和setting.xml文件的配置方法來介紹如何進(jìn)行jar包在私服的上傳和下載。
一、將jar包上傳到maven私服
1. 配置maven的settings.xml文件,在servers標(biāo)簽中配置server
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
這里的話,id并不需要一定和私服中的目錄名保持一致,只要和pom文件的配置對應(yīng)即可。
2. 配置項(xiàng)目的pom.xml文件
這里要注意,下面兩處的id需要和步驟1的settings.xml配置保持一致
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8079/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8079/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
3. 執(zhí)行mvn deploy 命令
mvn deploy
執(zhí)行后,在nexus頁面上看是否上傳成功

二、下載私服上的jar包到本地倉庫
1. 在maven的settings.xml中配置下載模板
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>nexus</id>
<!--倉庫地址,即nexus倉庫組的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下載releases構(gòu)件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下載snapshots構(gòu)件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件倉庫,maven的運(yùn)行依賴插件,也需要從私服下載插件 -->
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
這里的id并不是固定的,只要與activeProfiles標(biāo)簽中的值進(jìn)行對應(yīng)就(activeProfiles標(biāo)簽下面會(huì)說到)
2. 配置激活下載模板
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
3. 在項(xiàng)目中引用本地倉庫沒有的jar即可觀察到
新的jar包是通過我們的私服進(jìn)行下載的(由于私服是新建的,所以私服會(huì)向中央倉庫下載)


下載完成后,我們可以在我們私服的center目錄下看到我們剛剛下載的包

三、將第三方j(luò)ar包安裝到本地倉庫和maven私服
我們在pom文件配置了某個(gè)jar的坐標(biāo)后,如果本地倉庫沒有的話,就會(huì)先在我們的私服上面找jar包,如果私服也沒有,就會(huì)到中央倉庫找jar包。但是并不是所有的jar都可以在中央倉庫上面下載得到,比如常用的Oracle數(shù)據(jù)庫驅(qū)動(dòng)的jar包在中央倉庫就不存在。這個(gè)時(shí)候就需要我們自己到對應(yīng)的官網(wǎng)上下載jar包上傳到我們的本地和私服倉庫上。
(一)將第三方j(luò)ar安裝到本地?cái)?shù)據(jù)庫
1. 下載第三方j(luò)ar到本地
2. 使用mvn install命令安裝到本地倉庫
mvn install:install-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 –Dversion=10.2.0.4.0 -Dpackaging=jar
-Dfile表示要安裝的jar包名
-DgroupId表示生成的組織名
-DartifactId表示生成的項(xiàng)目id
-Dversion表示版本號
-Dpackaging表示生成的jar的格式
(二)將第三方的jar安裝到私服上
步驟和安裝到本地差不多,只是私服涉及到網(wǎng)絡(luò)傳輸,需要我們配一下url上傳地址
1. 下載第三方j(luò)ar包到本地
2. 在maven的settings.xml配置文件中配置,id只要和下面的命令匹配就行
<server>
<id>thirdparty</id>
<username>admin</username>
<password>admin123</password>
</server>
3. 執(zhí)行mvn deploy命令上傳即可
mvn deploy:deploy-file -Dfile=ojdbc14-10.2.0.4.0.jar -DgroupId=com.oracle -DartifactId=ojdbc14 –Dversion=10.2.0.4.0 -Dpackaging=jar –Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty