Checking that Gradle system properties have been set - gradle

The Question
How do I check to see if a System property has been properly set from within a build.gradle file?
The Situation
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') returns false
System.properties.get('user') returns exampleUsername
System.hasProperty('user') returns null
System.properties.get('user') == true returns true for non-falsey
values

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.")
}

Related

Quarkus ConfigMapping: how to map properties with keys with and without next level at the same time, like "fields" and "fields.excludes"

Converting my app to use Quarkus #ConfigMapping is not easy, especially when the properties names may be used by other apps and you are not free to change them.
I have two keys like fields(set of string) and fields.excludes(another set of string). I want to know what I can do to map both of them. I tried:
Set<String> fields();
#WithName("fields")
FieldsExcludes fieldsToExclude();
interface FieldsExcludes {
Set<String> excludes();
}
...
No compilation error, but runtime error:
Caused by: java.util.NoSuchElementException: SRCFG00014: The config property app.operator.0000001.endpoint.0.fields.exclude is required but it could not be found in any config source
at io.smallrye.config.SmallRyeConfig.getIndexedValues(SmallRyeConfig.java:114)
at io.smallrye.config.SmallRyeConfig.getValues(SmallRyeConfig.java:106)
...
Of course, I think Quarkus cannot tell if "fields" is a field of set, or a field of type FieldsExcludes. But this kind of configs are valid in YAML.
fields: [a,b,c,d]
fields.exclude: [e]
(if I break line at 2nd line after fields it's not valid; but if I put it like this it's valid, at least for Intellij IDEA)
See here: https://quarkus.io/guides/config-yaml#configuration-key-conflicts, in the doc it's stated that it's valid; I can use a ~ as a null key. So fields as a list and fields.exclude as another object should be supported.
OK at last it seems already supported; the error is due to it's not defined as "Optional<>" so Quarkus can find it in one place but cannot in another. (YAML contains 2 ConfigDetail, one of them does not have excludes, and it has no default value)
The final code is like this:
interface ConfigDetail {
Set<String> fields();
#WithName("fields")
Optional<FieldsToExclude> fieldsToExclude();
}
...
interface FieldsToExclude {
Set<String> exclude();
}
fields:
~: [a,b,c,d]
exclude: [e]
...
fields:
~: [a,b,c,d,e]
...

Gradle - Cannot set property on null object

I have a .gradle in which I want to set some project properties if a condition is true.
def isRelease = project.getProperty('isRelease')
if (isRelease) {
println 'Detected a release'
project.properties.'releaseCenter'.'uploadURL' = project.properties.'uatUploadURL'
}
The output is:
A problem occurred evaluating root project 'tasks'.
> Cannot set property 'uploadURL' on null object
I think it has to do with the '' around the object name but I couldn't get it to work.
Any help is much appreciated.
You call project.properties: https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#getProperties--
which returns a Map. You then call properties.'releaseCenter' which is equivalent to doing properties.get("releaseCenter") which returns null. You try to get a property on a null object which is exactly the error you are seeing.
Possible solutions:
Fix your Gradle configuration (some plugin may be adding this property)
Define the property yourself.

change a autoloaded variable in codeigniter framework

I need to change a variable which is saved over the autoloader function.
Inside of a existing class (named "app") I can check if the variable is set.
$this->options[$name];
Now I want to change the value of these autoloaded variable, inside my app.php class in this way
---
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
...
for this, i get the correct new value.
The problem is, that it seems that this new value is not updated for the rest of the script!? If i access this variable later, i get the old value!?
What i do wrong?
The only "variables" that persist across different pages/loads/refreshes .etc. are those that are stored in a session variable, cookie or database.
To reiterate; this:
echo"old value: ".$this->options[$name].")";
$this->options[$name] = $value;
echo "new value:".$this->options[$name];
return true;
only affects the current instance at run-time (you can look at this as a page view). It will not persist. Same goes for config value changes in CodeIgniter.
Your only method is using some sort of file/database based storage.

How to create local variables at runtime for any keyword test in TestComplete

I need to create a set of local variables at the beginning of a Keyword test and then use them later while executing the Test.
Is there any possibility to create local variables dynamically as like project variables which can be created dynamically.
Project.variables.<variable_name> = "project_variable_value"
in the similar fashion can we create any variable associated to any keyword test
Keywordtests.<generic_keyword_test_name>.variables.<variable_name> = "local_variable_value"
Sure, you can do this. Please see this example:
function Test11()
{
if (KeywordTests.Test1.Variables.VariableExists("MyVariable") == false) {
KeywordTests.Test1.Variables.AddVariable("MyVariable", "String");
}
KeywordTests.Test1.Variables.MyVariable = "test value";
Log.Message(KeywordTests.Test1.Variables.MyVariable);
}
Information on the AddVariable method can be found in the AddVariable Method help topic.

Boolean property in Puppet provider

I am writing a Puppet provider and I need a boolean property. I declared it with:
newproperty(:no_sync, :boolean => true) do
desc "Whether to omit syncing the file after every logging, ony when action_type is file."
end
Then I need to declare the no_sync function in the provider which should return true or false. However, when I do this, it seems the values returned as not properly interpreted by Puppet. I've tried returning strings (:true and :false respectively), but as a result they always get interpreted as true (which is quite logical).
How are we supposed to declare boolean properties in a Puppet provider?
Returning the symbols :true and :false from the provider method is the correct thing to do.
You could take a look at the macauthorization source code for an example of how the type is defined. The provider for this type returns :true or :false.

Resources