How to declare / change Java variables via gradle script

Is it possible to declare or modify Java variables accessible through code using gradle?

I found Is it possible to declare a variable in Gradle as applicable in Java? and it tells me how to declare variables available through Java code for Android projects.

Can you do the same with "normal" Java projects? I want to set a variable depending on the task being performed.

Thank you for your help!

+3


source to share


1 answer


Thanks to @PeterNiederwieser I solved my problem:

In my gradle.build I am using:

task usernameMaxio(type: Copy) {
  from 'src/main/resources/properties'
  into 'build/resources/main/properties'
  expand([
    username: 'Maxio'
  ])
}

      

and I access it via:



Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream("properties/app.properties");
try {
    properties.load(stream);
} catch (IOException | NullPointerException e) {
    WebOptionPane.showMessageDialog(null, "Couldn't read app.properties", "Fehler",
                                    WebOptionPane.ERROR_MESSAGE);
    e.printStackTrace();
    return;
}
name = properties.getProperty("username");

      

And in my app.properties:

username=${username}

      

+4


source







All Articles