1. 程式人生 > >Maven學習總結(5)Nexus私服的搭建和使用

Maven學習總結(5)Nexus私服的搭建和使用

對於使用maven來說,私服的好處是顯而易見的。

首先,使用私服會提高jar包載入的速度,當本地jar包沒有時他會查詢私服,私服沒有才會查詢遠端中央倉庫,而私服一般是在區域網內的。

其次,使用私服的安全性比較高,只有私服連線外網的情況下,被攻擊目標就只剩下私服,安全的關注點也就少了。

同時,私服的jar包可以存在公司專案特有的,同事之間可以通過maven共享,而不需要拷貝,十分方便。

Nexus私服的安裝:

連結:https://pan.baidu.com/s/1mbm9mvtmDnd41yYCckGOng 密碼:2rfb

下載 nexus-2.5-bundle.zip 並解壓, 進入如圖所示目錄,找到對應版本的系統資料夾開啟

我這裡在window系統安裝

安裝完成後可以在服務中看到,啟動它

 輸入伺服器地址和埠,埠預設是預設是8081 http://localhost:8081/nexus,出現如下介面啟動成功。

 

 埠號可以在如下目錄修改:

 

然後在你的maven配合檔案中配置一下,連線私服。如圖:

配置訪問私服的使用者名稱密碼以及訪問私服的映象路徑。如圖。 

 

 配置屬性檔案,以及啟用屬性 如圖:

 配置完成的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">
  
  <localRepository>D:\maven\apache-maven-3.5.4-bin\apache-maven-3.5.4\repository</localRepository>
  
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
    <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
	<server>
      <id>snapshots</id>
      <username>deployment</username>
      <password>deployment123</password>
    </server>
  </servers>
  <mirrors>
	<mirror>    
      <id>nexus</id>  
      <name>nexus</name>
	  <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/nexus/content/groups/public</url>     
    </mirror>
  </mirrors>
  <profiles>
   <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>http://repo1.maven.org/maven2/</url>    
          <releases><enabled>true</enabled></releases>
		  <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
	  <pluginRepositories>
		<pluginRepository>
			<id>central</id>
			<url>http://repo1.maven.org/maven2/</url>   
			<releases><enabled>true</enabled></releases>
			<snapshots><enabled>true</enabled></snapshots>
		</pluginRepository>
	  </pluginRepositories>
    </profile>
  </profiles>
 <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

 這樣,私服就可以正常使用了。

配置過程中的路徑 mirriors中的路徑為私服中獲取的組倉庫路徑:如圖:

nexus預設管理員登入賬號密碼為admin/admin123

如何上傳jar包到第三方倉庫:

 

 

 

 上傳成功後可以通過這樣獲取依賴的座標,直接使用。

 shang

 下載私服中的jar包,直接通過配置pom.xml 檔案依賴,指定需要下載依賴的座標,ide會自動載入,你也可以重建專案。

最後,是配置將自己的專案打包部署到私服的的配置程式碼,需要在pom.xml 中配置

<distributionManagement>
    <repository>
      <id>releases</id>
      <name>Internal Releases</name>
      <url>http://localhost:8081/nexus/content/repositories/releases</url>
    </repository>
    <snapshotRepository>
      <id>snapshots</id>
      <name>Internal Snapshots</name>
      <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
  </distributionManagement>

 url還是通過訪問私服獲取