简体   繁体   English

Kotlin DSL:将versions.gradle.kts导入另一个build.gradle.kts

[英]Kotlin DSL: Import a versions.gradle.kts into another build.gradle.kts

I have created a versions.gradle.kts just like that: 我已经创建了一个versions.gradle.kts

object Defines {
     const val kotlinVersion = "1.2.61"
     const val junitVersion = "5.3.0"
}

Now I want to import and use that files like that: 现在我想导入并使用那样的文件:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "io.github.deglans"
version = "0.0.1-SNAPSHOT"

plugins {
    application
    kotlin("jvm") version Defines.kotlinVersion
}

application {
    mainClassName = "io.github.deglans.polishnotation.MainKt"
}

dependencies {
    compile(kotlin("stdlib-jdk8"))
    testCompile("org.junit.jupiter", "junit-jupiter-api", Defines.junitVersion)
    testRuntime("org.junit.jupiter", "junit-jupiter-engine", Defines.junitVersion)
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

How can I do that? 我怎样才能做到这一点?

NOTE: I have already seen this post but it is not exactly that I search... 注意:我已经看过这篇文章,但这并不是我搜索...

While I think it should be possible to import another gradle.kts file, I couldn't get it to work properly. 虽然我认为应该可以导入另一个gradle.kts文件,但我无法让它正常工作。

However, I did manage to define my dependencies in a separate Kotlin file in the buildSrc directory. 但是,我确实设法在buildSrc目录中的单独Kotlin文件中定义我的依赖buildSrc

  1. Create a buildSrc folder in the root of your project (same level as build.gradle.kts ) 在项目的根目录中创建一个buildSrc文件夹(与build.gradle.kts相同)
  2. Add a build.gradle.kts in that buildSrc folder. 添加build.gradle.ktsbuildSrc文件夹中。 Here, you need to define the kotlin-dsl plugin. 在这里,您需要定义kotlin-dsl插件。 You also need to define the repository where to get the plugin. 您还需要定义存储库从何处获取插件。
plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}
  1. Create a Kotlin file where you define your dependencies in src/main/kotlin inside the buildSrc folder. 创建一个Kotlin文件,在其中定义buildSrc文件夹中src/main/kotlinbuildSrc You need to create a normal Kotlin .kt file, not a gradle.kts . 您需要创建一个普通的Kotlin .kt文件,而不是gradle.kts

Reimport your Gradle config and you can now use the variables you defined in your Kotlin file created in step #3 in your build.gradle.kts . 重新导入Gradle配置,您现在可以使用在build.gradle.kts中步骤3中创建的Kotlin文件中定义的变量。

暂无
暂无

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

相关问题 如何定义 settings.gradle.kts 和 build.gradle.kts 中可用的 kotlin dsl gradle 属性? - How to define kotlin dsl gradle properties available in settings.gradle.kts and build.gradle.kts? 为什么我无法使用 Gradle Kotlin DSL (5.0) 访问 build.gradle.kts 的“插件”部分中的项目属性? - Why I cannot access project properties in "plugins" section of build.gradle.kts with Gradle Kotlin DSL (5.0)? gradle kotlin-dsl move android {} into subproject {} in root project build.gradle.kts - gradle kotlin-dsl move android {} into subproject {} in root project build.gradle.kts 如何在 buildSrc/build.gradle.kts、settings.gradle.kts 和 build.gradle.kts 中导入辅助类? - How to import a helper class in buildSrc/build.gradle.kts, settings.gradle.kts, and build.gradle.kts? 在 build.gradle.kts 中添加后无法导入 class - Can not import class after added in build.gradle.kts 如何在 build.gradle.kts 和 kotlin 中使用 JUnit 5? - How to use JUnit 5 with build.gradle.kts and kotlin? build.gradle.kts 未解决的参考:npm - build.gradle.kts unresolved reference: npm 如何使用 Maven-Publish Android Gradle Plugin 生成的组件在 Kotlin DSL (build.gradle.kts) 中? - How do you use Maven-Publish Android Gradle Plugin's generated components in Kotlin DSL (build.gradle.kts)? build.gradle.kts 语法糖 - 解释 - build.gradle.kts syntax sugar - explanation 未解决的参考:build.gradle.kts 中的 grgit - Unresolved reference: grgit in build.gradle.kts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM