检查是否已设置Gradle系统属性 [英] Checking that Gradle system properties have been set

查看:69
本文介绍了检查是否已设置Gradle系统属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查 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-falsey values

推荐答案

您可以根据需要使用 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系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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