2013-05-08 25 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

相关问题