'How to run the Gradle kotlin-dsl plugin in an offline environment?

Because of reasons, the machine I'm developing on, is not connected to the internet.

I have a local copy of all dependencies for the app and the build script. I want to run Gradle with Kotlin scripts, and specifically, the kotlin-dsl gradle plugin. For some reason, just downloading the dependencies, is not enough.

I currently have:

        <dependency>
            <groupId>org.gradle.kotlin.kotlin-dsl</groupId>
            <artifactId>org.gradle.kotlin.kotlin-dsl.gradle.plugin</artifactId>
            <version>1.4.9</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.gradle.kotlin</groupId>
            <artifactId>gradle-kotlin-dsl-plugins</artifactId>
            <version>1.4.9</version>
        </dependency>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-compiler-embeddable</artifactId>
            <version>1.4.20</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>1.4.20</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-sam-with-receiver</artifactId>
            <version>1.4.20</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-gradle-plugin</artifactId>
            <version>1.4.20</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-scripting-compiler-embeddable</artifactId>
            <version>1.4.20</version>
        </dependency>

(don't ask why that's in Maven format, but it should get the message across)

But in offline runtime, running any Gradle task fails with

FAILURE: Build failed with an exception.

* Where:
Build file '<path>/build.gradle.kts' line: 1

* What went wrong:
Plugin [id: 'org.gradle.kotlin.kotlin-dsl', version: '1.4.9'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:1.4.9')
  Searched in the following repositories:
    Gradle Central Plugin Repository

So, if anyone knows which other dependencies I need, or where to look to find out, that would be greatly appreciated.



Solution 1:[1]

I have tried to reproduce your setup. I generated a new Gradle project with gradle init, selecting a simple library written in Kotlin using the Gradle Kotlin DSL.

I am in an environment (a docker container) without any internet connection (container is started with --network none).

I am using a recent version of Gradle (7.3.1) and I can not reproduce your exact issue.

I see at the beginning stuff like this:

> Evaluating settings > Generating gradle-api-7.3.1.jar
> Evaluating settings > Generating gradle-kotlin-dsl-extensions-7.3.1.jar
...

So I suspect that Gradle manages to generate the kotlin dsl jars that you have issues with.

But my build is failing at:

FAILURE: Build failed with an exception.

* Where:
Build file '/home/work/lib/build.gradle.kts' line: 9

* What went wrong:
Plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.5.31'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.5.31')
  Searched in the following repositories:
    Gradle Central Plugin Repository

Which is similar to yours and makes sense to me. Gradle can not get the plugin


So following your approach, I prepared a pom file to download all the required dependencies.

Then I have also an alternative maven setting file (maven-settings.xml) that moves the local maven repository somewhere else:

<settings>
    <localRepository>/home/work/work/repo/m2</localRepository>
</settings>

Then I run maven to download all the dependencies to my local folder:

mvn dependency:go-offline -s maven-settings.xml

Then I need to indicate to Gradle that it should consume from this local repo (see Gradle Offline Build Using Maven Repository):

In the settings.gradle.kts file:

// Use the local maven repository:
pluginManagement {
  repositories {
      maven {
        url = uri("file:///home/work/repo/m2")
      }
  }
}

In the lib/build.gradle.kts (my Gradle project is called lib, as generated with gradle init), edit the repositories bloc:

repositories {
    // Use the local maven repository:
    maven {
        url = uri("file:///home/work/repo/m2")
    }
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

In theory we could even remove the mavenCentral() line, because in a scenario where Gradle is used with the --offline flag or without any internet connection it will not be used.

Then the build is working like a charm (inside my container without internet access):

root@574b7fd57f6d:/home/work# ./gradlew build

Welcome to Gradle 7.3.1!

Here are the highlights of this release:
 - Easily declare new test suites in Java projects
 - Support for Java 17
 - Support for Scala 3

For more details see https://docs.gradle.org/7.3.1/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 43s
5 actionable tasks: 5 executed

Note:

During my test I also got this error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':lib:compileTestKotlin'.
> Error while evaluating property 'filteredArgumentsMap' of task ':lib:compileTestKotlin'
   > Could not resolve all files for configuration ':lib:testCompileClasspath'.
      > Could not resolve org.jetbrains.kotlin:kotlin-test:1.5.31.
        Required by:
            project :lib
         > Unable to find a variant of org.jetbrains.kotlin:kotlin-test:1.5.31 providing the requested capability org.jetbrains.kotlin:kotlin-test-framework-junit:
              - Variant compile provides org.jetbrains.kotlin:kotlin-test:1.5.31
              - Variant runtime provides org.jetbrains.kotlin:kotlin-test:1.5.31
              - Variant platform-compile provides org.jetbrains.kotlin:kotlin-test-derived-platform:1.5.31
              - Variant platform-runtime provides org.jetbrains.kotlin:kotlin-test-derived-platform:1.5.31
              - Variant enforced-platform-compile provides org.jetbrains.kotlin:kotlin-test-derived-enforced-platform:1.5.31
              - Variant enforced-platform-runtime provides org.jetbrains.kotlin:kotlin-test-derived-enforced-platform:1.5.31


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

* Get more help at https://help.gradle.org

This was because for the dependency org.jetbrains.kotlin:kotlin-test you also will need the kotlin-test-1.5.31.module file. See Gradle Module Metadata documentation page.

This is why I have also this dependency:

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-test</artifactId>
    <version>1.5.31</version>
    <type>module</type><!-- force maven to download the gradle metadata for this dependency -->
</dependency>

in the POM file that helps to collect all dependencies in advance.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jmini