Skip to content

Instantly share code, notes, and snippets.

@yaauie
Last active May 15, 2024 03:34
Show Gist options
  • Save yaauie/02a6e4c8fea61a43fef5160e3a980d13 to your computer and use it in GitHub Desktop.
Save yaauie/02a6e4c8fea61a43fef5160e3a980d13 to your computer and use it in GitHub Desktop.

Use a single jvm options file with multiple versions of Java. Simply prefix a line with a specification of which versions it should be included with.

  • X:: emitted when target version is exactly X
  • X-:: emitted when target version is at least X
  • -Y:: emitted when target version is not greater than Y
  • X-Y: emitted when target version is at least X and not greater than Y
    8:option that only works with java 8
  -11:option that is deprecated after java 11
 9-  :option that requires at least java 9
11-17:option for use in java 11 through 17
     :option that is always included
option without prefix that is always included

-- jvm.options

╭─{ rye@perhaps:~/src/REDACTED }
╰─● ./filter_jvm_ops 8 < jvm.options
option that only works with java 8
option that is deprecated after java 11
option that is always included
option without prefix that is always included
[success]

╭─{ rye@perhaps:~/src/REDACTED }
╰─● ./filter_jvm_ops 9 < jvm.options
option that is deprecated after java 11
option that requires at least java 9
option that is always included
option without prefix that is always included
[success]

╭─{ rye@perhaps:~/src/REDACTED }
╰─● ./filter_jvm_ops 10 < jvm.options
option that is deprecated after java 11
option that requires at least java 9
option that is always included
option without prefix that is always included
[success]

╭─{ rye@perhaps:~/src/REDACTED }
╰─● ./filter_jvm_ops 11 < jvm.options
option that is deprecated after java 11
option that requires at least java 9
option for use in java 11 through 17
option that is always included
option without prefix that is always included
[success]

╭─{ rye@perhaps:~/src/REDACTED }
╰─● ./filter_jvm_ops 12 < jvm.options
option that requires at least java 9
option for use in java 11 through 17
option that is always included
option without prefix that is always included
[success]

╭─{ rye@perhaps:~/src/REDACTED }
╰─● ./filter_jvm_ops 17 < jvm.options
option that requires at least java 9
option for use in java 11 through 17
option that is always included
option without prefix that is always included
[success]

╭─{ rye@perhaps:~/src/REDACTED }
╰─● ./filter_jvm_ops 18 < jvm.options
option that requires at least java 9
option that is always included
option without prefix that is always included
[success]
#!/usr/bin/env bash
set -e
target_version="${1:?target version required!}"
while IFS= read -r line; do
if [[ "${line}" =~ ^[[:blank:]]*([[:digit:]]+)?[[:blank:]]*(-)?[[:blank:]]*([[:digit:]]+)?[[:blank:]]*:(.*) ]]; then
lo_bound="${BASH_REMATCH[1]}"
operator="${BASH_REMATCH[2]}"
up_bound="${BASH_REMATCH[3]}"
content="${BASH_REMATCH[4]}"
if [[ -z "${operator}" ]]; then
: "${lo_bound:=${up_bound}}"
: "${up_bound:=${lo_bound}}"
fi
if (( "${target_version}" >= "${lo_bound:-0}" && "${target_version}" <= "${up_bound:-9999}" )); then
[[ -z "${DEBUG}" ]] || >&2 echo "INCLUDE[${target_version} in (${lo_bound}${operator:+-}${up_bound})]: ${line}"
echo "${content}"
else
[[ -z "${DEBUG}" ]] || >&2 echo "EXCLUDE[${target_version} not in (${lo_bound}${operator:+-}${up_bound})]: ${line}"
fi
else
[[ -z "${DEBUG}" ]] || >&2 echo "INCLUDE[no-bound]: ${line}"
echo "${line}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment