首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在离线环境中运行Gradle kotlin-dsl插件?

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

Stack Overflow用户
提问于 2021-09-23 14:28:39
回答 1查看 640关注 0票数 3

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

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

我目前有:

代码语言:javascript
复制
        <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任务都会失败。

代码语言:javascript
复制
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

因此,如果有人知道我需要哪些其他依赖项,或者在哪里查找,这将是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-07 21:15:30

我试过复制你的装置。我用gradle init生成了一个新的Gradle项目,使用Gradle Kotlin DSL选择一个用Kotlin编写的简单库。

我所处的环境(一个码头容器)没有任何互联网连接(容器是用--network none启动的)。

我使用的是最近版本的Gradle (7.3.1),我无法复制您的确切问题。

我从一开始就看到了这样的东西:

代码语言:javascript
复制
> Evaluating settings > Generating gradle-api-7.3.1.jar
> Evaluating settings > Generating gradle-kotlin-dsl-extensions-7.3.1.jar
...

因此,我怀疑Gradle能够生成您有问题的kotlin dsl jars。

但我的设计失败了:

代码语言:javascript
复制
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文件来下载所有所需的依赖项。

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

代码语言:javascript
复制
<settings>
    <localRepository>/home/work/work/repo/m2</localRepository>
</settings>

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

代码语言:javascript
复制
mvn dependency:go-offline -s maven-settings.xml

然后,我需要向Gradle指出,它应该使用这个本地回购(请参阅使用Maven Repository进行离线构建):

settings.gradle.kts文件中:

代码语言:javascript
复制
// Use the local maven repository:
pluginManagement {
  repositories {
      maven {
        url = uri("file:///home/work/repo/m2")
      }
  }
}

lib/build.gradle.kts (my Gradle项目称为lib,与gradle init一起生成)中,编辑repositories块:

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

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

然后,构建就像一种魅力(在我的容器内,没有互联网接入):

代码语言:javascript
复制
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

注意:

在我的测试中,我还得到了以下错误:

代码语言:javascript
复制
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文件。请参阅分级模块元数据文档页面。

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

代码语言:javascript
复制
<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文件中,它有助于预先收集所有依赖项。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69301981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档