在gradle kotlin中访问任务的属性

我一直在试图了解如何使用Kotlin作为Gradle的脚本语言,并且遇到了我需要在startScripts任务中访问类路径属性的问题,但是我无法弄清楚如何解决这个问题。

我的假设是,我没有得到我期望的类型,因此我需要将它转换为该类型(在我的情况下它应该是CreateStartScripts)。 现在有没有人如何将startScript任务转换为lambda方法中的CreateStartScripts?

我的脚本如下所示:

import groovy.lang.GroovyObject import org.springframework.boot.gradle.plugin.* import org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact buildscript { dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE") } } val artifactoryUrl by project val artifactoryRepo by project plugins { java application idea } apply<SpringBootPlugin>() application { mainClassName = "com.emstar.Application" } java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } distributions { getByName("main") { contents { into("/") } } } tasks { "distTar" { val artifacts = configurations.archives.artifacts val distTar = tasks.findByName("distTar") artifacts.remove(artifacts.find{ (it as ArchivePublishArtifact).archiveTask == distTar }) enabled = false } "distZip" { dependsOn(createLogDir) } "startScripts" { // is a type of CreateStartScripts // classpath += files("src/dist/conf") doLast { // val windowsScriptFile = project.file(getWindowsScript()) // val unixScriptFile = project.file(getUnixScript()) // windowsScriptFile.text = windowsScriptFile.text.replace("set CLASSPATH=%APP_HOME%\\lib", "set CLASSPATH=%APP_HOME%\\conf;%APP_HOME%\\lib") // unixScriptFile.text = unixScriptFile.text.replace("CLASSPATH=$APP_HOME/lib", "CLASSPATH=$APP_HOME/conf$:APP_HOME/lib") } } } dependencies { compile("org.springframework.boot:spring-boot-starter-web") { exclude("org.springframework.boot", "spring-boot-starter-logging") exclude("org.springframework.boot", "spring-boot-starter-tomcat") } compile("org.springframework.boot:spring-boot-starter-log4j2") compile("org.springframework.boot:spring-boot-starter-jetty") compile("org.springframework.boot:spring-boot-starter-jdbc") { exclude("org.springframework.boot", "spring-boot-starter-logging") exclude("org.springframework.boot", "spring-boot-starter-tomcat") } compile("org.springframework.boot:spring-boot-starter-security") { exclude("org.springframework.boot", "spring-boot-starter-logging") } compile("org.springframework.boot:spring-boot-starter-security") { exclude("org.springframework.boot", "spring-boot-starter-logging") } compile("org.springframework.ldap:spring-ldap-core") compile("org.springframework.security:spring-security-ldap") compile("com.google.guava:guava:22.0") compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.7") compile("com.fasterxml.jackson.core:jackson-databind:2.8.7") testCompile("junit:junit") testCompile("org.assertj:assertj-core") testCompile("org.mockito:mockito-core") testCompile("org.springframework.boot:spring-boot-starter-test") } 

当我修改startScripts到这个:

 "startScripts" { it -> val concrete = it as CreateStartScripts concrete.classpath += files("src/dist/conf") } 

发生以下错误:

 e: C:\source\service\build.gradle.kts:82:5: None of the following functions can be called with the arguments supplied: public final operator fun <U : Task!> String.invoke(type: KClass<???>): ??? defined in org.gradle.script.lang.kotlin.NamedDomainObjectContainerScope public final operator inline fun String.invoke(configuration: Task!.() -> Unit): Task! defined in org.gradle.script.lang.kotlin.NamedDomainObjectContainerScope e: C:\source\service\build.gradle.kts:84:9: Cannot infer a type for this parameter. Please specify it explicitly. FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'service'. > Could not open cache directory akz6f8omykk1xzv7r475p6wwq (C:\Users\matanaka\.gradle\caches\4.0\gradle-script-kotlin\akz6f8omykk1xzv7r475p6wwq). > Internal error: unable to compile script, see log for details * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

有人遇到这个问题?

我用kotlin-dsl 0.9运行Gradle 4.0

提前致谢

编辑

我发现一个解决方案似乎更像是一个解决方法,所以如果有人有更有效的方式来写它将不胜感激。

我找到的解决方案是按名称搜索任务并投射。

 var startScripts = findByName("startScripts") as CreateStartScripts startScripts.apply { classpath += files("src/dist/conf") doLast { val windowsScriptFile = project.file(windowsScript) val unixScriptFile = project.file(unixScript) windowsScriptFile.writeText(windowsScriptFile.readText() .replace("set CLASSPATH=%APP_HOME%\\lib", "set CLASSPATH=%APP_HOME%\\conf;%APP_HOME%\\lib")) unixScriptFile.writeText(unixScriptFile.readText() .replace("CLASSPATH=\$APP_HOME/lib", "CLASSPATH=\$APP_HOME/conf:APP_HOME/lib")) } }