Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven shade jar throw exception

I have the following Exception:

Exception in thread "main" java.lang.SecurityException: no manifiest section for signature file entry javax/security/cert/CertificateException.class at sun.security.util.SignatureFileVerifier.verifySection(SignatureFileVerifier.java:380) at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:231) at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:176) at java.util.jar.JarVerifier.processEntry(JarVerifier.java:288) at java.util.jar.JarVerifier.update(JarVerifier.java:199) at java.util.jar.JarFile.initializeVerifier(JarFile.java:323) at java.util.jar.JarFile.getInputStream(JarFile.java:388) at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:692) at sun.misc.Resource.cachedInputStream(Resource.java:61) at sun.misc.Resource.getByteBuffer(Resource.java:144) at java.net.URLClassLoader.defineClass(URLClassLoader.java:256) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) Could not find the main class: com.mainClass. Program will exit.

My pom:

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                <version>1.5</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filter>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.mainClass</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
like image 218
lucky_start_izumi Avatar asked Nov 28 '11 20:11

lucky_start_izumi


People also ask

What is shaded jar in Maven?

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.

What is uber jar maven?

An uber-JAR—also known as a fat JAR or JAR with dependencies—is a JAR file that contains not only a Java program, but embeds its dependencies as well. This means that the JAR functions as an “all-in-one” distribution of the software, without needing any other Java code.


1 Answers

The SecurityException comes up because one of your dependencies is a signed jar. As the shade plugin is repacking this jar, it gets invalid. -> SecurityException at launch

To solve the problem, you have to unsign the dependency jars while repacking them. This can be done by simply not repacking the files that make the jar signed, using a filter:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <id>stand-alone</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>stand-alone</shadedClassifierName>
                <filters>
                    <filter>
                        <!--
                            Exclude files that sign a jar
                            (one or multiple of the dependencies).
                            One may not repack a signed jar without
                            this, or you will get a
                            SecurityException at program start.
                        -->
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                            <exclude>META-INF/*.INF</exclude> <!-- This one may not be required -->
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

This solution was extracted from here: https://issues.apache.org/jira/browse/MSHADE-61

like image 196
hoijui Avatar answered Sep 19 '22 12:09

hoijui