代码之家  ›  专栏  ›  技术社区  ›  Jorn

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

  •  0
  • Jorn  · 技术社区  · 3 年前

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

    我有一个应用程序和构建脚本的所有依赖项的本地副本。我想用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
    

    因此,如果有人知道我还需要哪些依赖项,或者在哪里可以找到,我们将不胜感激。

    0 回复  |  直到 3 年前
        1
  •  1
  •   Jmini Peter Kriens    2 年前

    我试着复制你的设置。 我用生成了一个新的Gradle项目 gradle init ,使用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 ,生成时使用 渐变初始化 ),编辑 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文件中,这有助于提前收集所有依赖项。