部署 Maven 项目会抛出 java.util.zip. ZipException: 无效的 LOC 头(错误签名)

当我运行我的 mvn install时,我得到了下面的异常。我甚至删除了本地存储库,并再次运行获得相同的异常。

未能完成目标 Plugins: maven-shadow-plugin: 2.1: shadow (default) on 项目核心-批处理: 创建阴影 jar 错误: 无效 LOC 头 (坏的签名)-> [帮助1]

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>commons-logging:commons-logging:jar:*</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<!-- workaround for a spring issues -->
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<!-- don't want to pick up any other log4j.xml -->
<exclude>log4j.xml</exclude>
</excludes>
</filter>
</filters>
<!-- May be needed to work around another issue in Spring -->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on project cores-batch: Error creating shaded jar: invalid LOC header (bad signature) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on project cores-batch: Error creating shaded jar: invalid LOC header (bad signature)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error creating shaded jar: invalid LOC header (bad signature)
at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute(ShadeMojo.java:528)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
... 19 more
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
at java.util.zip.ZipFile.read(Native Method)
at java.util.zip.ZipFile.access$1400(ZipFile.java:56)
at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:679)
at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:415)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.codehaus.plexus.util.IOUtil.copy(IOUtil.java:189)
at org.codehaus.plexus.util.IOUtil.copy(IOUtil.java:175)
at org.apache.maven.plugins.shade.DefaultShader.addResource(DefaultShader.java:427)
at org.apache.maven.plugins.shade.DefaultShader.shade(DefaultShader.java:186)
at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute(ShadeMojo.java:458)
... 21 more
[ERROR]
[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/MojoExecutionException
305674 次浏览

Jar 文件可能已损坏。请尝试删除下列文件夹的内容:

 C:\Users\[username]\.m2\repository

然后右键单击您的项目,选择 Maven,Update Project,检查强制更新快照/发布。

主要问题是罐子损坏。

要找到损坏的那个,您需要在 Eclipse 的 Breakpoints View 或您首选的 IDE 中添加一个 Java 异常断点,选择 java.util.zip.ZipException类,然后重新启动 Tomcat 实例。

当 JVM 挂起到 ZipException断点时,您必须转到 在堆栈跟踪中查看 JarFile.getManifestFromReference(),并检查属性 name以查看文件名。

之后,您应该从文件系统中删除该文件,然后右键单击您的项目,选择 Maven,Update Project,检查强制更新快照/发布。

看起来像是您的 pom 文件中的 maven 编译器的配置问题。默认版本 Java 源码和目标是1.5,即使使用 JDK 也有较高的版本。

要修复这个问题,需要添加 maven 编译器插件配置部分和更高的 java 版本,例如:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

更多信息请查看以下链接:

Maven 编译程序

漏洞报告

Gsitgithub/find-currupt-jar. txt开始,下面的命令列出了存储库中所有损坏的 jar 文件:

find  /home/me/.m2/repository/ -name "*jar" | xargs -L 1 zip -T | grep error | grep invalid

您可以删除损坏的 jar 文件,并重新编译项目。

输出示例:

warning [/cygdrive/J/repo/net/java/dev/jna/jna/4.1.0/jna-4.1.0.jar]:  98304 extra bytes at beginning or within zipfile
(attempting to process anyway)
file #1:  bad zipfile offset (local header sig):  98304
(attempting to re-compensate)
zip error: Zip file invalid, could not spawn unzip, or wrong unzip (original files unmodified)

我想给我的实践。

使用你喜欢的 IDE,以 eclipse 为例:

  1. 在异常堆栈中找到适当的位置
  2. 设置条件断点
  3. 调试一下
  4. 它将在异常之前打印损坏的 jar

enter image description here

我的解决方案是用 -X运行 mvn:

$ mvn package -X

然后向后查看输出,直到看到失败,然后继续查看,直到看到 mvn 试图处理的最后一个 jar 文件:

...
... <<output ommitted>>
...
[DEBUG] Processing JAR /Users/snowch/.m2/repository/org/eclipse/jetty/jetty-server/9.2.15.v20160210/jetty-server-9.2.15.v20160210.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.607 s
[INFO] Finished at: 2017-10-04T14:30:13+01:00
[INFO] Final Memory: 23M/370M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.1.0:shade (default) on project kafka-connect-on-cloud-foundry: Error creating shaded jar: invalid LOC header (bad signature) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.1.0:shade (default) on project kafka-connect-on-cloud-foundry: Error creating shaded jar: invalid LOC header (bad signature)

查看它失败之前的最后一个 jar,并从本地存储库中删除它,即。

$ rm -rf /Users/snowch/.m2/repository/org/eclipse/jetty/jetty-server/9.2.15.v20160210/

你需要检查哪个罐子出了问题。一定是被破坏了。删除那个 jar 并再次运行 mvn spring-boot:run命令。可能多于一个 jar 已损坏,因此每次需要运行该命令删除该 jar 时。在我的案例中,mysql,jackson,方面的 jar 被破坏了3次 mvn spring-boot:run命令,我发现这一点,并从 .m2文件夹中删除了 jar。现在问题解决了。

我在将耳朵部署到本地 Web 逻辑实例时遇到了这个问题。对我来说,清理本地存储库并再次构建耳朵解决了这个问题。

无法移除。M2/仓库,从服务器中删除应用程序,运行服务器(不包括应用程序) ,停止它,并再次添加应用程序。现在应该起作用了。出于某种原因,仅仅清理接口中的服务器文件夹不会产生相同的效果。

这个问题的答案不是针对 DevOps/系统管理员的,而是针对那些正在使用 Eclipse 这样的 IDE 并面临 invalid LOC header (bad signature)问题的人。

您可以强制更新 maven 依赖项,如下所示:

enter image description here

enter image description here

这是一个用 Java 编写的小型检测器,只需复制并运行:)

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
import java.util.stream.Collectors;


public class JarValidator {


public static void main(String[] args) throws IOException {
Path repositoryPath = Paths.get("C:\\Users\\goxr3plus\\.m2");


// Check if the main Repository Exists
if (Files.exists(repositoryPath)) {


// Create a class instance
JarValidator jv = new JarValidator();


List<String> jarReport = new ArrayList<>();
jarReport.add("Repository to process: " + repositoryPath.toString());


// Get all the directory files
List<Path> jarFiles = jv.getFiles(repositoryPath, ".jar");
jarReport.add("Number of jars to process: " + jarFiles.size());
jarReport.addAll(jv.openJars(jarFiles, true));


// Print the report
jarReport.stream().forEach(System.out::println);


} else {
System.out.println("Repository path " + repositoryPath + " does not exist.");
}
}


/**
* Get all the files from the given directory matching the specified extension
*
* @param filePath      Absolute File Path
* @param fileExtension File extension
* @return A list of all the files contained in the directory
* @throws IOException
*/
private List<Path> getFiles(Path filePath, String fileExtension) throws IOException {
return Files.walk(filePath).filter(p -> p.toString().endsWith(fileExtension)).collect(Collectors.toList());
}


/**
* Try to open all the jar files
*
* @param jarFiles
* @return A List of Messages for Corrupted Jars
*/
private List<String> openJars(List<Path> jarFiles, boolean showOkayJars) {
int[] badJars = { 0 };
List<String> messages = new ArrayList<>();


// For Each Jar
jarFiles.forEach(path -> {


try (JarFile file = new JarFile(path.toFile())) {
if (showOkayJars)
messages.add("OK : " + path.toString());
} catch (IOException ex) {
messages.add(path.toAbsolutePath() + " threw exception: " + ex.toString());
badJars[0]++;
}
});


messages.add("Total bad jars = " + badJars[0]);
return messages;
}


}

输出

Repository to process: C:\Users\goxr3plus\.m2
Number of jars to process: 4920
C:\Users\goxr3plus\.m2\repository\bouncycastle\isoparser-1.1.18.jar threw exception: java.util.zip.ZipException: zip END header not found
Total bad jars = 1
BUILD SUCCESSFUL (total time: 2 seconds)

我们可以使用至少两个选项在 maven 中强制校验和验证:

1. 将 --strict-checksums添加到我们的 maven 命令中。

2. 在我们的 maven 设置文件中添加以下配置:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--...-->
<profiles>
<profile>
<!--...-->
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>
<!--...-->
</url>
</repository>
</repositories>
<pluginRepositories>
<!--...-->
</pluginRepositories>
<!--...-->
</profile>
</profiles>
<!--...-->
</settings>

更多细节在这篇文章: https://dzone.com/articles/maven-artifact-checksums-what

也许与这个问题的原因无关,但对于那些将面临同样的梯度和局部模块依赖的人来说

dependencies {
checkstyle project(":module")
}

如果模块不包含组和版本,则可能发生此错误,因此只需在 module/build.gradle中指定

plugins {
id 'java-library'
}


group = "com.example"
version = "master-SNAPSHOT"

如果您正在使用 CentOS linux 系统,Maven 本地存储库将是:

/root/.m2/repository/

您可以删除.m2并在 dev tool 中构建您的 maven 项目,这将解决这个问题。

清洗和重建对我来说很有用。