Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

java - Maven shade plugin does not exclude the manifest signature files

I am using the maven shade plugin to generate a consolidate jar for my project. The jar is generated as expected and when i try to use the jar and run it, i get a

java.lang.SecurityException: Invalid signature file digest for Manifest main attributes error.

I googled the above error message and many people have suggested to exclude the manifest signatures from the META-INF directory. Thus i have included the step to exclude these files from the directory [i see two files by the name JARSIGN_.RSA and JARSIGN_.SF], but for some strange reason, maven shade plugin is unable to exclude these files from the META-INF directory. Could anyone explain me what i might be doing wrong? My pom.xml is below and the command that i am using to generate the jar is:

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With shade plugin 3.2.1 the following works for me.

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

On the plugin's doc page (https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html) the whole <configuration> block is shown inside the <execution> tag. This does not work. The <configuration> block should be outside the <executions> tag as shown above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...