【问题标题】:Maven shade plugin does not exclude the manifest signature filesMaven shade插件不排除清单签名文件
【发布时间】:2016-01-12 08:23:16
【问题描述】:

我正在使用 maven shade 插件为我的项目生成一个合并 jar。 jar 按预期生成,当我尝试使用 jar 并运行它时,我得到一个

java.lang.SecurityException:签名文件摘要无效 显示主要属性错误。

我用谷歌搜索了上述错误消息,许多人建议从 META-INF 目录中排除清单签名。因此,我已经包含了从目录中排除这些文件的步骤 [我看到两个文件名为 JARSIGN_.RSAJARSIGN_.SF],但由于某些奇怪的原因,maven shade 插件无法从 META-INF 中排除这些文件目录。谁能解释一下我可能做错了什么?我的 pom.xml 在下面,我用来生成 jar 的命令是:

mvn clean package shade:shade

pom.xml

    <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.abc.xyz</groupId>
        <artifactId>myjar</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <url>http://maven.apache.org</url>

        <properties>
            <!-- A few custom properties -->
        </properties>


        <dependencies>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.3.1</version>
            </dependency>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>18.0</version>
            </dependency>
        <!-- Other The dependencies are here -->
        </dependencies>

        <repositories>
            <!-- Repository Information -->
        </repositories>
        <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>2.3.2</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </plugin>
                    <!-- Maven Shade Plugin -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>2.4.2</version>
                        <executions>
                            <!-- Run shade goal on package phase -->
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <filters>
                                        <filter>
                                            <artifact>*:*</artifact>
<!-- The below statement is not executed by shade plugin -->
                                            <excludes>
                                                <exclude>META-INF/*.SF</exclude>
                                                <exclude>META-INF/*.DSA</exclude>
                                                <exclude>META-INF/*.RSA</exclude>
                                            </excludes>
                                        </filter>
                                    </filters>                          
                                    <minimizeJar>true</minimizeJar>
                                    <artifactSet>
                                        <includes>
                                            <include>com.google.guava:guava</include>
                                            <include>com.google.code.gson:gson</include>
                                        </includes>
                                    </artifactSet>
                                    <transformers>
                                        <!-- add Main-Class to manifest file -->
                                        <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <manifestEntries>
                                                <Main-Class>com.abc.xyz.HelloWorld</Main-Class>
                                            </manifestEntries>
                                        </transformer>
                                    </transformers>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
        </build>
    </project>

【问题讨论】:

  • 这个配置好像是正确的,我用同样的成功了:pastebin.com/2ZQjehMi-X上打开调试输出来验证插件是否被执行并且这些资源以后没有添加

标签: java maven jar maven-shade-plugin


【解决方案1】:

使用 shade 插件 3.2.1,以下对我有用。

<!-- language: lang-xml -->
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

在插件的文档页面 (https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html) 上,整个 &lt;configuration&gt; 块显示在 &lt;execution&gt; 标记内。这不起作用。如上所示,&lt;configuration&gt; 块应位于 &lt;executions&gt; 标记之外。

【讨论】:

    【解决方案2】:

    也许插件的配置语法已经改变,但这在过去对我来说适用于 1.5 版的着色器插件:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.5</version>
        <configuration>
            <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                    <resource>META-INF/JARSIGN_.SF</resource>
                </transformer>
            </transformers>
        </configuration>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    我没有尝试使用通配符。但是查看documentation 以下应该排除所有.SF 文件:

    <resource>.SF</resource>
    

    另一个例子见thread

    【讨论】:

      【解决方案3】:

      我有一个类似的问题,无论我尝试什么设置,Shade 插件显然都没有从 META-INF 目录中排除文件。我正在使用以下 bash 命令对其进行检查:

      mvn clean install
      7za x target/built-jar-6.4.0.jar -aoa -o/tmp/unpacked/
      ls /tmp/unpacked/META-INF/
      

      问题实际上不在于 shade 插件本身,而在于我解压 JAR 的方式 - 解压命令会覆盖文件,但它会将旧文件保留在原位,这让我觉得我的设置有问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-17
        • 2021-11-24
        • 2014-07-23
        • 1970-01-01
        • 1970-01-01
        • 2015-08-20
        • 2015-02-05
        相关资源
        最近更新 更多