在gradle中,如何使用一个插件版本的变量?
In gradle, how to use a variable for a plugin version?
我的一个构建脚本导入了那个 nebula 插件:
plugins {
id 'nebula.ospackage' version '3.5.0'
}
我一直在将我所有的版本信息移动到一个所有项目都可以访问的单独文件中,我想知道转换为以下内容的正确语法是什么:
plugins {
id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
}
当我用 "gradle clean build" 尝试 运行 上述操作时,我收到以下错误:
build file 'build.gradle': 2: argument list must be exactly 1 literal
non empty string
See
https://docs.gradle.org/2.7/userguide/plugins.html#sec:plugins_block
for information on the plugins {} block
@ line 2, column 33.
id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
链接的文章显示了我如何使用 "buildscript" 块,它有效,但似乎必须有一种方法可以在一行中完成这项工作?
这里不能使用变量:
Where «plugin version» and «plugin id» must be constant, literal,
strings. No other statements are allowed; their presence will cause a
compilation error.
这是一个旧的 post,但 the bug 仍然开放,我发现这个 post 正在寻找解决方法。
看来你已经意识到了这一点,而且实际上 , but I think this may help others finding this post like I did. The following workaround is based on the Gradle docs 并为我解决了这个问题:
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
// and other plugins
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
}
// but the one with the variable version is applied the old way:
apply plugin: 'org.springframework.boot'
// We can use the variable in dependencies, too:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
// ...
}
从 Gradle 5.6 开始,您可以在 gradle.properties
文件中声明您的插件版本,并在 plugins
块中引用这些属性。
例如gradle.properties
文件:
springBootVersion=2.2.0.RELEASE
build.gradle
中的 plugins
块:
plugins {
id "org.springframework.boot" version "${springBootVersion}"
}
参见:https://github.com/gradle/gradle/issues/1697#issuecomment-506910915。
另请参阅:
总结
- 省略
plugin
块中的版本
- Move/specify
buildscript.dependencies
块中的版本,而不是
- 在 that 块中使用一个
const
变量 的变量
简而言之,plugin
块需要字符串文字或属性,而 dependency
块允许变量。最初的问题要求在 "single line" 中执行此操作,使用这种方法,您的插件块会更短,并且所有模块的所有依赖项都位于一个位置。根据您的目标,这可能比在 "one line."
中完成所有事情更好
完整示例
对于 Android,我能够通过省略 plugin
块中的版本然后在构建脚本依赖项中将其指定为 const
来做到这一点。该块允许变量,而插件块只允许字符串文字。从那里开始,我在 buildSrc
中使用了一个对象,因为它提供了最大的灵活性,同时将所有模块的所有依赖信息保存在一个文件中。所以我的设置如下所示:
├── project/
│ ├── build.gradle
| └── app/
| └── build.gradle
| └── buildSrc/
| └── build.gradle.kts
| └── src/main/java/com/example/package/Deps.kt
从那里开始,为了在一个地方将任何插件版本指定为变量,app/build.gradle
文件省略了版本(以 kotlin 为例,但相同的方法适用于任何插件):
app/build.gradle
plugins {
id 'kotlin-android' // omit version property here but provide it in buildscript dependencies
...
}
...
dependencies {
...
// Dagger
implementation Deps.Dagger.ANDROID_SUPPORT
kapt Deps.Dagger.ANDROID_PROCESSOR
kapt Deps.Dagger.COMPILER
}
然后 buildscript 依赖部分(通常在父 build.gradle
文件中,但也可以在同一文件中)提供使用变量的版本!
project/build.gradle
import com.example.package.Deps // object in the buildSrc folder with all version info
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Deps.kotlinVersion}"
}
}
最后,所有版本信息都在 buildSrc 中管理,在 Deps.kt 文件中有一个对象(那里有很多关于在 Android 项目中使用 buildSrc 的博客文章):
Deps.kt
object Deps {
// For use in the top-level buildscript dependency section which only works with a constant rather than `Deps.Kotlin.version`
const val kotlinVersion = "1.3.72"
object Kotlin : Version(kotlinVersion) {
val STDLIB = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$version"
}
object Dagger : Version("2.25.2") {
val ANDROID_SUPPORT = "com.google.dagger:dagger-android-support:$version"
val ANDROID_PROCESSOR = "com.google.dagger:dagger-android-processor:$version"
val COMPILER = "com.google.dagger:dagger-compiler:$version"
}
}
open class Version(@JvmField val version: String)
总的来说,我非常喜欢这个设置。它很灵活,所有依赖信息都在一个地方,甚至跨多个模块,最重要的是,它允许在输入依赖项时在 gradle 文件中完成代码!
除了之外,您不仅可以在gradle.properties
中指定版本,还可以通过编程方式定义它,如:
buildscript {
ext {
kotlinVersion = "${JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16) ? '1.5.32' : '1.4.32'}"
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version "${kotlinVersion}"
}
// gradle.properties
springBootVersion=2.6.7
// settings.gradle.kts
pluginManagement {
val springBootVersion: String by settings
plugins {
id("org.springframework.boot") version springBootVersion
}
}
// build.gradle.kts
plugins {
id("org.springframework.boot")
}
我的一个构建脚本导入了那个 nebula 插件:
plugins {
id 'nebula.ospackage' version '3.5.0'
}
我一直在将我所有的版本信息移动到一个所有项目都可以访问的单独文件中,我想知道转换为以下内容的正确语法是什么:
plugins {
id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
}
当我用 "gradle clean build" 尝试 运行 上述操作时,我收到以下错误:
build file 'build.gradle': 2: argument list must be exactly 1 literal non empty string
See https://docs.gradle.org/2.7/userguide/plugins.html#sec:plugins_block for information on the plugins {} block
@ line 2, column 33. id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
链接的文章显示了我如何使用 "buildscript" 块,它有效,但似乎必须有一种方法可以在一行中完成这项工作?
这里不能使用变量:
Where «plugin version» and «plugin id» must be constant, literal, strings. No other statements are allowed; their presence will cause a compilation error.
这是一个旧的 post,但 the bug 仍然开放,我发现这个 post 正在寻找解决方法。
看来你已经意识到了这一点,而且实际上
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
// and other plugins
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
}
// but the one with the variable version is applied the old way:
apply plugin: 'org.springframework.boot'
// We can use the variable in dependencies, too:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
// ...
}
从 Gradle 5.6 开始,您可以在 gradle.properties
文件中声明您的插件版本,并在 plugins
块中引用这些属性。
例如gradle.properties
文件:
springBootVersion=2.2.0.RELEASE
build.gradle
中的 plugins
块:
plugins {
id "org.springframework.boot" version "${springBootVersion}"
}
参见:https://github.com/gradle/gradle/issues/1697#issuecomment-506910915。
另请参阅:
总结
- 省略
plugin
块中的版本 - Move/specify
buildscript.dependencies
块中的版本,而不是 - 在 that 块中使用一个
const
变量 的变量
简而言之,plugin
块需要字符串文字或属性,而 dependency
块允许变量。最初的问题要求在 "single line" 中执行此操作,使用这种方法,您的插件块会更短,并且所有模块的所有依赖项都位于一个位置。根据您的目标,这可能比在 "one line."
完整示例
对于 Android,我能够通过省略 plugin
块中的版本然后在构建脚本依赖项中将其指定为 const
来做到这一点。该块允许变量,而插件块只允许字符串文字。从那里开始,我在 buildSrc
中使用了一个对象,因为它提供了最大的灵活性,同时将所有模块的所有依赖信息保存在一个文件中。所以我的设置如下所示:
├── project/
│ ├── build.gradle
| └── app/
| └── build.gradle
| └── buildSrc/
| └── build.gradle.kts
| └── src/main/java/com/example/package/Deps.kt
从那里开始,为了在一个地方将任何插件版本指定为变量,app/build.gradle
文件省略了版本(以 kotlin 为例,但相同的方法适用于任何插件):
plugins {
id 'kotlin-android' // omit version property here but provide it in buildscript dependencies
...
}
...
dependencies {
...
// Dagger
implementation Deps.Dagger.ANDROID_SUPPORT
kapt Deps.Dagger.ANDROID_PROCESSOR
kapt Deps.Dagger.COMPILER
}
然后 buildscript 依赖部分(通常在父 build.gradle
文件中,但也可以在同一文件中)提供使用变量的版本!
import com.example.package.Deps // object in the buildSrc folder with all version info
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Deps.kotlinVersion}"
}
}
最后,所有版本信息都在 buildSrc 中管理,在 Deps.kt 文件中有一个对象(那里有很多关于在 Android 项目中使用 buildSrc 的博客文章):
Deps.ktobject Deps {
// For use in the top-level buildscript dependency section which only works with a constant rather than `Deps.Kotlin.version`
const val kotlinVersion = "1.3.72"
object Kotlin : Version(kotlinVersion) {
val STDLIB = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$version"
}
object Dagger : Version("2.25.2") {
val ANDROID_SUPPORT = "com.google.dagger:dagger-android-support:$version"
val ANDROID_PROCESSOR = "com.google.dagger:dagger-android-processor:$version"
val COMPILER = "com.google.dagger:dagger-compiler:$version"
}
}
open class Version(@JvmField val version: String)
总的来说,我非常喜欢这个设置。它很灵活,所有依赖信息都在一个地方,甚至跨多个模块,最重要的是,它允许在输入依赖项时在 gradle 文件中完成代码!
除了gradle.properties
中指定版本,还可以通过编程方式定义它,如:
buildscript {
ext {
kotlinVersion = "${JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16) ? '1.5.32' : '1.4.32'}"
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version "${kotlinVersion}"
}
// gradle.properties
springBootVersion=2.6.7
// settings.gradle.kts
pluginManagement {
val springBootVersion: String by settings
plugins {
id("org.springframework.boot") version springBootVersion
}
}
// build.gradle.kts
plugins {
id("org.springframework.boot")
}