在使用maven-dependency-plugin选择想要的deps之后,如何创建具有特定deps的jar?

时间:2018-04-15 05:00:16

标签: maven

我的目标是从pom中的依赖列表创建一个具有特定依赖关系的jar。我是这样使用maven-dependency-plugin:

String sql = "DELETE FROM course 
              WHERE (username_entry = " + username + 
            " AND course_name = " + courseToDelete.toUpperCase() + ")";

和一个assembly.xml文件:

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
                <includeScope>runtime</includeScope>
                <excludes>META-INF/*.SF,META-INF/*.DSA,META-INF/*.RSA</excludes>
                <outputDirectory>${project.build.directory}/uber-deps/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.some.blaClass</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <id>plugin</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <unpack>true</unpack> <scope>runtime</scope> <includes> <include> ${project.build.directory}/uber-deps/ </include> </includes> <excludes> <exclude>*:sources</exclude> </excludes> </dependencySet> </dependencySets> </assembly> 之后所有相关的依赖关系都出现在target / uber-deps中,正如我所期望的那样。 我的问题是使用mvn clean install下的下一个插件 - maven-assembly-plugin。在我看来,好像它没有采取超级计划。

我只是通过尝试使用<plugins>解压缩jar来了解这一点,以查看uber-deps中的deps是否包装在jar xf之后创建的jar中。

应该改变什么?

1 个答案:

答案 0 :(得分:1)

1) 您将作为./target/<artifactId>-plugin.jar的一部分构建的jar将被调用(默认情况下)plugin

请注意,id部分是您在程序集xml文件中fileSets下的内容。

2) 由于您已将依赖项解压缩到文件夹,因此应使用dependencySets而不是<fileSets> <fileSets> <fileSet> <outputDirectory>/</outputDirectory> <includes> <include> ${project.build.directory}/uber-deps/ </include> </includes> </fileSet> </fileSets> </fileSets>

fileSet

3) 顺便说一句,如果你想在你的jar中你自己的项目的输出,你应该添加另一个<fileSet> <outputDirectory>/</outputDirectory> <includes> <include> ${project.build.outputDirectory} </include> </includes> </fileSet>

shade-plugin

4)还要注意,您的程序集插件定义没有说明程序集xml文件的位置,并且您尝试使用src/assembly/plugin.xml配置定义mainClass。这是它在程序集插件中的外观(假设您的程序集文件位于<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <configuration> <descriptors> <descriptor>src/assembly/plugin.xml</descriptor> </descriptors> <archive> <manifest> <mainClass>com.some.blaClass</mainClass> </manifest> </archive> </configuration> </execution> </executions> </plugin>

下)
{{1}}