Expand my Community achievements bar.

best way to implement env vars or similar?

Avatar

Level 9

In the core module, rather than hard code things which might change, or are env specific, we want to pull them in.

E.g rather than 

final private String rootUrl = "https://somesite.com/api" 

In the classes,

We want to use something like.

String rootURL = getEnv("rootURL");

In a typical java app, getEnv would search for var set on java command line, set in a config file, set globally in an env var etc.

Ideally, it would be possible to edit these at runtime for a given env, in a UI, so as to not require a release cycle to edit.  Is there something built in?

 

=== update 1 ===

Thanks for the answers below.  It looks like the only way to do this is via osgi, which I was hoping to avoid due to its complexity and steep learning curve.  It looks like its primarily to implement pluggable services, rather than simple env vars.  I am currently looking for simpler options due to time limitations.  I assume we can extract the run mode in code, so I could put the config a class with some if statements.

 

=== update 2 ===

We have still not found a way to read an env var (configuration variable), as there appear to be no tutorials other than the one which requires hundreds of lines of code to create a custom OSGi service: https://aem.redquark.org/2018/10/day-12-creating-your-custom-osgi.html

There appear to be other methods, we just cant find a tutorial or guide (other than reference pages which only list out part of the picture, and don't contain enough information to implement an env var end to end in code (i.e. not using the various UIs which only affect the local dev instance).

 

15 Replies