【发布时间】:2018-10-24 18:11:47
【问题描述】:
尝试将我现有的 build.gradle 迁移到 Kotlin,但我的项目中出现以下错误:
Script compilation error:
Line 86: from(sourceSets["main"].allSource)
^ Unresolved reference: sourceSets
1 error
当我尝试定义sourcesJar 任务时,错误来自我的subprojects 块:
subprojects {
val sourcesJar by tasks.registering(Jar::class) {
classifier = "sources"
from(sourceSets["main"].allSource) // error here
}
configure<PublishingExtension> {
publications {
register("mavenJava", MavenPublication::class) {
from(components["java"])
artifact(sourcesJar.get())
}
}
}
val implementation by configurations
val compileOnly by configurations
val annotationProcessor by configurations
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
compileOnly("org.springframework.boot:spring-boot-autoconfigure")
// ...
}
}
我正在使用以下内容:
- Gradle 4.10.2
- Kotlin 1.2.70
build.gradle.kts 在subprojects 块之前的开始部分:
import com.diffplug.gradle.spotless.KotlinExtension
import com.diffplug.gradle.spotless.SpotlessExtension
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val kotlinVersion: String by extra
val springBootVersion: String by extra
buildscript {
val kotlinVersion: String by extra { "1.2.70" }
val springBootVersion: String by extra { "2.0.6.RELEASE" }
repositories {
maven {
val nexusPublicRepoURL: String by project
url = uri(nexusPublicRepoURL)
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
classpath("com.diffplug.spotless:spotless-plugin-gradle:3.9.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")
classpath("org.jetbrains.kotlin:kotlin-compiler-embeddable:1.2.70")
}
}
allprojects {
val projectGroup: String by project
group = projectGroup
apply(plugin = "kotlin")
apply(plugin = "java-library")
apply(plugin = "maven-publish")
apply(plugin = "kotlin-spring")
apply(plugin = "com.diffplug.gradle.spotless")
apply(plugin = "io.spring.dependency-management")
configure<DependencyManagementExtension> {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:$springBootVersion")
mavenBom("org.springframework.cloud:spring-cloud-dependencies:Finchley.SR1")
}
}
repositories {
maven {
val nexusPublicRepoURL: String by project
url = uri(nexusPublicRepoURL)
}
}
tasks.existing(KotlinCompile::class) {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
configure<SpotlessExtension> {
kotlin {
ktlint()
}
}
configure<PublishingExtension> {
repositories {
maven {
val nexusReleaseRepoURL: String by project
val nexusSnapshotRepoURL: String by project
val nexusUsername: String by project
val nexusPassword: String by project
val version = if ((project.version as String).contains("SNAPSHOT")) nexusReleaseRepoURL else nexusSnapshotRepoURL
url = uri(version)
credentials {
username = nexusUsername
password = nexusPassword
}
}
}
}
}
【问题讨论】:
-
IIRC,你应该可以使用
the<SourceSetContainer>()["main"] -
@JBNizet 这样做会产生一个新错误
Extension of type 'SourceSetContainer' does not exist. Currently registered extension types: [ExtraPropertiesExtension] -
您是否在每个子项目上应用了一个插件,该插件实际上将 sourceSets 添加到项目中(例如 java 插件)
-
我在
allprojects块中应用了java-library插件。请参阅我的编辑,其中显示了我的build.gradle.kts的其余部分。 -
对不起:简单的范围界定问题。
the()被解析为this.the(),即Task::the()。将其更改为project.the<SourceSetContainer>()["main"]。