Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

A Maven build fails in the pipeline with "Could not resolve dependencies". How do you fix it?

IntermediateTroubleshootingJava builds (Maven, Gradle, JAR, WAR)
Answer

Short answer

Maven couldn't download one or more libraries. The usual causes are: no network access to Maven Central or Nexus, wrong or missing credentials in settings.xml, a version that doesn't exist, or a corrupted local cache.

Step by step

1. Read the exact error — it names the missing artifact and the repository it tried:

[ERROR] Failed to execute goal on project orders-service:
Could not resolve dependencies for project com.devopsshack:orders-service:jar:1.0.0:
Could not find artifact com.devopsshack:common-lib:jar:2.3.0 in nexus (https://nexus.company.com/repository/maven-public/)

2. Check which case it is:

What you see Likely cause Fix
Could not find artifact ... The version doesn't exist in that repository Check the version in pom.xml; check it was published to Nexus
401 Unauthorized Wrong or missing credentials Fix the <server> credentials in settings.xml (from a CI secret)
Connect timed out / UnknownHost No network or proxy issue on the runner Allow outbound access, or configure the proxy in settings.xml
Checksum validation failed / strange errors Corrupted cache Delete that folder in ~/.m2/repository and rebuild

3. Commands that help:

mvn -B -U clean package             # -U forces Maven to re-check remote repositories
mvn dependency:tree                 # see where a dependency comes from
mvn -X clean package | grep -i "repository"   # debug output: which repos are used
curl -I https://nexus.company.com/repository/maven-public/   # can the runner reach Nexus?

4. Example settings.xml for Nexus in CI:

<settings>
  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>https://nexus.company.com/repository/maven-public/</url>
    </mirror>
  </mirrors>
  <servers>
    <server>
      <id>nexus</id>
      <username>${env.NEXUS_USER}</username>
      <password>${env.NEXUS_PASSWORD}</password>
    </server>
  </servers>
</settings>
mvn -B -s settings.xml clean package

What to say in the interview

"First I read the error to see which artifact and which repository failed. If it's 'could not find', I check the version exists in Nexus. If it's 401, the credentials in settings.xml are wrong — in CI they come from secrets. If it's a timeout, the runner can't reach the repository, so I check network, proxy or firewall rules. If the cache is corrupted, I delete that artifact from ~/.m2 and rerun with -U. mvn dependency:tree and -X debug output help find exactly where it breaks."

Follow-up questions you may get

  • Why do the server id and mirror id need to match? Maven uses the server whose id matches the repository or mirror id to find the credentials.
  • How do you stop builds breaking when Maven Central is slow? Route everything through a Nexus proxy repository, which caches artifacts inside the company.
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR

More on Java builds (Maven, Gradle, JAR, WAR)