簡體   English   中英

maven使用簡單的命令行安裝和部署第三方依賴項

[英]maven install and deploy 3rd party dependencies with simple command line

我們有許多不在任何地方托管的第三方依賴項。 對於其中的每一個,我們都有一個jar文件,我們希望能夠安裝和/或部署到我們的存儲庫。 一些jar文件有自己的依賴項,我們還需要聲明這些。

我們為每個聲明groupId,artifactId,依賴項等的jar文件制作了pom.xml文件。這些pom.xml文件都有一個共同的父pom,它聲明了一些常見信息(例如<repositories><distributionManagement> )。

我希望能夠安裝或部署這些依賴項,例如mvn installmvn deploy (或者mvn install:install-filemvn deploy:deploy-file ),並具有這些命令的所有必要屬性( artifactIdrepositoryId等)可以從pom.xml文件中讀取。

為了實現這一點,至少在部署時,我嘗試將以下內容放在我的父pom中:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>2.4</version>
      <configuration>
        <file>${jarfile}</file>
        <pomFile>pom.xml</pomFile>
        <repositoryId>our-id</repositoryId>
        <url>our-url</url>
      </configuration>
    </plugin>
  </plugins>
</build>

然后讓每個子poms定義jarfile屬性。 這允許我運行mvn deploy:deploy-file來部署所有子pom工件。 據推測,我可以做類似的事情來獲得mvn install:install-file

但是使用這種方法,我無法釋放父pom(我必須這樣做,因為孩子poms依賴它),如果我嘗試mvn release:perform在父pom上mvn release:perform ,我會得到如下錯誤:

Cannot override read-only parameter: pomFile

我覺得我可能會以錯誤的方式解決這個問題。 我真正想做的就是:

  • 將所有第三方jar的公共代碼放在一個共享父pom中
  • 為每個第三方jar寫一個額外的最小pom
  • 能夠運行mvn installmvn deploy類的東西,而無需指定所有那些復雜的命令行屬性

我怎樣才能做到最好?

編輯:更清楚地說,我希望能夠運行像mvn installmvn deploy這樣簡單的東西,而不必在命令行上指定屬性。

當Maven缺少依賴項時,它會在輸出中給出一個錯誤,其中包含要將jar添加到遠程存儲庫的命令行。 該命令行將自動為您創建一個POM並將兩者一起上傳。 這不足以滿足您的需求嗎?

使用這個工具,如果我知道我有一個沒有遠程存儲庫表示的依賴項,我通常只需繼續使用我想要的GAV在我的構建中定義它,運行一次構建,然后查看構建錯誤。 復制出部署命令,然后運行該命令。 您需要確保在父POM中正確設置<repository>元素,並使用存儲庫上載密碼在settings.xml相應的<server>元素。 除此之外,你應該好好去。

我想補充一點,你應該在到達遠方之前查看Nexus。 現在建立起來是值得的麻煩! :-)

好的,我找到了一個解決方案,允許我只運行mvn installmvn deploy ,並將jar文件安裝到本地或遠程存儲庫。 靈感來自maven-users列表中的帖子並使用build-helper插件 ,在父pom中,我有:

<pluginManagement>
    <plugins>
        <!-- Attach the jar file to the artifact -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>attach-artifacts</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>${artifactId}-${version}.jar</file>
                                <type>jar</type>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</pluginManagement>

然后在孩子們的poms中,我有:

<packaging>pom</packaging>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

最初絆倒我的一些部分:

  • attach-artifact執行應該在<pluginManagement>因此如果你mvn install或者mvn deploy父pom,它就不會被執行。
  • 孩子們需要在build-helper-maven-plugin下指定build-helper-maven-plugin ,以便運行父<pluginManagement>代碼。
  • 必須將子項聲明為具有<packaging>pom</packaging>因為如果它與工件具有相同的名稱,則無法將jar添加到工件中。

我看到這種方法的唯一缺點是工件被部署為類型pom而不是jar類型。 但我沒有看到任何真正的后果。

我在工作中遇到了這個問題:

現在我有一個target.jar(它有一個依賴列表:a.jar,b.jar,c.jar ......),我想用mvn install:install-file把它放到我的本地倉庫中,但是當我執行命令打擊

mvn install:install-file -Dfile=/Users/username/.../target.jar -DgroupId=com.cnetwork.relevance  -DartifactId=target  -Dversion=1.0.0

但是當我使用它時我發現有很多錯誤,使用target.jar的jar找不到a.jarb.jarc.jar ,如:

com.cnetwork.a does not exist
com.cnetwork.b does not exist
com.cnetwork.c does not exist

然后我去~/.m2/repository/.../target/1.0.0/target.pom找到目標的pom文件,但沒有任何內容!

...
<groupId>com.cnetwork.relevance</groupId>
<artifactId>target</artifactId>
<version>1.0.0</version>
....
# no dependencies about a/b/c.jar !!!

這是出了什么問題, install:file -Dfile -DgroupId -D..不會將依賴項添加到pom中,我通過這種方法更正了答案

  1. 如果您已經have this maven project source ,只需將其安裝到本地倉庫即可

    mvn clean install

  2. 如果你have the jar and the pom of it ,請安裝帶有pom的罐子

    mvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom

  3. 如果你don't have a pom with target jar ,請寫一個並使用upper命令。

  4. 如果你無法恢復它的pom文件,可能你應該放棄。

暫無
暫無

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

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