未解决的参考:Gradle Kotlin DSL的sourceSets [英] Unresolved reference: sourceSets for Gradle Kotlin DSL

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

问题描述

尝试将现有的build.gradle迁移到Kotlin,并且在我的项目中遇到以下错误:

Trying to migrate my existing build.gradle to Kotlin and I am getting the following error in my project:

Script compilation error:

  Line 86:         from(sourceSets["main"].allSource)
                        ^ Unresolved reference: sourceSets

1 error

当我尝试定义sourcesJar任务时,错误来自我的subprojects块:

The error is coming from my subprojects block when I try to define the sourcesJar task:

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")
        // ...
    }
}

我正在使用以下内容:

  • 4.10.2级
  • 科林1.2.70

subprojects块之前的build.gradle.kts的开始部分:

Beginning part of the build.gradle.kts before subprojects block:

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
                }
            }
        }
    }
}

推荐答案

由于该插件必须在同一构建脚本中应用,因此Gradle无法知道该插件已应用,因此无法生成允许访问的扩展功能源集.

Since the plugin is applied imperatively in the same build script, Gradle can't know the plugin is applied and thus can't generate the extension functions allowing to access the source sets.

因此,您需要以编程方式获取源集:

So you need to get the source set programmatically:

project.the<SourceSetContainer>()["main"]

不使用

the<SourceSetContainer>()["main"]

否则,将在当前配置的任务而不是项目上解决the()功能.

otherwise the the() function will be resolved on the current task being configured instead of the project.

这篇关于未解决的参考:Gradle Kotlin DSL的sourceSets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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