How to configure lightbend/typesafeConfig via command line parameter when using picocli

420 views Asked by At

In our project we are using Lightbend Config / TypesafeConfig.

I can run my program with java -jar. The configuration of my program can also be done by using command line parameters.

Example:

java -jar simpleclient.jar -Dservice.url="http://localhost:8123"

Now I introduced https://picocli.info/ to have a better command line handling for my application.

The problem I'm facing now ist, that picocli doesn't allow the usage of -D... parameters in the standard configuration.

How can this be changed?

1

There are 1 answers

2
Remko Popma On BEST ANSWER

When you say “picocli doesn’t allow the use of -D... options”, I assume you mean you want to allow end users to set system properties with the -Dkey=value syntax. When such parameters are passed to the application, the application needs to use these values to set system properties, as shown below.

First, users can set system properties by passing -Dkey=value parameters to the java process instead of to the main class in the jar. In the below invocation, the system properties are set directly and are not passed as parameters to the application:

java -Dservice.url="http://localhost:8123"  -jar simpleclient.jar

Secondly, you can define a -D option in your application that sets system properties:

@Command
class SimpleClient {

    @Option(names = "-D")
    void setProperty(Map<String, String> props) {
        props.forEach((k, v) -> System.setProperty(k, v == null ? "" : v));
    }
}