How to Fix “Error in Opening Zip File” When Running Maven – 如何修复运行 Maven 时出现的 “打开 Zip 文件时出错 “问题

最后修改: 2023年 8月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Maven is a build automation and project management tool mainly used for Java-based applications. As a result, it automates software development lifecycles, making it easier for developers to manage dependencies, compile code, run tests, and package applications effectively.

Maven 是一种构建自动化和项目管理工具,主要用于基于 Java 的应用程序。因此,它能实现软件开发生命周期的自动化,使开发人员更容易有效地管理依赖关系、编译代码、运行测试和打包应用程序。

However, we may encounter an “Error in Opening Zip File” issue when using Maven commands to build an application. This issue often arises due to corrupted or inaccessible JAR files in the local Maven repository.

但是,在使用 Maven 命令构建应用程序时,我们可能会遇到 “打开 Zip 文件时出错” 问题。出现这一问题的原因通常是本地 Maven 资源库中的 JAR 文件已损坏或无法访问。

In this tutorial, we’ll explore various ways to resolve this issue.

在本教程中,我们将探讨解决这一问题的各种方法。

2. Removing the Corrupted JAR File

2.删除损坏的 JAR 文件

Maven follows the “convention over configuration” principle to ensure a predefined project structure to avoid build and configuration errors. But, sometimes, we face an “Error in Opening Zip File” issue due to a corrupted JAR file.

Maven遵循 “惯例重于配置 “的原则,以确保预定义的项目结构,避免出现构建和配置错误。但有时,我们会因 JAR 文件损坏而遇到“打开 Zip 文件时出错”问题。

2.1. Identify the Corrupted JAR

2.1.识别损坏的 JAR

To resolve this issue, we must first identify the corrupted JAR file. In order to locate the corrupted JAR file, we need to check the build logs. These logs provide all the process details and the file’s name.

要解决这个问题,我们必须首先确定损坏的 JAR 文件。为了找到损坏的 JAR 文件,我们需要检查构建日志。这些日志提供了所有进程细节和文件名称。

2.2. Remove the JAR File

2.2.删除 JAR 文件

So far, we have already figured out a process to find the culprit JAR file. We now need to remove the JAR file from the local Maven repository. To demonstrate, let’s assume we have junit-3.8.1.jar as a corrupted file:

到目前为止,我们已经找到了找到问题 JAR 文件的方法。现在,我们需要从本地 Maven 资源库中删除该 JAR 文件。为了演示,让我们假定 junit-3.8.1.jar 文件已损坏:

$ rm -rf /Home/ubuntu/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar

By using the above command, the junit-3.8.1.jar file will be removed from the local Maven repository.

使用上述命令后,junit-3.8.1.jar 文件将从本地 Maven 资源库中删除。

2.3. Rebuild the Project

2.3.重建项目

The corrupted JAR file has already been removed from the local Maven repository. Let’s rebuild the project using the mvn command:

已损坏的 JAR 文件已从本地 Maven 资源库中删除。让我们使用 mvn 命令重建项目:

$ mvn clean install

By running the above command, the project will be rebuilt, and Maven will search for the junit-3.8.1.jar dependency in the local repository. Since it won’t be able to get it from the local repository, it will download it from the remote repository.

运行上述命令后,项目将被重建,Maven 将在本地版本库中搜索 junit-3.8.1.jar 依赖项。由于无法从本地版本库中获取,它将从远程版本库中下载。

3. Clearing the Local Repository

3.清除本地存储库

If there are multiple JAR files leading to this issue, then we need to clean the entire local Maven repository. Therefore, we’ll remove all the JAR files kept in the local Maven repository. By doing this, we ensure that we have the latest version of the dependencies and there are no conflicts due to multiple versions of the same JAR file.

如果有多个 JAR 文件导致此问题,那么我们需要清理整个 local Maven repository 。因此,我们将删除本地 Maven 资源库中保存的所有 JAR 文件。通过这样做,我们可以确保我们拥有最新版本的依赖关系,并且不会因为同一 JAR 文件有多个版本而产生冲突。

3.1. Backup Local Repository

3.1.备份本地存储库

Before proceeding to remove the existing /.m2/repository/ directory. Moreover, we should first take a backup of this repository to prevent any data loss. As an outcome, it ensures that we have a copy of the required dependencies in our local repository.

在继续删除现有的 /.m2/repository/ 目录之前。此外,我们应首先备份该版本库,以防止任何数据丢失。这样做的结果是,确保我们的本地版本库中有所需依赖项的副本。

3.2. Delete the Local Repository

3.2.删除本地存储库

By deleting the local Maven repository, all cached dependencies will be cleared. Since all the dependencies must be redownloaded again, this process can be time-consuming. To demonstrate, let’s look at the command to clean the whole repository:

删除本地 Maven 资源库后,所有缓存的依赖关系都将被清除。由于所有依赖项都必须重新下载,这个过程可能会很耗时。为了演示,让我们看看清理整个版本库的命令:

$ rm -rf /.m2/repository/

The above command will simply remove all the dependencies present in the /.m2/repository/ directory.

上述命令将删除 /.m2/repository/ 目录中的所有依赖项。

3.3. Rebuild the Project

3.3.重建项目

As we have already cleaned the entire repository, let’s run the command to build the project again:

由于我们已经清理了整个版本库,因此让我们再次运行命令来构建项目:

$ mvn clean install

Using this command, Maven will fetch all the dependencies from the remote repository and add them to the local repository.

使用该命令,Maven 将从远程版本库中获取所有依赖项,并将其添加到本地版本库中。

4. Conclusion

4.结论

In this article, we explored different ways to resolve the “Error in Opening Zip File” issue. First, we looked at removing the particular corrupted JAR file. After that, we resolved the issue by completely deleting the entire local Maven repository.

在本文中,我们探讨了解决 “打开 Zip 文件时出错” 问题的不同方法。首先,我们查看了删除特定损坏 JAR 文件的方法。之后,我们通过完全删除整个本地 Maven 资源库来解决该问题。