我的一个构建脚本导入了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”块,它可以工作,但似乎必须有一种方法使它在一行中工作?
7条答案
按热度按时间ocebsuys1#
自Gradle 5.6起,您可以在
gradle.properties
文件中声明插件版本,并在plugins
块中引用这些属性。例如,
gradle.properties
文件:x1M4N1x中的x1M3N1x块:
请访问:https://github.com/gradle/gradle/issues/1697#issuecomment-506910915。
另请参阅:
disbfnqx2#
此处不能使用变量:
其中,plugin version和plugin id必须是常量、文字、字符串。不允许使用其他语句;它们存在将导致编译错误。
kninwzqo3#
这是一个旧的职位,但the bug仍然是开放的,我发现这个职位寻找变通办法。
看起来你已经意识到了这一点,并且实际上已经有了BoygeniusDexter's answer,但我认为这可能会帮助其他人像我一样找到这篇文章。以下解决方案基于Gradle文档,为我解决了这个问题:
dz6r00yl4#
除了@zhouji comment,你不仅可以在
gradle.properties
中指定版本,还可以通过编程方式定义它,比如:gblwokeq5#
摘要
1.忽略
plugin
块中的版本1.改为在
buildscript.dependencies
块中移动/指定版本1.在 that 块中使用
const
变量简而言之,
plugin
块需要字符串文字或属性,而dependency
块允许变量。最初的问题要求在“单行”中完成这一点,使用这种方法,您的插件块会更短,并且所有模块的所有依赖项都位于一个位置。根据您的目标,这可能比在“一行”中完成所有事情要好。完整示例
对于Android,我可以通过省略
plugin
块中的版本,然后在buildscript依赖项中将其指定为const
来实现。该块允许变量,而plugin块只允许字符串。在那里,我使用buildSrc
中的对象,因为这提供了最大的灵活性。同时将所有模块的所有依赖关系信息保存在一个文件中。所以我的设置如下所示:从那里,为了在一个地方指定任何插件版本作为变量,
app/build.gradle
文件省略了版本(以Kotlin为例,但同样的方法适用于任何插件):应用程序/构建版本.gradle
然后,buildscript依赖部分(通常在父
build.gradle
文件中,但也可以在同一文件中)提供版本USING A VARIABLE!项目/构建.gradle
最后,所有版本信息都在buildSrc中进行管理,并在Deps.kt文件中使用一个对象(有很多关于在Android项目中使用buildSrc的博客文章):
折旧kt
总的来说,我真的很喜欢这个设置。它很灵活,所有的依赖关系信息都在一个地方,甚至跨多个模块,最重要的是,它允许在输入依赖关系时在gradle文件中完成代码!
kuarbcqp6#
第七级
Gradle 7引入了version catalogs,用于声明依赖项并在项目/模块之间共享它们。
插件可以在目录中声明:
声明可以在
plugins
块中引用:该版本也可以在
plugins
块中引用:如需详细信息,请参阅version catalogs文件。
-
被用作依赖项名称中的分隔符。在使用端,则使用.
,因为-
是groovy语言的减号运算符的符号,不能在属性名称中使用。因此,声明端和使用端的分隔符是不同的--这让我一开始感到困惑。*ykejflvf7#
第一个