天天看點

項目打包釋出到maven私倉常見的幾種方式

前言

在早期沒有使用maven之前,我們引用一些公有jar或者api jar,我們可能會采用這樣的方式,通過手動導入這些jar到項目的classpath路徑進行引用。

有了maven後,我們公司内部可能就會搭建maven私倉比如nexus,然後把這些公有jar或者api jar上傳到nexus私倉,在pom.xml配置一下這些jar的坐标就可以引用。

今天我們的話題就是來聊聊項目打包釋出到maven私倉常見的幾種方

釋出到maven私倉的步驟

1.在maven的settings.xml中< servers >節點配置使用者名和密碼,形如下:

<servers>
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    </servers>
           

注: 其中id可先看做是一個辨別。username和password為nexus私倉的使用者名和密碼

2、指定釋出到nexus私倉的url并進行釋出

方式一:pom.xml檔案添加distributionManagement節點

<distributionManagement>
         <!--正式版本-->
        <repository>
            <!-- 在settings.xml中<server>的id-->
            <id>nexus-releases</id>
            <url>http://192.168.0.11:8081/nexus/content/repositories/releases/</url>
        </repository>

         <!--快照版本-->
        <snapshotRepository>
             <id>nexus-snapshots</id>
             <url>http://192.168.0.11:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
           

注:

如果存在parent,隻需在parent中的pom.xml中配置,沒有則在本項目的pom.xml配置即可

< repository >節點下的< id >對應maven的配置檔案settings.xml檔案中的server的id,兩者必須保持一緻

上傳到私倉的是正式版本還是快照版本,取決于pom.xml檔案version中是SNAPSHOT還是RELEASE。比如你項目中配置如下

<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
           

則上傳到私倉的就是快照版本,最後執行maven的deploy指令進行釋出

方式二:在maven的settings.xml中< profiles >節點配置< properties >,并在< properties >指定 < altSnapshotDeploymentRepository > 和< altReleaseDeploymentRepository >

<profiles>
     <profile>
	 <id>nexus</id>
     <properties>
         <altSnapshotDeploymentRepository>
             nexus-snapshots::default::http://192.168.0.11:8081/repository/maven-snapshots/
         </altSnapshotDeploymentRepository>
         <altReleaseDeploymentRepository>
            nexus-releases::default::http://192.168.0.11:8081/repository/maven-releases/
         </altReleaseDeploymentRepository>
     </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
           

注:

nexus-snapshots和 nexus-releases要和maven的配置檔案settings.xml檔案中的server的id,兩者必須保持一緻

屬性altSnapshotDeploymentRepository和altReleaseDeploymentRepository是随maven-release-plugin 2.8版一起引入的。低于2.8版本,執行mvn deploy時,則會報如下錯誤

Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
           

解決方案就是在釋出的項目中指定一下2.8版本以上的插件,形如下

<build>
        <plugins>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
        </plugins>
    </build>
           

最後再執行maven的deploy指令進行釋出

方式三:通過mvn deploy指定參數

  • 方法一:通過-D參數指定altSnapshotDeploymentRepository和altReleaseDeploymentRepository
mvn deploy 
-DskipTests 
-DaltSnapshotDeploymentRepository=nexussnapshots::default::https://YOUR_NEXUS_URL/snapshots
-DaltReleaseDeploymentRepository=nexus-releases::default::https://YOUR_NEXUS_URL/releases
           

同理上述指令要執行成功,得確定deploy插件是基于2.8版本以上

  • 方法二:通過-D指定要釋出的jar的相關資訊以及私倉位址,私倉id,私倉id要和settings.xml檔案中的server的id保持一緻
mvn deploy:deploy-file 
-DskipTests 
-Dfile=jar封包件位址,絕對路徑  
-DgroupId=組名 
-DartifactId=項目名稱 
-Dversion=版本号 
-Dpackaging=jar 
-DrepositoryId=私庫id(和setting.xml檔案中的server的id保持一緻) 
-Durl=私倉位址
           

方式四:通過nexus的可視化界面進行上傳jar釋出

項目打包釋出到maven私倉常見的幾種方式

這幾種釋出方式的選擇

方式一,通過distributionManagement這種方式釋出,可能是大多數人的選擇。但如果要釋出的項目很多,我們就可以考慮使用方式二,通過在全局的settings檔案配置altSnapshotDeploymentRepository 和altReleaseDeploymentRepository進行釋出,隻需配置一次,所有項目就都可以釋出,無需在多個項目pom指定

方式一和方式二比較适合公司自己内部開發項目,對于一些第三方提供的jar,推薦使用mvn deploy -DrepositoryId=私庫id(和settings.xml檔案中的server的id保持一緻) -Durl=私倉位址的方式或者直接使用nexus可視化界面上傳的方式

繼續閱讀