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

标签 kotlin gradle offline

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

我有应用程序和构建脚本的所有依赖项的本地副本。我想使用 Kotlin 脚本运行 Gradle,特别是 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 jar。

但是我的构建失败了:

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 表明它应该从这个本地存储库中消费(参见 Gradle Offline Build Using Maven Repository ):

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生成),编辑存储库 block :

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 文件中。

关于kotlin - 如何在离线环境下运行Gradle kotlin-dsl插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69301981/

相关文章:

kotlin - 如何显式调用对象的 init block ?

android - 如何访问 src 包中的隐藏文件级属性

Android 构建 gradle - 替换字符串资源值

java - Gradle 可以将多个项目打包到一个 jar 中吗?

angularjs - 使 Angular Web App 可离线使用

kotlin - 面向多平台时测试方法名称中的空格

java - ARCore – arFragment 中的图像与片段的显示不同

gradle - java.lang.IllegalStateException:不支持替换其他插件可能已经使用的现有任务

android - TTS API 是否可以在 android 上没有网络连接的情况下工作?

offline - 有没有人构建了可以完全离线运行的网络应用程序?