先說點理論:
倉庫分類:
- hosted(宿主):宿主倉庫主要用於存放項目部署的構件、或者第三方構件用於提供下載。
- proxy(代理):代理倉庫就是對遠程倉庫的一種代理,從遠程倉庫下載構件和插件然後緩存在Nexus倉庫中
- group(倉庫組):倉庫的一種組合策略,並不存在實在意義的依賴,只是作爲一種中轉站的作用存在。
Nexus內置倉庫類型
- maven-central:代理中央倉庫、策略爲Release、只會下載和緩存中央倉庫中的發佈版本構件。
- maven-releases:策略爲Release的宿主倉庫、用來部署組織內部的發佈版本內容。
- maven-snapshots:策略爲Snapshot的宿主倉庫、用來部署組織內部的快照版本內容。
- maven-public:該倉庫將上述所有策略爲Release的倉庫聚合並通過一致的地址提供服務。
- nuget-hosted:用來部署nuget構件的宿主倉庫
- nuget.org-proxy:代理nuget遠程倉庫,下載和緩衝nuget構件。
- nuget-group:該倉庫組將nuget-hosted與nuget.org-proxy倉庫聚合並通過一致的地址提供服務。
- maven-public:該倉庫組將maven-central,maven-releases與maven-snapshots倉庫聚合並通過一致的地址提供服務。
1、下載nexus3.10
官網鏈接:https://www.sonatype.com/nexus-repository-oss
第一個是mac版本, 第二個是windows版本, 第三個是unix版本.
可以選擇windows版和linux版,兩者區別不大,以windows爲例。
2、配置啓動
下載解壓文件後:配置bin目錄下nexus.vmoptions文件,適當調整內存參數,防止佔用內存太大
就改這三項
etc目錄下nexus-default.properties文件可配置默認端口和host及訪問根目錄。
linux:bin目錄下執行sh nexus start啓動服務,sh nexus stop停止服務
windows: bin目錄下,執行 nexus.exe /run就可以啓動了
地址欄訪問 localhost:8081,每次啓動服務需要等待一會纔可以打開
3、用戶登錄
默認登錄是遊客身份:anonymous,可以查看倉庫並下載依賴,但不能配置nexus
使用默認管理員身份登錄,帳號:admin,密碼:密碼:D:\mavenservice\sonatype-work\nexus3\admin.password 中
第一次進去需要修改密碼. 這個文件會自動再刪除的.
4.創建一個倉庫
選擇maven2(hosted)
version policy 這邊決定創建的蒼鷺存放是發佈版本還是快照版本的建構, 當然可以選擇混合
Deployment policy 這邊決定發佈策略, 選擇可以重新發布構建策略
這樣就完成了
5.發佈jar包到倉庫
新建一個common-demo項目,發佈jar包出去
修改本地的maven 的 配置conf 文件下 的settings.xml 文件:
添加自己的服務器. 其實就是配置登錄的賬號密碼, 我自己手動修改了密碼爲123
<servers>
<!-- 自己的服務器 -->
<server>
<id>nexus-release</id>
<username>admin</username>
<password>123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>123</password>
</server>
</servers>
Pom配置:
<?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.onyx</groupId>
<artifactId>common-demo</artifactId>
<version>1.0.0.release</version>
<packaging>jar</packaging>
<distributionManagement>
<repository>
<id>nexus-release</id>
<url>http://127.0.0.1:8081/repository/demo3/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://127.0.0.1:8081/repository/demo3/</url>
</snapshotRepository>
<!-- 這邊url就是倉庫的路徑 -->
</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>
</project>
一個java工具類
package util;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.CRC32;
/**
* @author zk
* @Description: CRC32 工具類, 從一個源,獲取crc32編碼
* @date 2019年10月15日14:10:06
*/
public class CRC32Util {
private static final int BUFFER_SIZE = 512;
private static final Long DEFAULT_CODE = 0L;
private CRC32Util() {
}
/**
* 編碼
*
* @param data
* @return
*/
public static long encode(String data) {
if (data == null || data.length() == 0) {
return DEFAULT_CODE;
}
return encode(data.getBytes());
}
/**
* 編碼
*
* @param data
* @return
*/
public static long encode(byte[] data) {
if (data == null || data.length == 0) {
return DEFAULT_CODE;
}
CRC32 crc32 = new CRC32();
crc32.update(data, 0, data.length);
return crc32.getValue();
}
/**
* 編碼
*/
public static long encode(InputStream data) {
if (data == null) {
return DEFAULT_CODE;
}
try {
byte[] buffer = new byte[BUFFER_SIZE];
int read = data.read(buffer, 0, BUFFER_SIZE);
CRC32 crc32 = new CRC32();
while (read > -1) {
crc32.update(buffer, 0, read);
read = data.read(buffer, 0, BUFFER_SIZE);
}
return crc32.getValue();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
執行命名 mvn clean package deploy
6.引用jar包
再次新建一個maven-demo的項目,引用jar包
其最後的pom文件是:
<?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.onyx</groupId>
<artifactId>maven-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>nexus-release</id>
<name>nexus-release</name>
<url>http://127.0.0.1:8081/repository/demo3/</url>
<!--<layout>default</layout>-->
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.onyx</groupId>
<artifactId>common-demo</artifactId>
<version>1.0.0.release</version>
</dependency>
</dependencies>
</project>
測試類:
package com.onyx;
import util.CRC32Util;
/**
* @author zk
* @Description:
* @date 2019-10-21 15:33
*/
public class Test {
public static void main(String[] args) {
CRC32Util.encode("123");
}
}
當新建兩個倉庫一個是release的版本發佈, 另外一個作爲snapshot的版本發佈的時候的配置
只需要修改common-demo的倉庫
<distributionManagement>
<repository>
<id>nexus-release</id>
<url>http://127.0.0.1:8081/repository/demo3/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://127.0.0.1:8081/repository/demo4/</url>
</snapshotRepository>
<!-- 這邊url就是倉庫的路徑 -->
</distributionManagement>
注意是兩個不同的url
在maven-demo的項目引用中, 修改爲:
分別配置相關的兩個配置, 千萬別寫錯了
<repositories>
<repository>
<id>nexus-release</id>
<name>nexus-release</name>
<url>http://127.0.0.1:8081/repository/demo3/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>nexus-snapshots</id>
<name>nexus-snapshots</name>
<url>http://127.0.0.1:8081/repository/demo4/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
就可以了. 把 release 和 snapshot 的倉庫分開