问题描述
如何检查 build.gradle
文件中是否正确设置了系统属性?
How do I check to see if a System property has been properly set from within a build.gradle
file?
我的根目录中有一个 gradle.properties
文件,看起来像这样:
I have a gradle.properties
file in my root directory that looks something like this:
systemProp.user=exampleUsername
systemProp.password=examplePassword
我想验证这些属性的存在,例如:
I would like to validate the existence of these properties, something like:
if (!System.hasProperty('user')) {
throw new InvalidUserDataException("No user found in `gradle.properties`; please set one.")
}
某些代码
我尝试了以下操作:
Some Code
I've tried the following:
-
project.hasProperty('user')
返回false
System.properties.get('user')
返回 exampleUsername
System.hasProperty('user')
返回 null
System.properties.get('user')== true
对于非false返回true值
System.properties.get('user') == true
returns true for non-falseyvalues
推荐答案
您可以根据需要使用 System.properties.containsKey('your_property')
.如果具有提供的键的属性存在,则返回true,否则返回false.这样的实现可能如下所示:
You can use System.properties.containsKey('your_property')
for your purpose. It returns true if a property with the provided key exists, false otherwise. An implementation of this could look like the following:
if (!System.properties.containsKey('user')) {
throw new InvalidUserDataException("No user found in `gradle.properties`; please set one.")
}
这篇关于检查是否已设置Gradle系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!