如何在离线环境下 运行 Gradle kotlin-dsl 插件?

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

由于某些原因,我正在开发的机器没有连接到互联网。

我有应用程序和构建脚本的所有依赖项的本地副本。我想 运行 Gradle 使用 Kotlin 脚本,特别是 kotlin-dsl gradle 插件。出于某种原因,仅下载依赖项是不够的。

我目前有:

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

(不要问为什么这是 Maven 格式,但它应该能传达信息)

但在离线 运行 时间,运行任何 Gradle 任务失败

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

所以,如果有人知道我需要哪些其他依赖项,或者在哪里可以找到,那将不胜感激。

我已尝试重现您的设置。 我使用 gradle init 生成了一个新的 Gradle 项目,选择了一个使用 Gradle Kotlin DSL 用 Kotlin 编写的简单库。

我在一个没有任何互联网连接的环境(一个 docker 容器)中(容器以 --network none 启动)。

我使用的是最新版本的 Gradle (7.3.1),我无法重现您的确切问题。

我看到开头是这样的:

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

所以我怀疑 Gradle 设法生成了您遇到问题的 kotlin dsl 罐子。

但是我的构建失败了:

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

这与你的相似并且对我来说很有意义。 Gradle 无法获取插件


所以按照你的方法,我准备了一个pom file来下载所有需要的依赖。

然后我还有一个替代的 maven 设置文件 (maven-settings.xml),它将本地 maven 存储库移动到其他地方:

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

然后我运行 maven 将所有依赖项下载到我的本地文件夹:

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

然后我需要向 Gradle 表明它应该从这个本地存储库中使用(参见 ):

settings.gradle.kts 文件中:

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

lib/build.gradle.kts 中(我的 Gradle 项目称为 lib,由 gradle init 生成),编辑 repositories 块:

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

理论上我们甚至可以删除 mavenCentral() 行,因为在 Gradle 与 --offline 标志一起使用或没有任何互联网连接的情况下,它不会被使用。

然后构建就像一个魅力一样工作(在我的容器内没有互联网访问):

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

注:

在我的测试过程中,我也遇到了这个错误:

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

这是因为对于依赖项 org.jetbrains.kotlin:kotlin-test,您还需要 kotlin-test-1.5.31.module 文件。请参阅 Gradle Module Metadata 文档页面。

这就是为什么我也有这种依赖:

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

在有助于提前收集所有依赖项的 POM 文件中。