Maven deploy 部署 jar 到 Nexus 私服


在SOA服務成為標准配置的今天,我們經常會遇到需要將jar上傳到公司Nexus私服來滿足其他服務調用的需求。

常用命令如下:

mvn deploy:deploy-file -DgroupId=<group-id> \
  -DartifactId=<artifact-id> \
  -Dversion=<version> \
  -Dpackaging=<一般是jar> \
  -Dfile=<相對路徑和絕對路徑都可> \
  -Durl=<公司倉庫地址> \
  -DrepositoryId=<一般是snapshots或者releases,根據.m2/settings.xml文件servers配置來> \
  -DpomFile=<pom.xml> \
  -Dsources=<源碼file地址,可不填>

上面這個命令會生成jar並且上傳到Nexus 私服中。

-DpomFile=pom.xml

這個參數指定pom文件為我們自己的pom文件,方便當其他人引入我們的jar的時候把我們的jar包的依賴都一起引入。但是如果我們不指定的話,默認會生成一個空pom.xml,沒有依賴關系,這個時候如果別人引用了我們的 jar 包,就會拋出 NoClassDefFoundError 錯誤,因為編譯時沒有問題,但運行時卻找不到 class 文件。

maven deploy plugin pom 配置

下面提供了maven deploy plugin的xml 配置,方面在snapshots階段快速部署。

<plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <executions>
        <execution>
            <id>deploy-file</id>
            <phase>deploy</phase>
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
                <groupId>com.example</groupId>
                <artifactId>demo</artifactId>
                <version>1.0.0-SNAPSHOT</version>
                <packaging>jar</packaging>
                <file>target/demo-api-1.0.0-SNAPSHOT.jar</file>
                <url>http://nexus.公司.com/nexus/content/repositories/snapshots/</url>
                <repositoryId>snapshots</repositoryId>
                <pomFile>pom.xml</pomFile>
            </configuration>
        </execution>
    </executions>
</plugin>

maven deploy source jar(maven上傳源代碼到Nexus私服)

因為maven部署插件的sources配置是文件,因此我們需要在部署jar之前將 source.jar打包出來,所以將jar-no-fork綁定在verify流程節點上。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar-no-fork</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
            <executions>
                <execution>
                    <id>deploy-file</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy-file</goal>
                    </goals>
                    <configuration>
                        <groupId>com.example</groupId>
                        <artifactId>demo</artifactId>
                        <version>1.0.0-SNAPSHOT</version>
                        <packaging>jar</packaging>
                        <file>target/demo-1.0.0-SNAPSHOT.jar</file>
                        <url>http://nexus.公司.com/nexus/content/repositories/snapshots/</url>
                        <repositoryId>snapshots</repositoryId>
                        <pomFile>pom.xml</pomFile>
                        <sources>target/demo-1.0.0-SNAPSHOT-sources.jar</sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

參考文檔:

  1. deploy-file 插件 官方文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM