Maven "Cannot access central in offline mode" in CI
The build was started in offline mode (-o or <offline>true</offline>), so Maven may not contact any remote repository. The requested artifact is not in the local ~/.m2 repository, and offline mode forbids downloading it.
What this error means
Resolution fails with "Cannot access central (...) in offline mode and the artifact ... has not been downloaded from it before." It only happens when the job runs mvn -o.
[ERROR] Failed to execute goal on project app: Could not resolve dependencies for
project com.example:app:jar:1.0.0: Cannot access central
(https://repo.maven.apache.org/maven2) in offline mode and the artifact
org.apache.commons:commons-lang3:jar:3.14.0 has not been downloaded from it before. -> [Help 1]Common causes
Offline build with an empty or partial local repo
The CI job runs mvn -o but the local .m2 cache was never populated with this dependency, so there is nothing to resolve from.
A new dependency was added after the cache was primed
A dependency added since the last online resolve is missing from the cached repository, and offline mode cannot fetch it.
How to fix it
Prime the local repository online first
- Run an online resolve step (
mvn dependency:go-offline) to download everything. - Cache
~/.m2/repositorybetween jobs. - Then run the build with
-oagainst the warm cache.
mvn -B dependency:go-offline
mvn -B -o verifyDrop offline mode if the cache cannot be guaranteed
Remove -o so Maven can fetch newly added artifacts, or only enable offline once go-offline has run successfully.
mvn -B verifyHow to prevent it
- Run
dependency:go-offlinebefore any offline build. - Cache
~/.m2/repositorykeyed on the POM/lock hash. - Re-prime the cache whenever dependencies change.