Handling dependency download errors

There could be situations when a dependency might not be downloaded due to network problems or other issues. Sometimes, the error reported by Maven might not indicate the problem. It is good to know how to get around this problem.

How to do it...

It is difficult to simulate this problem in a normal scenario, but we can create a contrived scenario, by using the following steps:

  1. Modify the dependency version for JUnit in our simple project:
    <version>3.9.1 </version>
  2. Run the following command:
    mvn verify
    
  3. This will attempt to download the dependency and fail (as the version is invalid):
    [INFO] Building simple-project 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    Downloading:https://repo.maven.apache.org/maven2/junit/junit/3.9.1/junit-3.9.1.pom
    [WARNING] The POM for junit:junit:jar:3.9.1 is missing, no dependency information available
    Downloading:https://repo.maven.apache.org/maven2/junit/junit/3.9.1/junit-3.9.1.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3.329 s
    [INFO] Finished at: 2014-11-08T15:59:33+05:30
    [INFO] Final Memory: 7M/154M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal on project simple-project: Could not resolve dependencies for project com.packt.cookbook:simple-project:jar:1.0-SNAPSHOT: Could not find artifact junit:junit:jar:3.9.1 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
    
  4. Run the command again and observe the results:
    [ERROR] Failed to execute goal on project simple-project: Could not resolve dependencies for project com.packt.cookbook:simple-project:jar:1.0-SNAPSHOT: Failure to find junit:junit:jar:3.9.1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
    
  5. Delete the folder 3.9.1 (or the files in the folder ending with .lastUpdated) in the local repo (.m2/repository/junit/junit/3.9.1) and retry.

    The resolution will not be attempted error will go away and Maven will attempt to download the dependency again

How it works...

Maven first downloads the pom file of the dependency. It analyzes the pom file and recursively downloads the transitive dependencies specified there. It then downloads the actual dependency file, typically, a JAR file.

When Maven fails to download an artifact, it creates a file with the same name as the artifact it failed to download, but suffixed with .lastUpdated. In the file, it puts information related to the download, as shown in the following example:

#NOTE: This is an Aether internal implementation file, its format can be changed without prior notice.
#Sat Nov 08 15:59:33 IST 2014
https://repo.maven.apache.org/maven2/.lastUpdated=1415442573938
https://repo.maven.apache.org/maven2/.error=

When a request is made to Maven to download the dependency again, maven refers to the contents of this file to decide whether or not maven should reattempt. This is the case for release dependencies. The deletion of this file will ensure maven reattempts to download the dependency when asked.

We have seen how this works for SNAPSHOT dependencies in the Understanding the SNAPSHOT dependencies recipe of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.61