java - maven-dependency-plugin 不排除匹配文件

标签 java maven jar maven-3 maven-compiler-plugin

我正在尝试从 bouncycaSTLe 中删除 .SF、.DSA 和 .RSA 文件,以便我可以将 jar 包含到我的 jar 中并远程部署它。我发现我需要使用 maven-dependency-plugin,但是排除似乎实际上并没有...排除。有点违背了整个目的。

如果我解压缩文件,签名 key 仍然在那里,特别是在 META-INF 目录中。如果我手动删除它们并重新打包它,应用程序就可以工作。我只需要弄清楚如何自动排除这些文件!

下面是我的 POM 文件,任何人都可以看到我出错的地方以及如何修复它吗?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycom</groupId>
<artifactId>MyApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-sns</artifactId>
        <version>1.10.50</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-core</artifactId>
        <version>1.11</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-parsers</artifactId>
        <version>1.11</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-elasticsearch</artifactId>
        <version>1.10.35</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <jdk.version>1.8</jdk.version>
</properties>
<build>
    <!-- Declare Maven plugins -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <excludes>**/*.SF</excludes>
                        <excludes>**/*.DSA</excludes>
                        <excludes>**/*.RSA</excludes>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- Set a compiler level -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
        <!-- Maven Shade Plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <!-- Run shade goal on package phase -->
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!-- add Main-Class to manifest file -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.mycom.MyApp</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<name>MyApp</name>

编辑 1 根据建议,我尝试指定排除项之上的所有 Artifact ,但它仍然不起作用。当我解压 jar 文件时,签名 key 仍然在其中。这是我修改后的 POM(片段)。我注意到 Netbeans 在解包阶段的输出显示每个包的“包括“”并排除“”。如何配置它以从 jar 中删除签名的 key 文件?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <!-- Other configuration items -->
            </configuration>
        </execution>
    </executions>
</plugin>

编辑 2 - 最终答案我使用下面 Ralf 的答案和以下 POM 片段让它工作。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <transformers>
                    <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.mycom.MyApp</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

最佳答案

可能是 mave-shade-plugin更适合您的目的。它提供了几个转换器,允许您在将某些资源包含到最终的 uber-jar 之前控制对其采取哪些操作。

看看org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer

也许这个thread也有助于您入门。

关于java - maven-dependency-plugin 不排除匹配文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169940/

相关文章:

java - 什么是减少Java冗长的好模式/技术

java - 有没有办法从命令行配置 Maven POM 的版本?

java - 由于 UnsupportedClassVersionError,使用 JDK 6 的 Maven 构建失败

java - maven-failsafe-plugin 是否应该在指定的 POM 的后代中执行?

java - 是否有 rpm --whatprovides 的 maven 等价于 jar 文件?

Java,使用本地文件夹发布

java - 在 IDE 上运行的 JVM 比在命令行上运行需要更多的处理能力

Java JTable 转到行错误

java - TypeAdapterFactory 的 Lambda 表达式

java - 如何使用客户端/服务器 Nailgun(在 Debian Stretch 上)运行一个简单的 Java 程序?