使用功能包括 代理中央倉庫、Snapshot包的管理、Release包的管理、第三方Jar上傳到Nexus上、從nexus下載依賴jar
代理中央倉庫
只要在PMO文件中配置私服的地址即可(獲取jar包信息走私服,然後私服保存的有中央倉庫jar包信息),配置如下:
<repositories>
<repository>
<id>maven-central</id>
<name>maven-central</name>
<url>http://localhost:8081/repository/maven-central/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
Snapshot包的管理
修改Maven的settings.xml文件,加入認證機制
<!--可以設置多個server-->
<servers>
....
<server>
<id>mynexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
修改工程的pom文件
<!--分發管理-->
<distributionManagement>
<!--snapshot-->
<snapshotRepository>
<id>mynexus</id>
<name>Nexus Snapshot</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意:pom文件中 distributionManagement/snapshotRepository/id 和 settings.xml文件中的 servers/server/id 必須保持一致,都是mynexus
上傳到Nexus上
1、maven項目 pom文件version是-SNAPSHOT結尾;maven 的packaging爲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>com.nexus</groupId>
<artifactId>nexue01</artifactId>
<version>1.1.0-SNAPSHOT</version>
2、發佈
命令行切換到項目的pom.xml文件所在目錄。執行 mvn deploy
如果使用idea開發,直接點擊deploy
3、因爲Snapshot是快照版本,默認他每次會把Jar加一個時間戳,做爲歷史備份版本。
Releases包的管理
1、與Snapshot大同小異,只是上傳到私服上的Jar包不會自動帶時間戳
2、與Snapshot配置不同的地方,就是工程的PMO文件,加入repository配置
<!--分發管理-->
<distributionManagement>
<!--releases-->
<repository>
<id>mynexus</id>
<name>Nexus releases</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>
3、打包的時候需要把Snapshot去掉
<?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.nexus</groupId>
<artifactId>nexue01</artifactId>
<version>1.1.0</version>
第三方Jar上傳到Nexus上
mvn deploy:deploy-file -DgroupId=junit -DartifactId=junit -Dversion=4.11 -Dpackaging=jar -Dfile=D:\mvn\repository\junit\junit\4.11\junit-4.11.jar -Durl=http://localhost:8081/repository/maven-third-party/ -DrepositoryId=mynexus
-DrepositoryId=mynexus 對應的就是上面配置的Maven中settings.xml servers/server/id的認證配的名字。在nexus 3中無法在nexus3後臺自行上傳,這個功能已經被幹掉。
從nexus下載依賴jar
1、對項目獨立設置:
- 打開項目的 pom.xml 文件:
- 添加下面內容:
<!--配置私服-->
<repositories>
<repository>
<id>maven-public</id>
<name>maven-central</name>
<url>http://localhost:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
2、對全局配置進行設置:
- 打開 maven 的 settings.xml 文件:
- 添加下面內容:
<!-- 倉庫鏡像 -->
<mirrors>
<mirror>
<id>mynexus</id>
<name>mynexus public</name>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>