下載地址 https://help.sonatype.com/repomanager3/product-information/download,下載好后進行解壓縮會得到兩個文件,nexus-3.39.0-01 和 sonatype-work。可以配置 nexus 變量到我們的環境中方便啟動,也可以不配置每次都到 nexus-3.39.0-01/bin 下去啟動。
NEXUS_HOME: /opt/software/nexus3/nexus-3.39.0-01/
PATH: %NEXUS_HOME%/bin
nexus 默認使用的是8081端口,很多微服務的端口都會從8080等開始使用,可以修改 nexus 的端口,具體位置文件為 /opt/software/nexus3/nexus-3.39.0-01/etc/nexus-default.properties
啟動 nexus 命令
./nexus {start|stop|run|run-redirect|status|restart|force-reload}
# 提示信息
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
# 這個信息需要修改 /opt/software/nexus3/nexus-3.39.0-01/bin/nexus 文件,找到 run_as_root=true,改為如下
run_as_root=false
啟動成功后就可以登錄 nexus 所提供的客戶端界面,登錄的時候會問你要賬號密碼,賬號默認是 admin,密碼在 /opt/software/nexus3/sonatype-work/nexus3/admin.password 文件中,登錄成功后會要求更改密碼,密碼更改后 admin.password 會自動刪除。修改會會讓你選擇嚴格模式,建議允許所有人訪問,畢竟是私服沒太大必要嚴格。
點擊設置、點擊倉庫,我們可以看到倉庫管理配置列表,其中跟 Maven 相關的有 4個,Maven相對有3個Type
- proxy,表示為代理倉庫,下載組件時,如果代理倉庫搜索不到,則把請求轉發到遠程倉庫(默認 https://repo1.maven.org/maven2/,該地址可以修改),并從遠程倉庫下載,然后將該組件緩存到代理庫,當再次請求該組件時,則直接到代理倉庫下載,不會再從遠程倉庫下載。
- hosted
表示宿主倉庫,主要用來部署團隊內部組件,其中 maven-releases 用來部署團隊內部的發布版組件,maven-snapshots用來部署團隊內部的快照版組件。 - group
表示分組倉庫,默認將 maven-central、maven-releases、maven-snapshots三個倉庫組合在一起對外提供服務,簡化了maven客戶端在 setting.xml 或 pom.xml 的配置
修改 maven-central 的 proxy地址,你可以在列表中點擊 maven-central,就會進到 maven-central 的編輯頁,然后在 Remote storage 修改為阿里云的倉庫點擊保存即可。
https://maven.aliyun.com/nexus/content/groups/public/
maven 想使用我們自己搭建的 nexus,只需要在 maven-3.8.4\conf\settings.xml 文件修改鏡像地址即可
<mirrors>
<!-- 阿里云 -->
<!--
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
-->
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://10.240.30.93:9527/repository/maven-public/</url>
</mirror>
</mirrors>
按照以上的操作,我們只新啟動一個項目,并在項目中指定該 maven,此時我們下載的jar就會緩存到 nexus 里,當其他同事使用該項目就會發現該項目的依賴下載為我們 nexus 的部署地址。
但是這還不夠,在實際開發中,除了我們本身使用的第三方依賴外,我們自己也會寫一些依賴包或工具包等,此時若想讓其他同事可以下載并依賴使用,我們就需要把我們制作的jar發布到 nexus 里去。我們先要在我們的 maven 的 settings.xml 中配置在 nexus 的賬號密碼
<servers>
<server>
<!-- 注意id nexus-->
<id>nexus</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
之后我們只需要創建我們的 jar 并添加一些配置即可,相應配置在代碼中有說明
<?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>org.example</groupId>
<artifactId>demoJar</artifactId>
<!-- 后綴 SNAPSHOT 就會把 jar 發布到 nexus repository的 maven-snapshots -->
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- 配置 nexus -->
<distributionManagement>
<repository>
<!-- 這里的id邀約 setting.xml 配置的id相同 -->
<id>nexus</id>
<!-- 配置發布版的名稱與路徑 -->
<name>Nexus Release Repository</name>
<url>>http://10.240.30.93:9527/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Nexus Snapshot Repository</name>
<url>http://10.240.30.93:9527/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
配置完成后,寫好的你的工具類,然后對其進行maven包的發布
然后讓你的同事進行依賴引入,就可以調用你的方法了。
<dependency>
<groupId>org.example</groupId>
<artifactId>demoJar</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
相同版本的jar默認是不能重復發布到 nexus 中的,可以修改你要發布地址的配置,改為 Allow redeploy
如果發現自己上傳的包,確定無誤后無法下載依賴,不管是自己還是別人,那么可能原因是Maven內置的插件遠程倉庫配置,關閉了對SNAPSHOT的支持,防止不穩定的構建。所以解決辦法最關鍵的是:在maven 的conf 目錄下的setting.xml 文件中,添加 對SNAPSHOT的支持
<snapshots>
<enabled>true</enabled>
</snapshots>
在你 maven setting.xml 里加,或者 pom.xml 里加都行
<profiles>
<profile>
<id>central-repo</id>
<repositories>
<repository>
<id>central</id>
<name>Central-repo</name>
<url>http://******/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>central-repo</activeProfile>
</activeProfiles>
如果需要添加兩個可以是
<profile>
<repositories>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>releases</id>
<name>release</name>
<url>http://***********/maven-releases/</url>
</repository>
<repository>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>snapshots</id>
<name>libs-snapshot</name>
<url>http://***************/maven-snapshots/</url>
</repository>
</repositories>
<id>artifactory</id>
</profile>