下載地址:https://help.sonatype.com/repomanager3/downloadapache
注意:Nexus Repository Manager 3是一個Java服務器應用程序,安裝須要 jdk1.8以上的版本。api
下載解壓後,用命令行到解壓目錄的bin目錄下運行 nexus.exe /run(Linux運行./nexus run),啓動完成後會顯示「Started Sonatype Nexus」:緩存
------------------------------------------------- Started Sonatype Nexus OSS 3.16.2-01 -------------------------------------------------
Nexus管理後臺地址:http://localhost:8081/
點擊右上角Sign in登陸,默認帳號和密碼爲:admin/admin123
服務器
在Repositories 倉庫管理界面中有多種默認的倉庫,也能夠添加新的倉庫,本實例直接使用默認的倉庫:
maven-central,Type爲proxy,表示代理倉庫。代理倉庫用來代理遠程倉庫(maven-central代理的是超級POM中配置的Maven中央倉庫),當在下載組件時,若是代理倉庫搜索不到,則會把請求轉發到遠程倉庫從遠程倉庫下載。從遠程倉庫下載後會緩存到代理倉庫,下次還有該組件的請求則會直接到代理倉庫下載,不會再次請求遠程倉庫。併發
maven-releases/maven-snapshots,Type爲hosted,表示爲宿主倉庫。宿主倉庫主要用來部署團隊內部使用的內部組件,默認的maven-releases和maven-snapshots分別用來部署團隊內部的發佈版本組件和快照版本組件。
maven
配置settings.xml:測試
<settings> <!-- 配置鏡像,此處攔截全部遠程倉庫的請求到代理倉庫--> <mirrors> <mirror> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://localhost:8081/repository/maven-central/</url> </mirror> </mirrors> <!-- 配置遠程庫和遠程插件庫--> <profiles> <profile> <id>nexus</id> <!-- Maven用於填充構建系統本地存儲庫的遠程倉庫集合--> <repositories> <repository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <!-- 相似於repositories元素,指定Maven能夠在哪裏找到Maven插件的遠程倉庫位置--> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <!-- 激活profiles配置 --> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings>
建立Maven項目,pom.xml以下:url
<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.nocoffee</groupId> <artifactId>coffee-api</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>coffee-api</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
執行mvn clean,執行mvn clean須要下載maven-clean-plugin插件,經過Browse界面能夠看到由於執行mvn clean而下載的maven-clean-plugin.jar:
插件
注意:若是界面爲空,表示沒有下載,緣由是以前下載過該插件到本地倉庫,須要把本地倉庫的maven-clean-plugin插件刪除,個人本地倉庫路徑爲D:\Reporsitory,因此須要刪掉文件夾:D:\Reporsitory\org\apache\maven\plugins\maven-clean-plugin,而後從新構建便可。命令行
settings.xml增長以下配置:
<servers> <server> <id>nexus</id> <username>admin</username> <password>admin123</password> </server> </servers>
配置pom.xml:
<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.nocoffee</groupId> <artifactId>coffee-api</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>coffee-api</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <distributionManagement> <repository> <id>nexus</id> <name>maven-releases</name> <url>http://localhost:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>nexus</id> <name>maven-snapshots</name> <url>http://localhost:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> </project>
執行mvn clean deploy將項目打包併發布到宿主倉庫,構建成功後到Browse中maven-snapshots庫查看(由於項目版本爲0.0.1-SNAPSHOT,是帶SNAPSHOT的快照版本):
須要將項目版本改爲發佈版本,在pom.xml中
注意:maven-releases庫默認不能從新發布,須要可從新發布則須要修改該倉庫配置。
測試從新發布到maven-releases庫,執行mvn clean deploy將會構建失敗:
[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.112 s [INFO] Finished at: 2019-06-10T16:34:29+08:00 [INFO] Final Memory: 18M/164M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project coffee-api: Failed to deploy artifacts: Could not transfer artifact com.nocoffee:coffee-api:jar:0.0.1 from/to nexus (http://localhost:8081/repository/maven-releases/): Failed to transfer file: http://localhost:8081/repository/maven-releases/com/nocoffee/coffee-api/0.0.1/coffee-api-0.0.1.jar. Return code is: 400, ReasonPhrase: Repository does not allow updating assets: maven-releases. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
將maven-releases庫中Deployment pollcy改成Allow redeploy既可: