javagradlebuild.gradleproperties-filesystem-properties

How to update java project properties with gradle commandline


I'm working on a java project and currently loading properties by:

 properties.load(ConfigProperty.class.getClassLoader().getResourceAsStream("project.properties"));

But sometimes I also need to run from command line and pass values to override the values in project.properties.

I tried something like this(user is a key in properties), but doesn't really work.

gradle gatlingRun -Duser=1

What should be the proper way to do it?

Is it the way properties.load limiting it to only read from file? Should change this part?


Solution

  • You can't use the getResourceAsStream method then. This can't be overridden with system properties as you show in your example.

    It's not clear what your overall environment is but systems like Quarkus include a configuration subsystem to help you do exactly this. You can use system properties, environment variables, and files.

    For smaller command line type applications I've used Apache Commons configuration that also can read from a variety of places. It's simple and has a very large number of sources.