Gradle6 settings.gradle.kts属性问题

Gradle 6 settings.gradle.kts properties problem

请帮助我了解 Gradle 6 中发生了什么变化,因此以下代码不再起作用(在 Gradle 5 中运行良好):

val artifactoryUser: String by settings
val artifactoryPassword: String by settings

pluginManagement {
    repositories {
        mavenLocal()
        maven {
            url = uri("https://internal-artifactory")
            credentials {
                username = artifactoryUser
                password = artifactoryPassword
            }
        }
    }
}

现在我有一个错误:"Unresolved reference: artifactoryUser"。

这个问题可以通过在 pluginManagement 块中移动属性声明来解决

pluginManagement {
val artifactoryUser: String by settings
val artifactoryPassword: String by settings
    repositories {
        mavenLocal()
        maven {
            url = uri("https://internal-artifactory")
            credentials {
                username = artifactoryUser
                password = artifactoryPassword
            }
        }
    }
}

但是我不明白为什么。

原因在Grade 6 upgrade notes中提到:

The pluginManagement block in settings scripts is now isolated

Previously, any pluginManagement {} blocks inside a settings script were executed during the normal execution of the script.

Now, they are executed earlier in a similar manner to buildscript {} or plugins {}. This means that code inside such a block cannot reference anything declared elsewhere in the script.

This change has been made so that pluginManagement configuration can also be applied when resolving plugins for the settings script itself.

的确,当从 Gradle 5.x 迁移到 Gradle 6.x.[=13 时,将 val 移到 pluginManagement 块中就可以了=]

val kotlinVersion: String by settings

pluginManagement {
...
}

至:

pluginManagement {
    val kotlinVersion: String by settings
...
}