在linux上安裝nexus作為私有倉庫並實現上傳下載jar包
最近的專案用到了分散式架構,分散式的好處自然不用多說,但有一個問題就是如何處理公共類或者說工具類,比方說時間格式轉換、生成隨機數、生成訂單號這些開發人員都要用到的函式,不可能讓每個開發人員都維護一個這樣的工具類,因此,想到了利用打包成jar包並上傳到maven倉庫的方式,讓開發人員可以共享公有類。
一、安裝nexus
首先,先下載nexus,nexus是maven倉庫的管理器:
wget https://sonatype-download.global.ssl.fastly.net/nexus/3/nexus-3.6.1-02-unix.tar.gz
下載完之後解壓該檔案:tar zxvf nexus-3.6.1-02-unix.tar.gz
切換到nexus-3.6.1-02/bin檔案目錄下:bin/nexus start
至此,nexus就安裝完成,並且已經在伺服器上啟動。
在瀏覽器上輸入http://ip:8081/(nexus預設的埠號是8081),進入nexus,sign in登入,預設的使用者名稱是admin,密碼是admin123;
二、建立私有倉庫
登入成功之後,點選左上方的小齒輪,選擇Repository->Repostitories->create repository,建立倉庫:
進入下一步,選擇maven2(hosted);
hosted:本地倉庫,通常我們會部署自己的構件到這一型別的倉庫。比如公司的第二方庫。
proxy:代理倉庫,它們被用來代理遠端的公共倉庫,如maven中央倉庫。
group:倉庫組,用來合併多個hosted/proxy倉庫。
進入下一步,輸入倉庫的基本屬性;
點選save,建立倉庫成功。
三、上傳jar包
開啟maven對應的setting.xml,新增如下節點:
開啟需要打包的專案的pom,新增如下節點,url對應的是maven私庫的地址;
<distributionManagement> <repository> <id>maven-releases</id> <url>http://ip+:8081/repository/maven-third-party/releases/</url> </repository> </distributionManagement>
新增打包外掛:
<build>
<plugins>
<!-- 打jar包外掛 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</configuration>
</plugin>
<!-- 打包原始碼外掛 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
配置完成後,在終端輸入:mvn clean source:jar package,進行打包;mvn deploy -e,部署到maven私有庫;
部署成功後,即可在nexus控制面板看到jar包:
四、在其他專案中引入私庫中的jar
新增如下節點,專案會先從私庫中查詢dependency,如果找不到,會從maven的中央倉庫中下載;
<repositories>
<repository>
<id>maven-releases</id>
<name>maven-third-party</name>
<url>http://ip+:8081/repository/maven-third-party/releases/</url>
</repository>
</repositories>
加入依賴:
<dependency>
<groupId>myUtils</groupId>
<artifactId>myUtils</artifactId>
<version>1.0</version>
</dependency>
下載完成後,即可在專案中引用該jar包。