在Gradle Kotlin DSL中包含脚本 [英] Include scripts with Gradle Kotlin DSL

查看:140
本文介绍了在Gradle Kotlin DSL中包含脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在具有以下限制的项目中开始使用带有gradle的Kotlin DSL:

I'm trying to start using Kotlin DSL with gradle in the project with the following restrictions:

  • 项目具有不同的模块(此外:有时这些模块使用不同的插件,但是,如果两个项目使用相同的插件,则插件的版本相同).
  • 项目仅具有内部公司存储库(例如,我们不直接使用jcenter,而是使用代理).

Groovy的优势:

What we have with Groovy:

  • 一些常见的配置项不包括在单独的脚本中.请检查以下示例.
  • Gradle模块包含这些文件.

结果(仅基于我的示例):

As a result (just based on my example):

  • 我们不需要在每个模块中添加相同的代码行.
  • 大多数项目的依赖项列表有所不同.

我试图用Gralde KTS复制相同内容,但遇到了以下困难:

I tried to reproduce the same with Gralde KTS and received the following difficulties:

  • 我无法在包含文件中应用插件,而无法在模块中使用它.在这种情况下,我会收到编译错误(因为未将插件类型添加到模块脚本中).
  • 我无法将常量提取到在每个脚本(包括根build.gradle.kts)中使用它们的常见用法.使用Groovy,我只能使用springBootVersion之类的变量,但是使用Kotlin Script,我必须在每个模块文件中创建相同的属性.
  • 预编译的脚本插件在没有公共存储库访问的情况下无法正常工作(例如,我无法以仅使用默认的嵌入式Kotlin脚本版本,从这些url下载所有依赖项:..."的想法配置通用脚本文件.
  • I'm unable to apply plugin in the include file and use it in the module. In this case I receive compilation error (because plugin types are not added into the module script).
  • I'm unable to extract constants to the something common to use them in the each scripts (root build.gradle.kts inclusive). With Groovy I can just use variable like springBootVersion, however with Kotlin Script I have to create the same property in the each module file.
  • Precompiled script plugins does not work without public repositories access (e.g. I'm unable to configure common script file with idea "just use default embedded Kotlin Script version, download all dependencies from these urls: ...".

包含文件示例:

apply plugin: 'kotlin'

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Gradle模块示例:

Gradle module sample:

apply from: "${rootDir}/gradle/include/kotlin-common-include.gradle"

dependencies {
    compile project(':my.project.libraries.common') 

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
}

问题:

  • 如何仅通过使用springBootVersionConstants.springBootVersion之类的东西进行编译时检查,将所有公共常量(例如依赖项版本)放入单独的文件中?
  • 如何提取适用于包含脚本的插件(以避免Gradle模块脚本过载)?
  • 如何在没有公共全局的情况下使用预编译的脚本插件存储库访问权限?
  • How can I put all common constants (such as dependency versions) to the separate file to include them just by using something like springBootVersion or Constants.springBootVersion with compile-time checks?
  • How can I extract plugin applying to the include scripts (to avoid Gradle module scripts overload)?
  • How can I use precompiled script plugins without public global repositories access?

其他链接:

  • Issue for Gradle KTS for plugin applying.
  • Issue for Gradle KTS for shared constants extracting.

推荐答案

Kotlin DSL当前有局限性(5.3),它限制了到处进行编译时检查.为了使Kotlin DSL正常工作,它必须在Gradle API之上添加扩展,并且不能神奇地做到这一点.首先,您需要阅读 Kotlin DSL入门页面以了解它.

There are limitations in Kotlin DSL currently (5.3) that prevents to have compile-time checks everywhere. In order for Kotlin DSL to work it has to add extensions on top of the Gradle API and it can't do it magically. First of all you need to go through Kotlin DSL Primer page to understand it.

如何提取适用于包含脚本的插件(以避免Gradle模块脚本过载)?

How can I extract plugin applying to the include scripts (to avoid Gradle module scripts overload)?

一种方法是使用预编译的构建脚本使用Kotlin DSL插件.为此,您需要将脚本移到$rootDir/buildSrc项目中.如下所示:

The one way to do it is to use precompiled build scripts with Kotlin DSL Plugin. In order to do it you need to move your script into $rootDir/buildSrc project. Here how it might look like:

// $rootDir/buildSrc/build.gradle.kts
plugins {
  `kotlin-dsl`
}
repositories {
   maven {
     url = uri("http://host:8082/artifactory/...")
  }
}

然后按如下所示放置您的常用脚本:

Then put your common script like that:

// $rootDir/buildSrc/src/main/kotlin/common.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
  kotlin("jvm") version "1.3.21"
}
tasks.compileKotlin {
  kotlinOptions.jvmTarget = "1.8"
}
tasks.compileTestKotlin {
  kotlinOptions.jvmTarget = "1.8"
}

然后,您可以将此脚本应用于类似这样的插件:

Then you can apply this script as to a plugin like that:

// $rootDir/build.gradle.kts
subprojects {
  apply(id = "common")
}

如何使用没有公共全局存储库访问权限的预编译脚本插件?

How can I use precompiled script plugins without public global repositories access?

为预编译脚本插件配置自定义存储库与通常的构建脚本没什么不同.这样做:

Configuring custom repositories for pre-compiled scripts plugin is no different that your usual build script. Do it like that:

// $rootDir/buildSrc/settings.gradle.kts
pluginManagement {
  repositories.maven {
    url = uri("http://host:8082/artifactory/...")
  }
}

如果您不想使用预编译的插件,另一种解决方法是显式配置扩展.您可以这样做:

The other way around that if you don't want to use precompiled plugins is to configure extensions explicitly. You can do it like that:

// $rootDir/gradle/common.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
tasks.withType<KotlinCompile> {
  kotlinOptions.jvmTarget = "1.8"
}

在主脚本中:

// $rootDir/build.gradle.kts
subprojects {
  apply {
    plugin(KotlinPlatformJvmPlugin::class)
    from("common.gradle.kts")
  }
}

如何仅通过使用springBootVersion或Constants.springBootVersion之类的东西进行编译时检查,就可以将所有公共常数(例如依赖项版本)放入单独的文件中?

How can I put all common constants (such as dependency versions) to the separate file to include them just by using something like springBootVersion or Constants.springBootVersion with compile-time checks?

目前尚无好的方法.您可以使用其他属性,但不能保证编译时检查.像这样的东西:

There is no good way to do it currently. You can use extra properties, but it won't guarantee compile time checks. Something like that:

// $rootDir/dependencies.gradle.kts

// this will try to take configuration from existing ones
val compile by configurations
val api by configurations
dependencies {
  compile("commons-io:commons-io:1.2.3")
  api("some.dep")
}

// This will put your version into extra extension
extra["springBootVersion"] = "1.2.3"

您可以像这样使用它:

// $rootDir/build.gradle.kts
subprojects {
  apply {
    plugin<JavaLibraryPlugin>()
    from("$rootDir/dependencies.gradle.kts")
  }

在您的模块中:

// $rootDir/module/build.gradle.kts
// This will take existing dependency from extra
val springBootVersion: String by extra
dependencies {
  compile("org.spring:boot:$springBootVersion")
}

这篇关于在Gradle Kotlin DSL中包含脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆