Maven shade jar throw exception

寵の児 提交于 2019-11-26 21:16:11

问题


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>

回答1:


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




回答2:


The problem is because of java version. I didn't notice that my new ide automatically use ibm's java, when I change the jre to sun's java ,it works well:)




回答3:


The last line of the stacktrace above says

Could not find the main class: com.mainClass.

Perhaps a typo in the classname or the class is not compiled before the plugin in invoked?



来源:https://stackoverflow.com/questions/8302022/maven-shade-jar-throw-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!