天天看點

mvn打封包件簽名不合法問題

問題現象

$ java -jar target/x.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
    at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:330)
    at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:263)
    at java.util.jar.JarVerifier.processEntry(JarVerifier.java:318)
    at java.util.jar.JarVerifier.update(JarVerifier.java:230)
    at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
    at java.util.jar.JarFile.ensureInitialization(JarFile.java:612)
    at java.util.jar.JavaUtilJarAccessImpl.ensureInitialization(JavaUtilJarAccessImpl.java:69)
    at sun.misc.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:991)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:451)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
           

原因

因為在使用Maven打包的時候導緻某些包的重複引用,以至于打包之後的

META-INF

的目錄下多出了一些*.SF,.DSA,.RSA檔案所緻。

例如:

./META-INF$ ls
BCKEY.DSA  BCKEY.SF  DEPENDENCIES  LICENSE  MANIFEST.MF  NOTICE  io.netty.versions.properties  maven  native  services  spring.provides  versions
           

包含

BCKEY.DSA BCKEY.SF

解決方法

删除jar包中的意外檔案

zip -d your.jar 'META-INF/*.SF' 'META-INF/*.RSA' 'META-INF/*.DSA' 
           

mvn 過濾檔案

我們可以在Plugin中加入以下配置:

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

診斷mvn package dependecy方法

診斷package重複使用

mvn dependency:tree -Dverbose

$ mvn dependecy:tree -Dverbose 
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.mheartcube:game-server:jar:1.2-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 87, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (20 kB at 16 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (14 kB at 10 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.986 s
[INFO] Finished at: 2019-05-20T17:18:27+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'dependecy' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/guangfuhe/.m2/repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

# guangfuhe @ guangfudeMacBook-Pro in ~/Project/learn/java/gameserver on git:master x [17:18:27] C:1
$ mvn dependency:tree -Dverbose                                            
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.mheartcube:game-server:jar:1.2-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 87, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ---------------------< com.mheartcube:game-server >---------------------
[INFO] Building game-server 1.2-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ game-server ---
[INFO] com.mheartcube:game-server:jar:1.2-SNAPSHOT
[INFO] +- io.netty:netty-all:jar:4.1.27.Final:compile
[INFO] +- io.jpower.kcp:kcp-netty:jar:1.4.2:compile
[INFO] |  \- io.netty:netty-transport:jar:4.1.27.Final:compile
[INFO] |     +- io.netty:netty-buffer:jar:4.1.27.Final:compile
[INFO] |     |  \- io.netty:netty-common:jar:4.1.27.Final:compile
[INFO] |     \- io.netty:netty-resolver:jar:4.1.27.Final:compile
[INFO] |        \- (io.netty:netty-common:jar:4.1.27.Final:compile - omitted for duplicate)
[INFO] +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.6.RELEASE:compile
[INFO] |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  |  +- (org.slf4j:slf4j-api:jar:1.7.25:compile - omitted for duplicate)
[INFO] |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |     \- (org.slf4j:slf4j-api:jar:1.7.25:compile - omitted for duplicate)
[INFO] +- com.google.protobuf:protobuf-java:jar:3.6.1:compile
[INFO] +- junit:junit:jar:4.12:compile
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] +- io.jsonwebtoken:jjwt-api:jar:0.10.5:compile
[INFO] +- io.jsonwebtoken:jjwt-impl:jar:0.10.5:compile
[INFO] |  \- (io.jsonwebtoken:jjwt-api:jar:0.10.5:compile - omitted for duplicate)
[INFO] +- io.jsonwebtoken:jjwt-jackson:jar:0.10.5:compile
[INFO] |  +- (io.jsonwebtoken:jjwt-api:jar:0.10.5:compile - omitted for duplicate)
[INFO] |  \- com.fasterxml.jackson.core:jackson-databind:jar:2.9.6:compile
[INFO] |     +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |     \- com.fasterxml.jackson.core:jackson-core:jar:2.9.6:compile
[INFO] \- org.bouncycastle:bcpkix-jdk15on:jar:1.52:compile
[INFO]    \- org.bouncycastle:bcprov-jdk15on:jar:1.52:compile
           

--Posted from Rpc

版權聲明:本文為CSDN部落客「weixin_34384557」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_34384557/article/details/91945632