簡體   English   中英

Maven部署jar,依賴於repo

[英]Maven deploy jar with dependencies to repo

我可以在我的pom.xml使用以下命令並運行mvn deploy來部署jar

    <distributionManagement>
    <repository>
        <id>releases</id>
        <url>http://${host}:8081/nexus/content/repositories/releases</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <name>Internal Snapshots</name>
        <url>http://${host}:8081/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
</distributionManagement>

我可以使用以下內容構建一個可執行jar-with-dependencies

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>create-executable-jar</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>my.company.app.Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>

問題是我不知道如何將這些拼接在一起以將可執行jar部署到我的Maven倉庫。 我真的不知道這是通過新插件還是通過向現有程序集插件添加目標或其他步驟來實現的。

如果將程序集綁定到打包階段,則在執行構建時,它將在您的存儲庫中安裝“常規”jar和with-dependencies jar:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>my.company.app.Main</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!--  bind to the packaging phase -->
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

然后只需運行mvn clean install deploy即可將兩個jar上傳到您的存儲庫。

為了構建一個(所謂的)ÜberJAR並使用maven部署它,你也可以使用shade插件 以下代碼來自他們的網站,但我使用此功能制作了一個或兩個項目。

 <project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>jackofall</shadedClassifierName> <!-- Any name that makes sense -->
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

在此配置中,除了普通的JAR之外,您還可以將ÜberJAR作為一個部署。 然后,JAR的用戶可以決定根據分類器提取一體化軟件包或具有依賴關系的JAR。

我通常會使用shade插件來構建ÜberJAR(或以某種方式修改JAR)並使用程序集插件來構建安裝包(包含JAR和其他可能的東西)之類的東西。 我不確定單個插件的預期目標是什么。

以下工作。 我要稍微離開這個問題,因為我不肯定這是最好的做法,但工作是有道理的。

我注意到的問題是我編寫了ID名稱,我不知道這是否是通常的做法,我必須硬編碼jar名稱; 它不是從其他任何東西推斷出來的。

<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
    <execution>
        <id>deploy-executable</id>
        <goals>
            <goal>deploy-file</goal>
        </goals>
        <configuration>
            <file>target/Monitoring-Client-1.0-SNAPSHOT-jar-with-dependencies.jar</file>
        </configuration>
    </execution>
</executions>
</plugin>

基本上我做這件事的困難揭示了我的pom.xml已經pom.xml的事實。 一切都會自行攫取。 我以前做過:

  • 將所有依賴項保存到lib文件夾中
  • 使用類路徑構建一個jar文件夾
  • 使用程序集插件制作另一個可部署的jar

我認為這樣做有幾個原因,特別是當我的庫沒有從我的應用程序中得到很好的考慮時。

但是,通過刪除1和2,所需的只是distributionManagement部分和部署階段自動運行。 總而言之,這是一個通過刪除大量代碼逐字添加功能的驚人案例。

首先,您不應該在部署階段創建ueber jar,最好在打包階段執行此操作。 此外,創建的jar文件通常會自動附加到您的工件,並將轉移到遠程存儲庫(在您的情況下為Nexus)。 如果您只是嘗試執行mvn安裝並查看輸出(如果已創建的jar安裝到本地存儲庫中),則可以檢查此項。 要將結果部署到nexus,您需要調用mvn deploy。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM