当前位置: 首页 > 知识库问答 >
问题:

maven依赖插件不排除匹配文件

鲍理
2023-03-14

我试图摆脱。SF,。DSA和。bouncyCastle中的RSA文件,这样我就可以将jar包含到我的jar中并远程部署它。我发现我需要使用maven-依赖-插件,但是排除似乎并没有真正……排除。有点破坏了整个目的。

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

下面是我的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基于一个建议,我尝试指定排除项上方的所有工件,但仍然不起作用。当我解压jar文件时,签名密钥仍然在其中。这是我修改后的POM(片段)。我注意到Netbean在解压阶段的输出为每个包显示“包含”并排除“”。如何配置它以从jar中删除签名密钥文件?

<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-最终答案我用下面拉尔夫的答案和下面的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>

共有2个答案

东弘扬
2023-03-14

试试这个:

<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>
西门旻
2023-03-14

也许mave-shade-plugin更适合你的目的。它提供了几个转换器,允许您在将某些资源包含到最终的超级jar中之前,控制对它们采取什么操作。

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

也许这个线程也有助于让你开始。

 类似资料:
  • 问题内容: 我有一个项目,需要以下Maven jibx插件: 在jibx插件pom内部,有一个xpp3依赖关系,我想从我的项目构建过程中排除它(由于某种原因,我无法在私有存储库中拥有它)。 有没有一种方法可以配置我的pom.xml(而不是插件pom)来排除该依赖关系? 编辑:我试图从插件pom中删除xpp3依赖项,并且该项目可以成功构建,所以我知道依赖项不是强制性的。 问题答案: 这是一个示例,其

  • 关于maven和传递依赖排除有几个问题。然而,我不能让它工作。我的pom中有一些依赖项,它们重新打包了一些库,以减小pom的大小。到目前为止,这是成功的。但是当我运行时,这些可传递的依赖项会被写入。类路径文件。尽管它们被排除在外,如以下摘录所示。 Apache Maven 3.3.3(7994120775791599e205a5524ec3e0dfe41d4a06;2015-04-22T13:57

  • 我正在使用maven war插件生成war文件。 在依赖关系层次结构中,我可以看到许多可传递的依赖关系,它们被提取到lib文件夹中。 经过多次研究,我发现将它们从war lib文件夹中排除的最简单方法是在我的依赖项中将它们声明为“提供”。 然而,我有很多依赖项要排除,我必须在许多WAR pom文件中这样做。 我的问题是: 有没有办法将所有这些依赖项分组到“pom”包装中,并在我的WAR pom文件

  • 主要内容:排除依赖,可选依赖,排除依赖 VS 可选依赖 我们知道 Maven 依赖具有传递性,例如 A 依赖于 B,B 依赖于 C,在不考虑依赖范围等因素的情况下,Maven 会根据依赖传递机制,将间接依赖 C 引入到 A 中。但如果 A 出于某种原因,希望将间接依赖 C 排除,那该怎么办呢?Maven 为用户提供了两种解决方式:排除依赖(Dependency Exclusions)和可选依赖(Optional Dependencies)。 排除依赖

  • 我需要从maven Dependence插件中排除单个工件:复制依赖项。 在文档上:https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html我发现了两个有趣的选择: ExcludeArtifactId,它将排除与给定工件id匹配的所有工件(组id上的通配符) ExcludeGroupId

  • 问题内容: 我想排除Maven插件的直接依赖关系,并且此答案中描述的方法不起作用(如此注释所示)。 作为一个特定的例子: 我仍然在依赖项列表中(带有)看到了。我究竟做错了什么? (如果有人对如何用该API的JDK 9等效物替换对工件的依赖有了想法[似乎发生在Java 8上,其中“ [JAXB API os从[jar:… jre / lib /rt.jar]“],我很乐意为此开设一期新书。) 更新资