如何创建一个带有依赖关系的jar作为主要构建工件,稍后由程序集插件使用?

时间:2013-05-08 21:39:57

标签: java maven jar dependencies

我一直在研究最终创建可执行jar文件的Java Maven项目。起初我没有问题,但后来我决定将依赖项复制到jar中。

我发现了以下(非常有用)堆栈溢出问题,并按照答案中提供的说明(替换我自己的主类和目标版本):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-daemon-0.0.1-SNAPSHOT.jar包含其依赖项
  • 使用maven-dependency-plugin复制第二个构建工件(ldap-daemon-0.0.1-SNAPSHOT-jar-with-dependencies.jar)。

这是我的ldap-daemon的插件配置(打包配置是“jar”):

<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>

非常感谢任何帮助。谢谢你的时间!

1 个答案:

答案 0 :(得分:4)

就像你已经知道的那样,程序集插件会生成两个正常的jar文件和一个具有所有依赖关系的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-daemon-0.0.1-SNAPSHOT-jar-with-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>

当您需要更多地控制生成的jar文件和使用的分类器时,我仍然建议您查看maven-shade-plugin