如何使用maven程序集插件从依赖jar中排除包?

And*_*y W 6 java packaging jar maven maven-assembly-plugin

我正在使用 Maven 程序集插件来打包项目的发行版,其中包含一个包含依赖项 jar 的 lib 文件夹、一个包含资源的配置文件夹以及包含项目类文件的 jar 文件。我需要从 lib 文件夹中的依赖项 jar 之一中排除一个包。

程序集插件有一个选项来解压依赖项 jar,如果使用此选项,那么您可以使用 assembly.xml 排除程序包,如下所示:

<assembly>
    <formats>
        <format>tar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>./${project.build.finalName}/lib</outputDirectory>
            <scope>runtime</scope>
            <unpackOptions>
                <excludes>
                    <exclude>**/excludedpackage/**<exclude>
                </excludes>
            </unpackOptions>
        </dependencySet>
    </dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何在不使用 unpack 的情况下从依赖项 jar 中排除包(即将所有依赖项打包为 jar)?理想情况下,我想要一个可以使用程序集插件来完成的解决方案 - 如果这不可能,那么实现我想做的事情的最简单方法是什么?

Tun*_*aki 1

我不认为你可以在 JAR 解包和过滤后重新打包它。您可以在Maven Assembly Plugin JIRA提交增强请求。


一个(复杂的)解决方法是使用 来maven-dependency-plugin解压想要从中排除某些内容的项目依赖项,然后使用再次maven-jar-plugin打包,排除新 JAR 中的包,最后为该特定依赖项声明一个<files>元素maven-assembly-plugin

示例配置是

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>unpack</id>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
                <includeArtifactIds><!-- include here the dependency you want to exclude something from --></includeArtifactIds>
                <outputDirectory>${project.build.directory}/unpack/temp</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>repack</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
                <classesDirectory>${project.build.directory}/unpack/temp</classesDirectory>
                <excludes>
                    <exclude>**/excludedpackage/**</exclude>
                </excludes>
                <outputDirectory>${project.build.directory}/unpack</outputDirectory>
                <finalName>wonderful-library-repackaged</finalName> <!-- give a proper name here -->
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

然后在您的装配配置中,您将拥有:

<files>
    <file>
        <source>${project.build.directory}/unpack/wonderful-library-repackaged.jar</source>
        <outputDirectory>/${project.build.finalName}/lib</outputDirectory>
    </file>
</files>
Run Code Online (Sandbox Code Playgroud)