一、手動使用maven發佈到nexus倉庫
1、本地開啓nexus2服務
nexus的安裝配置略
瀏覽器訪問:http://127.0.0.1:8888/nexus
2、maven安裝包conf/目錄的settings.xml
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>asdfghjkl</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>asdfghjkl</password>
</server>
<server>
<id>site</id>
<username>deployment</username>
<password>asdfghjkl</password>
</server>
</servers>
3、maven module的pom.xml文件中配置發佈倉庫
<distributionManagement>
<repository>
<id>releases</id>
<url>http://127.0.0.1:8888/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://127.0.0.1:8888/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<server>中的<id>要和<repository>、<snapshotRepository>的<id>一致,maven在發佈時,會根據此id來查找相應的用戶名密碼進行登錄驗證並上傳文件,id不一致會導致發佈失敗!!!
url都是nexus相應倉庫的鏈接地址,這一步做完之後,已經完成了發佈所需要的基本配置。
maven會判斷版本後面是否帶了-SNAPSHOT,如果帶了就發佈到snapshots倉庫,否則發佈到release倉庫。
3、通過Profile參數控制發佈
<groupId>cn.shary</groupId>
<artifactId>fast</artifactId>
<packaging>jar</packaging>
<version>${project.deploy.version}</version>
<properties>
<java.version>1.8</java.version>
<project.deploy.version>1.0.0-SNAPSHOT</project.deploy.version>
</properties>
<profiles>
<profile>
<id>release</id>
<properties>
<project.deploy.version>1.0.0</project.deploy.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id><!--附上源碼-->
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>snapshot</id>
<properties>
<project.deploy.version>0.9.9-SNAPSHOT</project.deploy.version>
</properties>
</profile>
</profiles>
配置完成後就可以在源碼工程根目錄下執行命令行:
mvn deploy -P release
mvn deploy -P snapshot
maven會自動將對應prifile id裏面的project.deploy.version替換全局project.deploy.version屬性,然後執行deploy
說明:通過maven profile變量project.deploy.version來控制需要發佈的版本,用命令mvn deploy -P release,發佈fastdao的1.0.0版本到releases庫。如果使用命令mvn deploy,則默認使用 1.0.0-SNAPSHOT版本號,將發佈fastao的1.0.0-SNAPSHOT版本到snapshots庫。
當然也可以把project.deploy.version當作環境變量來使
使用maven提供的命令去統一更改
mvn versions:set -DnewVersion=1.0.0
nexus配置鏡像:
1. 點擊 repositories
2. nexus3 點擊 create repository-選中maven2(proxy)
nexus2 點擊 Add-選中Add Proxy Repository
3. 設置鏡像參數
如:
阿里雲nexus倉庫URL: http://maven.aliyun.com/nexus/content/groups/public
id:nexus-aliyun
name:nexus-aliyun
4. 設置順序
Repositories
nexus2:Public Repositories-Configuration
nexus3:maven-public
5. 配置settings.xml
<!-- 不使用nexus,maven配置阿里雲加速 -->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<!-- 使用nexus 配置鏡像 -->
<mirror>
<id>nexus-central</id>
<mirrorOf>*</mirrorOf>
<name>Nexus Central</name>
<url>http://192.168.3.5:8888/nexus/repository/maven-public/</url>
</mirror>