Example

Envopts

Demonstrates how helix-envopts parses ENV_OPTS and custom environment-variable names into JVM system properties. Shows simple flags, -Dkey=value pairs, quoted comma-delimited values, custom env-var names, and why the pattern is handy in Docker containers.
Group
Library
Path
examples/library/envopts

Overview

Helix Java application that shows how helix-envopts turns environment variables into JVM system properties for local runs and containerized deployments.

Run this example from the shared examples build:

Run

Run this example from the shared examples build


./gradlew -p examples runExample -Pexample=library/envopts --init-script "$(pwd)/helix.init.gradle"

What It Shows

  • parsing the default ENV_OPTS environment variable with EnvOpts.parse()
  • parsing a custom environment variable with EnvOpts.parse("APP_OPTS")
  • populating simple flags, standard -Dkey=value properties, and quoted values containing commas
  • layering multiple envopts sources so a custom variable can override values from ENV_OPTS
  • why this pattern is useful in Docker containers, where one environment variable can inject many JVM properties without rewriting the image entrypoint

Example Output

EnvOpts example report
spring.profiles.active=local
demo.message=overridden by app opts
demo.regions=us-east-1,us-west-2
demo.container.role=api
demo.flag.present=true

Development

Building the Application

Run the following command to build the application:

Command

Building the Application


./gradlew clean build

Testing the Application

Run the following command to run the application tests:

Command

Testing the Application


./gradlew test
./gradlew integration

Running the Example

Populate the default ENV_OPTS variable with simple flags and -D properties:

Add a custom envopts variable and let it override one of the values from ENV_OPTS:

This is especially handy in Docker containers because the container runtime can set ENV_OPTS or APP_OPTS without changing the application command line.

Command

Running the Example


ENV_OPTS="demo.flag,-Dspring.profiles.active=local,-Ddemo.message='hello from envopts',-Ddemo.regions='us-east-1,us-west-2'" \
./gradlew run

Command

Running the Example


ENV_OPTS="demo.flag,-Dspring.profiles.active=local,-Ddemo.message='hello from envopts',-Ddemo.regions='us-east-1,us-west-2'" \
APP_OPTS="-Ddemo.message='overridden by app opts',-Ddemo.container.role=api" \
./gradlew run