在Gradle中,如何使用插件版本的变量?

46scxncf  于 2022-11-14  发布在  其他
关注(0)|答案(7)|浏览(204)

我的一个构建脚本导入了nebula插件:

plugins {
  id 'nebula.ospackage' version '3.5.0'
}

我一直在将我的所有版本信息移动到一个所有项目都可以访问的单独文件中,我想知道什么是正确的语法来转换为类似于:

plugins {
  id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
}

当我尝试使用“gradle clean build”运行上面的程序时,我得到了以下错误:
构建文件“build. gradle”:2:参数列表必须正好是1个文本非空字符串
有关https://docs.gradle.org/2.7/userguide/plugins.html#sec:plugins_block插件{}块的信息,请访问www.example.com
@第2行,第33列。id 'nebula.ospackage'版本“$版本.nebula_gradle_ospackage_plugin”
链接的文章展示了我如何使用“buildscript”块,它可以工作,但似乎必须有一种方法使它在一行中工作?

ocebsuys

ocebsuys1#

自Gradle 5.6起,您可以在gradle.properties文件中声明插件版本,并在plugins块中引用这些属性。
例如,gradle.properties文件:

springBootVersion=2.2.0.RELEASE

x1M4N1x中的x1M3N1x块:

plugins {
    id "org.springframework.boot" version "${springBootVersion}"
}

请访问:https://github.com/gradle/gradle/issues/1697#issuecomment-506910915。
另请参阅:

  1. https://docs.gradle.org/5.6/release-notes.html#central-management-of-plugin-versions-with-settings-script
  2. https://docs.gradle.org/5.6/userguide/plugins.html#sec:plugin_management
disbfnqx

disbfnqx2#

此处不能使用变量:
其中,plugin version和plugin id必须是常量、文字、字符串。不允许使用其他语句;它们存在将导致编译错误。

kninwzqo

kninwzqo3#

这是一个旧的职位,但the bug仍然是开放的,我发现这个职位寻找变通办法。
看起来你已经意识到了这一点,并且实际上已经有了BoygeniusDexter's answer,但我认为这可能会帮助其他人像我一样找到这篇文章。以下解决方案基于Gradle文档,为我解决了这个问题:

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    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
    // ...
}
dz6r00yl

dz6r00yl4#

除了@zhouji comment,你不仅可以在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}"
}
gblwokeq

gblwokeq5#

摘要

1.忽略plugin块中的版本
1.改为在buildscript.dependencies块中移动/指定版本
1.在 that 块中使用const变量
简而言之,plugin块需要字符串文字或属性,而dependency块允许变量。最初的问题要求在“单行”中完成这一点,使用这种方法,您的插件块会更短,并且所有模块的所有依赖项都位于一个位置。根据您的目标,这可能比在“一行”中完成所有事情要好。

完整示例

对于Android,我可以通过省略plugin块中的版本,然后在buildscript依赖项中将其指定为const来实现。该块允许变量,而plugin块只允许字符串。在那里,我使用buildSrc中的对象,因为这提供了最大的灵活性。同时将所有模块的所有依赖关系信息保存在一个文件中。所以我的设置如下所示:

├── project/
│   ├── build.gradle
|   └── app/
|       └── build.gradle
|   └── buildSrc/
|       └── build.gradle.kts
|       └── src/main/java/com/example/package/Deps.kt

从那里,为了在一个地方指定任何插件版本作为变量,app/build.gradle文件省略了版本(以Kotlin为例,但同样的方法适用于任何插件):

应用程序/构建版本.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文件中,但也可以在同一文件中)提供版本USING A VARIABLE!

项目/构建.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的博客文章):

折旧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文件中完成代码!

kuarbcqp

kuarbcqp6#

第七级
Gradle 7引入了version catalogs,用于声明依赖项并在项目/模块之间共享它们。
插件可以在目录中声明:

// settings.gradle

dependencyResolutionManagement {
    versionCatalogs {
        libs {
            version('android-plugins', '7.3.0')
            plugin('android-application', 'com.android.application').versionRef('android-plugins')

            // A library declaration using the same version.
            library('some-library', 'com.group', 'artifact').versionRef('android-plugins')
        }
    }
}

声明可以在plugins块中引用:

// build.gradle

plugins {
    alias(libs.plugins.android.application) apply false
}

该版本也可以在plugins块中引用:

// build.gradle

plugins {
    id 'com.android.application' version libs.versions.android.plugins apply false
}

如需详细信息,请参阅version catalogs文件。

  • 注意:在声明中,-被用作依赖项名称中的分隔符。在使用端,则使用.,因为-是groovy语言的减号运算符的符号,不能在属性名称中使用。因此,声明端和使用端的分隔符是不同的--这让我一开始感到困惑。*

相关问题