原文出處:http://www.yund.tech/zdetail.html?type=1&id=3688b5fa1f7d7df8b183ad8c9503546d
作者:jstarseven
上一遍博客已經在linux服務器上,搭建好nexus私服了
現在就需要配置setting.xml
和pom.xml
來使nexus作為maven的私服。setting.xml文件在conf下面,pom.xml是在你創建maven項目中的pom.xml中。
一、將jar發送到nexus私服務器
1、創建maven項目
創建一個最簡單的maven項目,然后新建一個工具類,用來測試當把它打成jar包放到私服后,其它項目是否能夠成功引用。
2、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jincou</groupId>
<artifactId>xuxiaoxiao</artifactId>
<!--SNAPSHOT代表是快照版本-->
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xuxiaoxiao</name>
<description>Demo project</description>
<distributionManagement>
<repository>
<!--id的名字可以任意取,但是在setting文件中的屬性<server>的ID與這里一致-->
<id>releases</id>
<!--指向倉庫類型為host(宿主倉庫)的儲存類型為Release的倉庫-->
<url>http://47.96.4.110:8081/repository/java-release/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<!--指向倉庫類型為host(宿主倉庫)的儲存類型為Snapshot的倉庫-->
<url>http://47.96.4.110:8081/repository/java-snapshot/</url>
</snapshotRepository>
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
</project>
3、setting.xml配置
在這里只要配置登陸nexus的用戶名密碼,不然沒有用戶名和密碼怎么能將jar包發送到私服呢。
<!--此處設置的用戶名和密碼都是nexus的登陸配置-->
<servers>
<server>
<id>releases</id> <!--對應pom.xml的id=releases的倉庫-->
<username>xuxiaoxiao</username>
<password>xuxiaoxiao123</password>
</server>
<server>
<id>snapshots</id> <!--對應pom.xml中id=snapshots的倉庫-->
<username>xuxiaoxiao</username>
<password>xuxiaoxiao123</password>
</server>
</servers>
注意
:maven會判斷版本后面是否帶了-SNAPSHOT,如果帶了就發布到snapshots倉庫,否則發布到release倉庫。這里我們可以在pom.xml文件中
執行命令:mvn deploy
發現部署到nexus私服成功,我們到私服查看下,因為這里的版本是帶SNAPSHOT,所以會發布到snapshots倉庫中。
說明已經成功將jar包發布到nexus私服中了。那么下一步是如何引用私服中的jar包了。
二、從nexus引用第三方jar包
讓maven項目使用nexus作為遠程倉庫有兩種方式,第一種
是在項目的pom.xml中進行更改,讓單個項目使用nexus倉庫;另一種
是通過修改maven的配置文件settings.xml進行更改,讓所有項目都使用nexus倉庫。我們這里采取第二種,只需要setting.xml就可以了。還有就是拉取jar的私服倉庫地址只要寫一個java-group就可以了,因為在創建這個組的時候,里面已經包含了其它三個倉庫。
1、setting.xml (完整版)
<?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">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<!--第一個nexus-xu要和下面的mirror中的id一致,代表拉取是也需要進行身份校驗-->
<server>
<id>nexus-xu</id>
<username>xuxiaoxiao</username>
<password>xuxiaoxiao113</password>
</server>
<server>
<!--這兩個前面講過,是jar上傳時候進行的驗證,id對應的是pom中id屬性的值-->
<id>releases</id>
<username>xuxiaoxiao</username>
<password>xuxiaoxiao113</password>
</server>
<server>
<id>snapshots</id>
<username>xuxiaoxiao</username>
<password>xuxiaoxiao113</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus-xu</id>
<name>internal nexus repository</name>
<!--鏡像采用配置好的組的地址-->
<url>http://47.96.44.110:8081/repository/java-group/</url>
<mirrorOf>!internal.repo,*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<!--ID用來確定該profile的唯一標識-->
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>nexus-pr</id>
<!-- 遠程倉庫列表 -->
<repositories>
<repository>
<id>nexus-xu</id>
<name>Nexus Central</name>
<!-- 虛擬的URL形式,指向鏡像的URL-->
<url>http://47.96.44.110:8081/repository/java-group/</url>
<layout>default</layout>
<!-- 表示可以從這個倉庫下載releases版本的構件-->
<releases>
<enabled>true</enabled>
</releases>
<!-- 表示可以從這個倉庫下載snapshot版本的構件 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件倉庫列表 -->
<pluginRepositories>
<pluginRepository>
<id>nexus-xu</id>
<name>Nexus Central</name>
<url>http://47.96.44.110:8081/repository/java-group/</url>
<layout>default</layout>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--需要激活 <profile>中的ID才生效-->
<activeProfile>nexus-pr</activeProfile>
<activeProfile>jdk-1.8</activeProfile>
</activeProfiles>
</settings>
2、驗證
(1)新建項目添加pom依賴
<dependencies>
<dependency>
<groupId>com.jincou</groupId>
<artifactId>xuxiaoxiao</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
(2)看是否拉取到私服的jar包
並沒有報錯,表拉取成功
(3)寫測試類
引用成功
(4)看后台輸出
輸出成功
從這里將jar包發送到私服和從私服拉取jar就成功了。
-END-