Repositories

A dependency is a coordinate, and a repository is a place that maps a coordinate to files. Gradle asks the repositories of the repositories block in order and stops at the first one that has the module. This image has no network, so one of the two well-known repositories cannot answer, and the lesson shows both the failure and the way out.

mavenCentral() offline

settings.gradle

rootProject.name = 'shop'

build.gradle

plugins { id 'java' } repositories { mavenCentral() } dependencies { implementation 'org.apache.commons:commons-lang3:3.14.0' }

src/main/java/com/example/Text.java

package com.example; import org.apache.commons.lang3.StringUtils; public final class Text { public static String reverse(String text) { return StringUtils.reverse(text); } }

commons-lang3 is in the local Maven repository of this image but not in the cache Gradle keeps for mavenCentral(), and with --offline Gradle refuses to ask the network. What it says is precise:

gradle compileJava --console=plain --offline --no-daemon

1 actionable task: 1 executed

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava' (registered by plugin class 'org.gradle.api.plugins.JavaBasePlugin').
> Could not resolve all files for configuration ':compileClasspath'.
   > Could not resolve org.apache.commons:commons-lang3:3.14.0.
     Required by:
         root project 'shop'
      > No cached version of org.apache.commons:commons-lang3:3.14.0 available for offline mode.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at https://help.gradle.org.

BUILD FAILED in 15s

mavenLocal()

One line changes the repository, and one line changes the answer:

build.gradle

plugins { id 'java' } repositories { mavenLocal() } dependencies { implementation 'org.apache.commons:commons-lang3:3.14.0' }

gradle compileJava --console=plain --offline --no-daemon

> Task :compileJava

BUILD SUCCESSFUL in 18s
1 actionable task: 1 executed

mavenLocal() reads ~/.m2/repository, the folder Maven fills with mvn install. Here it was filled when the image was built, which is why this course can resolve JUnit and a few other jars without a network.

Order and content filters

The order matters for speed and for correctness. Gradle asks each repository in turn for the metadata of the module, and a repository that answers with a 404 passes the question to the next one; a company build usually puts its own repository first. A repository can also be restricted to a group, with a content { includeGroup 'com.example' } block, so Gradle asks it only for the modules that belong to it and does not wait for a timeout on the rest.

The remote repository of that block is not reachable here and is not run. It is also why a build should never rely on mavenLocal() alone: a jar that only exists on one machine makes the build of another machine fail with the message above. mavenLocal() belongs in a course, in a build that publishes into it, and in a first experiment; a team build uses a repository of its own.

Related

Автор статьи: Arthur Isaev