【问题标题】:How to use maven assembly plugin to exclude a package from dependency jar?如何使用 maven 程序集插件从依赖 jar 中排除包?
【发布时间】:2016-05-16 19:08:19
【问题描述】:

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

程序集插件有一个解压依赖 jars 的选项,如果使用此选项,您可以使用 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>

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

【问题讨论】:

    标签: java maven jar packaging maven-assembly-plugin


    【解决方案1】:

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


    一个(复杂的)解决方法是使用 maven-dependency-plugin 到 unpack 项目的依赖项,您想从中排除某些内容,然后再次使用 maven-jar-plugin 到 jar 类排除新中的包JAR,最后为该特定依赖项的maven-assembly-plugin 声明一个&lt;files&gt; 元素。

    一个示例配置是

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

    然后在您的程序集配置中,您将拥有:

    <files>
        <file>
            <source>${project.build.directory}/unpack/wonderful-library-repackaged.jar</source>
            <outputDirectory>/${project.build.finalName}/lib</outputDirectory>
        </file>
    </files>
    

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2015-02-19
      • 2015-04-28
      • 1970-01-01
      相关资源
      最近更新 更多