我正在将Gradle构建脚本从Groovy迁移到Kotlin DSL,其中没有真正记录的事情之一是如何填充额外的属性。
在Groovy 中,我可以写:
ext {
comp = 'node_exporter'
compVersion = '0.16.0'
compProject = 'prometheus'
arch = 'linux-amd64'
tarball = "v${compVersion}/${comp}-${compVersion}.${arch}.tar.gz"
downloadSrc = "https://github.com/${compProject}/${comp}/releases/download/${tarball}"
unzipDir = "${comp}-${compVersion}.${arch}"
}
Run Code Online (Sandbox Code Playgroud)
我发现在Kotlin DSL 中,我可以通过以下方式实现相同的功能:
val comp by extra { "filebeat" }
val compVersion by extra { "6.4.0" }
val arch by extra { "linux-x86_64" }
val tarball by extra { "${comp}-${compVersion}-${arch}.tar.gz" }
val downloadSrc by extra { "https://artifacts.elastic.co/downloads/beats/${comp}/${tarball}" }
val unzipDir …Run Code Online (Sandbox Code Playgroud) 在 groovy 中,您可以使用 来设置环境变量environment key value。例如run你可以这样做:
run {
environment DB_HOST "https://nowhere"
}
Run Code Online (Sandbox Code Playgroud)
如何在 Kotlin 的 build.gradle.kts 中完成此操作?
在调整我的构建文件,我似乎 已经遇到了一个错误有mainClassName:
thufir@dur:~/NetBeansProjects/HelloSeleniumWorld$
thufir@dur:~/NetBeansProjects/HelloSeleniumWorld$ ./gradlew clean ShadowJar --stacktrace
> Task :shadowJar FAILED
FAILURE: Build failed with an exception.
* What went wrong:
A problem was found with the configuration of task ':shadowJar'.
> No value has been specified for property 'mainClassName'.
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.tasks.TaskValidationException: A problem was found with the configuration …Run Code Online (Sandbox Code Playgroud) 具有以下多模块设置:
\n\nmulti\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 projA\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 build.gradle.kts\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 projB\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 build.gradle.kts\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build.gradle.kts\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 settings.gradle.kts\nRun Code Online (Sandbox Code Playgroud)\n\n内容如下(略):
\n\nsettings.gradle.kts
rootProject.name = "multi"\ninclude("projA", "projB")\nRun Code Online (Sandbox Code Playgroud)projA\\build.gradle.kts
dependencies {\n implementation("important-library:1.0")\n}\nRun Code Online (Sandbox Code Playgroud)projB\\build.gradle.kts
dependencies {\n implementation(project(":projA"))\n}\nRun Code Online (Sandbox Code Playgroud)为什么我无法importantlibrary:1.0从访问该内容projB?
什么有效:如果我有一个projA使用该库的类,即使从其中的一个类调用该类,它也可以完美地工作projB(因此间接访问有效)。importantlibrary:1.0从内部直接访问任何类都projB不起作用(未解析的参考)。
我在这里缺少什么?或者需要进行哪些设置才能使其正常工作?
\n\n摇篮版本:5.6.1
\n我的应用程序使用了几个 gradle.kts 脚本。我想设置一个对每个人都是全局的变量。
object Versions{
val kotlin_version = "1.3.60-eap-25"
}
Run Code Online (Sandbox Code Playgroud)
但它没有解决:
classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:$Versions.kotlin_version")
Run Code Online (Sandbox Code Playgroud) 基本上我想这样做:
也就是说,从一个 gradle 脚本中调用另一个函数中的一个函数。问题是我的 build.gradle 使用 Kotlin (build.gradle.kts),而我的函数所在的脚本仍然是 groovy。
我按照上面的 groovy-to-groovy 链接进行操作,但我无法使用 Kotlin DSL 使其工作。
在我的常规文件,functions.gradle 中,我有:
def buildVersionName() {
//Do some stuff
}
Run Code Online (Sandbox Code Playgroud)
和
ext {
buildVersionName = this.&buildVersionName
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的 build.gradle.kts 脚本中,我有:
apply(from = "functions.gradle")
project.ext.buildVersionName()
Run Code Online (Sandbox Code Playgroud)
当我同步时,我收到错误:
Unresolved reference: buildVersionName
Run Code Online (Sandbox Code Playgroud) 我需要打开一些 javafx 模块,并被告知将其放入build.gradle.kts. 我不知道如何使用 kotlin DSL 将 jvmArgs 添加到运行任务中?有人可以告诉我怎么做吗?
我试图了解在 Gradle、Kotlin DSL 中应用插件的所有方法。 这个问题回答了我的部分问题,但不是全部(我猜方法是在六年后添加的)。
我在我的一份build.gradle.kts文件中看到过这种确切的情况。
plugins{
`kotlin-dsl`
kotlin("jvm") version "1.6.10"
id("com.foo.bar.someplugin") version 1.2.3
}
apply("foo2.bar2.anotherplugin")
Run Code Online (Sandbox Code Playgroud)
哇,这是应用插件的四种不同方式,我根本不明白它们之间的关系。从另一个答案中,我知道这apply(...)是遗留方式,最终将被弃用,但是其他三个呢?
此外,我很困惑为什么`kotlin-dsl`甚至不需要版本。这是什么巫术?
最后,为了保持一致性,我想标准化插件块(让我们忽略它,apply(...)因为它是遗留功能),以便一切都使用id(...). 如何转换另外两个?
当我尝试在 Kotlin Multiplatform Mobile (KMM) 项目的共享模块中使用以下块添加 Firebase-bom 依赖项时,该词platform出现在红色错误文本中,并且 Gradle 构建失败并显示“未解析的引用:平台”。我该如何解决这个问题以便正确构建?
val androidMain by getting {
dependencies {
implementation(platform("com.google.firebase:firebase-bom:28.0.1"))
implementation("com.google.firebase:firebase-analytics-ktx")
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试切换到使用 Kotlin DSL 从对象 Kotlin 文件而不是 gradle 文件获取依赖项版本。当我进行 gradle 同步时,它无法解析引用,但我可以从 build.gradle.kts 单击文件(库)。我没有对 build.gradle 或 settings.gradle 进行任何其他更改。这个问题并不是杰克逊特有的,我在多个项目中尝试过这个问题,并在发布问题之前阅读文档并搜索答案。
我得到一个 build.gradle.kts:15:20: Unresolved reference: Libraries
这是我的项目的结构
build.gradle
settings.gradle
+buildSrc
build.gradle.kts
+src
+main
+kotlin
Libraries.kt
Run Code Online (Sandbox Code Playgroud)
这是我的 build.gradle.kts 文件
import org.gradle.kotlin.dsl.`kotlin-dsl`
plugins {
`kotlin-dsl`
java
`java-gradle-plugin`
}
repositories {
mavenCentral()
}
dependencies {
implementation(Libraries.Jackson.core)
// implementation(Libraries.anotherJackson)
}
Run Code Online (Sandbox Code Playgroud)
这是 kotlin Libraries.kt 文件
object Libraries {
val Jackson = JacksonLibraries
const val anotherJackson = "com.fasterxml.jackson.core:jackson-core:2.11.1"
}
object JacksonLibraries {
const val core = "com.fasterxml.jackson.core:jackson-core:${Versions.Jackson.core}"
}
object Versions { …Run Code Online (Sandbox Code Playgroud) android dependency-management kotlin build.gradle gradle-kotlin-dsl