将属性传递给gradle构建 [英] Passing properties to a gradle build

查看:142
本文介绍了将属性传递给gradle构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我承认我对gradle很新,但我并不期望无法理解像下面这个例子那样简单的事情。我可以阅读关于使用hasProperty(String propertyName)调用来检查项目属性是否已设置的gradle文档,我坐在这里,不知道为什么这么基本的东西不起作用。



我相信我的头脑一定是这么蚂蚁一样的定位,以确保我缺少一些普通的基本元素。 $ c> task printSystem()<< {
println system
printlnhas property:+ hasProperty(system)
}

并使用以下命令调用该任务:

  $ gradle printSystem -Psystem = mySystem 
mySystem
有财产:null

所以我的问题是:


  1. 为什么系统打印出来,但hasProperty返回 null

  2. 我该如何检查名为system的项目属性的存在?

  3. 是否有不同的方式来测试项目属性而不是系统属性?
  4. >
  5. 您如何从命令行传递系统属性?

这是Gradle文档并且我相信我正在阅读它。

19.2.1。检查项目属性



只需使用名称就可以访问构建脚本中的项目属性,就像使用变量一样。如果此属性不存在,则会抛出异常并且构建将失败。如果您的构建脚本依赖于用户可能设置的可选属性(可能位于gradle.properties文件中),则在访问它们之前,您需要检查是否存在。你可以通过使用方法hasProperty('propertyName')来完成这个工作,它返回true或false。

hasProperty 关于项目实例 - 没有它, hasProperty 是在某些地方上下文中调用。下面的例子工作:

  task printSystem()<< {
println system
printlnhas property:+ project.hasProperty(system)
}




  1. 因为不存在属性( system 未在脚本中定义)从项目实例中 。如果您不会传递系统属性,则会在 println 中抛出异常。

  2. project.hasProperty('propName')

  3. 不知道我是否理解正确,但您可以访问项目属性通过项目实例和系统属性通过系统类来实现。使用 -D 开关 - gradle -Dprop =值


I admit I am quite new to gradle but I did not expect to be unable to understand something as simple as the example below. I can read the gradle documentation about checking whether a project property have been set or not using a hasProperty(String propertyName) call and I am sitting here and have no idea why something so basic does not work.

I believe my mind must be so much "ant like" oriented that for sure I am missing something ordinary basic

task printSystem() << {
    println system
    println "has property: " + hasProperty("system")
}

and invoking that task with the command below:

$gradle printSystem -Psystem=mySystem
mySystem
has property: null

So my questions would be:

  1. Why system is printed out but hasProperty returns null?
  2. How should I check for the existence of the project property called "system"?
  3. Is there a different way for testing for a project property as opposed to a system property?
  4. How would you pass a system property from the command line?

This is from, the gradle documentation and I believe I am reading it right

19.2.1. Checking for project properties

You can access a project property in your build script simply by using its name as you would use a variable. If this property does not exist, an exception will be thrown and the build will fail. If your build script relies on optional properties the user might set, perhaps in a gradle.properties file, you need to check for existence before you access them. You can do this by using the method hasProperty('propertyName') which returns true or false.

解决方案

You need to explicitly invoke hasProperty on the project instance - without it, hasProperty is invoked on some local context. The following example works:

task printSystem() << {
    println system
    println "has property: " + project.hasProperty("system")
}

  1. Because non-existing properties (system is not defined in the script) are taken from the project instance. If you won't pass the system property, an exception will be thrown on println.
  2. project.hasProperty('propName')
  3. Not sure if I understood right, but you can access project properties via the project instance and system properties via the System class.
  4. Using -D switch - gradle -Dprop=value

这篇关于将属性传递给gradle构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆