bob-cac
bob-cac

Reputation: 1302

Maven downloads corrupted jar

I have a project that depend on the following artifact:

  <dependency>
        <groupId>com.jaspersoft.jasperserver</groupId>
        <artifactId>jasperserver-common-ws</artifactId>
        <version>5.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.jaspersoft.jaspersoft.api.metadata</groupId>
        <artifactId>jasperserver-api-metadata</artifactId>
        <version>5.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.jaspersoft.jasperserver</groupId>
        <artifactId>jasperserver-ireport-plugin</artifactId>
        <version>3.7.0</version>
    </dependency>
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>5.0.1</version>
    </dependency>

    <dependency>
    <groupId>jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>5.0.1</version>
</dependency>

When I run

maven install eclipse

outputs a warning specifying that check-sum validation failed. When I try to open the installed jar using winrar , win-rar indicate that they are corrupted. I have tried to turn off antivirus yet the artifact are still fail to download properly.

Upvotes: 4

Views: 5057

Answers (4)

JRA_TLL
JRA_TLL

Reputation: 1361

Sporadicly, Maven downloads broken files. In this case, you can only locate the file, delete it and have Maven download it again.

This is just the workaround. The solution is to add fail to the repository config in your maven settings file:

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

Upvotes: 0

GOXR3PLUS
GOXR3PLUS

Reputation: 7255

Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature)

Here is an small detector written in Java , just copy and run :)

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

}

Output

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)

Upvotes: 0

Brendan Poole
Brendan Poole

Reputation: 41

I had the same problem and it turned turned out to be because one of the repositories was returning HTML instead of a jar file. This became apparent when I viewed the corrupt jar in Notepad++. In my case removing the following repo from the pom fixed the problem:

     <repository>
        <id>sibboleth</id>
        <name>Sibboleth</name>
        <url>http://shibboleth.internet2.edu/downloads/maven2/</url>
        <layout>default</layout>
    </repository>

Upvotes: 4

peterh
peterh

Reputation: 1

It is a network problem, or (with a low chance) the original jar file is damaged.

Try to download the original jar, with a different http client, as the maven has (it is not the best). It shows the downloading urls of the actual jars.

If it works, then a simple workaround were is you simply download this jar with a working http client and put this to its place in your repository.


As a longterm solution, I suggest some test on your network environment, and maybe a full regeneration of your maven repository.

Upvotes: 1

Related Questions