Gradle 6 settings.gradle.kts 属性问题

huangapple go评论60阅读模式
英文:

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

但我不明白为什么会这样。

英文:

please help me understand what was changed in Gradle 6 so the following code doesn't work anymore (worked well in 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
            }
        }
    }
}

Now I have an error: "Unresolved reference: artifactoryUser".

This problem can be solved by moving properties declaration inside the pluginManagement block

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

But I don't understand why.

答案1

得分: 5

这是关于它的原因在Grade 6升级说明中提到:

> 在设置脚本中的pluginManagement块现在被隔离
>
> 以前,在设置脚本中的任何pluginManagement {}块在脚本的正常执行期间都会被执行。
>
> 现在,它们在类似于buildscript {}或plugins {}的方式下更早执行。这意味着在这样的块内部的代码不能引用脚本中其他地方声明的内容。
>
> 这个改变是为了在解析设置脚本本身的插件时也可以应用pluginManagement配置。

英文:

The reason for it is mentioned in the 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.

答案2

得分: 1

val 移动到 pluginManagement 块内部可以解决问题,从 Gradle 5.x 迁移到 Gradle 6.x 时。

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

Indeed, moving the val inside the pluginManagement block does the trick, when migrating from Gradle 5.x to Gradle 6.x.

val kotlinVersion: String by settings

pluginManagement {
...
}

to:

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

huangapple
  • 本文由 发表于 2020年1月6日 21:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定