2013-05-08 28 views
3

我一直在研究最終創建可執行jar文件的Java Maven項目。起初,我沒有問題,但後來我決定我想將依賴關係複製到jar中。如何創建一個具有依賴性的jar作爲主構建工件,稍後由assembly插件使用?

我發現下面的(非常有用)棧溢出問題,隨後在回答中提供的說明(代我自己的主類和目標版本):Problem building executable jar with maven

這奇妙的工作,但我結束了兩個jar文件(ldap-daemon-0.0.1-SNAPSHOT.jar和ldap-daemon-0.0.1-SNAPSHOT-jar-with-dependencies.jar)。我可以肯定這一點,但據我所知,我實際上無法在以後使用maven-dependency-plugin的複製功能獲得具有依賴項的jar副本。

所以,我想知道的是如何完成下列操作之一:

  • 有我的主構建神器,LDAP守護-0.0.1-SNAPSHOT.jar,包括它的依賴
  • 使用maven-dependency-plugin複製第二個構建工件(ldap-daemon-0.0.1-SNAPSHOT-jar-with-dependencies.jar)。

下面是LDAP守護我的插件配置(包裝配置是「罐子」):

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
</plugin> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>2.4</version> 
    <configuration> 
     <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
     <archive> 
      <manifest> 
       <mainClass>com.acuitus.ldapd.LDAPDaemonImp</mainClass> 
      </manifest> 
     </archive> 
    </configuration> 
    <executions> 
     <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <configuration> 
     <source>6</source> 
     <target>6</target> 
    </configuration> 
</plugin> 

,這裏是我的插件配置試圖將產生的JAR複製到一個文件夾中的下游項目:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.7</version> 
    <executions> 
     <execution> 
      <id>copy</id> 
      <phase>package</phase> 
      <goals> 
       <goal>copy</goal> 
      </goals> 
      <configuration> 
       <artifactItems> 
        <artifactItem> 
         <groupId>com.acuitus</groupId> 
         <artifactId>ldap-daemon</artifactId> 
         <version>0.0.1-SNAPSHOT</version> 
         <type>jar</type> 
         <overWrite>true</overWrite> 
         <outputDirectory>${project.build.directory}/classes/www-export</outputDirectory> 
         <destFileName>ldap-daemon.jar</destFileName> 
        </artifactItem> 
       </artifactItems> 
        <outputDirectory>${project.build.directory}/wars</outputDirectory> 
       <overWriteReleases>false</overWriteReleases> 
       <overWriteSnapshots>true</overWriteSnapshots> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

任何援助是非常感謝。謝謝你的時間!

回答

4

就像你已經知道的程序集插件生成兩個jar文件的正常的一個和所有的依賴關係。 Maven使用分類器構造來構建來自同一個pom的構件,但其內容不同,例如jdk1.6或jdk1.7。或者更常見的例子是來自maven的源代碼jar文件。程序集插件也使用該構造。你的複製配置如下所示:

<artifactItem> 
    <groupId>com.acuitus</groupId> 
    <artifactId>ldap-daemon</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <type>jar</type> 
    <overWrite>true</overWrite> 
    <outputDirectory>${project.build.directory}/classes/www-export</outputDirectory> 
    <destFileName>ldap-daemon.jar</destFileName> 
</artifactItem> 

所以你告訴maven複製正常的jar文件而不需要依賴。

不過你想要的jar文件是LDAP守護-0.0.1-SNAPSHOT-JAR-與-dependencies.jar。因此,你需要這樣的Maven能夠獲取正確的jar文件到指定的分類:

<artifactItem> 
    <groupId>com.acuitus</groupId> 
    <artifactId>ldap-daemon</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <type>jar</type> 
    <classifier>jar-with-dependencies</classifier> 
    <overWrite>true</overWrite> 
    <outputDirectory>${project.build.directory}/classes/www-export</outputDirectory> 
    <destFileName>ldap-daemon.jar</destFileName> 
</artifactItem> 

我還是建議去看看還在maven-shade-plugin當你需要在使用的生成jar文件和分類更多的控制。

+0

它是一種依賴,而我執行MVN安裝,但我可以在下游項目得到的是LDAP守護-0.0.1-SNAPSHOT.jar,不是LDAP守護-0.0.1-SNAPSHOT-JAR-與-dependencies.jar。在上面的第二個插件配置中,我無法找到任何成功的值。我不覺得我需要使用陰影插件,因爲我創建的jar是正確的,並且在正確的目錄中,但我根本不知道如何在下游項目中引用它。我誤解了陰影插件的好處嗎?感謝您花時間回答。非常感激! – 2013-05-08 22:42:03

+0

嗨。對不起昨天有點困惑,並沒有得到你的要求。我已經更正了上面的答案,並希望這是您正在尋找的內容。 – mszalbach 2013-05-09 08:49:21

+0

完成這項工作:)謝謝!我們最近更改了構建系統,而且我仍然在加快Maven的速度。我感謝您的幫助! – 2013-05-09 18:57:34

相關問題