简体   繁体   English

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

[英]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.我想用 Kotlin 脚本运行 Gradle,特别是kotlin-dsl gradle 插件。 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) (不要问为什么这是 Maven 格式,但它应该传达信息)

But in offline runtime, running any Gradle task fails with但是在离线运行时,运行任何 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

So, if anyone knows which other dependencies I need, or where to look to find out, that would be greatly appreciated.因此,如果有人知道我需要哪些其他依赖项,或者在哪里可以找到,那将不胜感激。

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.我使用gradle init生成了一个新的 Gradle 项目,选择了一个使用 Gradle Kotlin DSL 用 Kotlin 编写的简单库。

I am in an environment (a docker container) without any internet connection (container is started with --network none ).我在一个没有任何互联网连接的环境(一个 docker 容器)中(容器以--network none启动)。

I am using a recent version of Gradle ( 7.3.1 ) and I can not reproduce your exact issue.我正在使用最新版本的 Gradle ( 7.3.1 ),我无法重现您的确切问题。

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.所以我怀疑 Gradle 会设法生成您遇到问题的 kotlin dsl jar。

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 Gradle 无法获取插件


So following your approach, I prepared a pom file to download all the required dependencies.因此,按照您的方法,我准备了一个pom 文件来下载所有必需的依赖项。

Then I have also an alternative maven setting file ( maven-settings.xml ) that moves the local maven repository somewhere else:然后我还有一个替代的 maven 设置文件( maven-settings.xml ),它将本地 maven 存储库移动到其他地方:

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

Then I run maven to download all the dependencies to my local folder:然后我运行 maven 将所有依赖项下载到我的本地文件夹:

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 ):然后我需要向 Gradle 表明它应该从此本地存储库中使用(请参阅Gradle Offline Build Using Maven Repository ):

In the settings.gradle.kts file:settings.gradle.kts文件中:

// 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: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()
}

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

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.这是因为对于依赖org.jetbrains.kotlin:kotlin-test ,您还需要kotlin-test-1.5.31.module文件。 See Gradle Module Metadata documentation page.请参阅Gradle 模块元数据文档页面。

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.在有助于提前收集所有依赖项的 POM 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM