-
Notifications
You must be signed in to change notification settings - Fork 78
Add support for Java version-specific JVM options #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
882d39e
feat: Add Java version-specific JVM options support
xerial 78e35af
fix: Address PR review comments for POSIX compliance and portability
xerial c623cc6
docs: Add packJvmVersionSpecificOpts example to README
xerial 68f649b
docs: Fix version number for packJvmVersionSpecificOpts (0.22 not 0.21)
xerial 2394798
fix: Apply code formatting and fix scripted test
xerial 9defb97
test: Add validation for Java version-specific JVM options
xerial 4ed7df1
feat: Add JDK 24 --sun-misc-unsafe-memory-access=allow option to test
xerial 38956b8
fix: Update Java 21 JVM option from GenerationalZGC to ZGenerational
xerial 3108567
test: Add build.properties for min-project test
xerial dcd9cfe
docs: Add formatting reminder to CLAUDE.md
xerial 22eda9a
Revert "test: Add build.properties for min-project test"
xerial 27d3a60
feat: Implement version range support for packJvmVersionSpecificOpts
xerial 58ecef9
fix: Remove quotes from JVM options in Windows batch scripts
xerial 350aa82
test: Update Main.scala to validate range-based JVM options
xerial File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| enablePlugins(PackPlugin) | ||
| name := "jvm-version-opts-test" | ||
|
|
||
| scalaVersion := "2.13.16" | ||
| crossPaths := false | ||
|
|
||
| packMain := Map("test" -> "Main") | ||
| packJvmOpts := Map("test" -> Seq("-Xms256m", "-Xmx512m")) | ||
|
|
||
| // Configure different JVM options for different Java versions | ||
| // These are applied as ranges: [8,11), [11,17), [17,21), [21,24), [24,∞) | ||
| packJvmVersionSpecificOpts := Map( | ||
| "test" -> Map( | ||
| 8 -> Seq("-XX:MaxPermSize=256m"), | ||
| 11 -> Seq("-XX:+UnlockExperimentalVMOptions", "-XX:+UseJVMCICompiler"), | ||
| 17 -> Seq("-XX:+UseZGC"), | ||
| 21 -> Seq("-XX:+UseZGC", "-XX:+ZGenerational"), | ||
xerial marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 24 -> Seq("-XX:+UseG1GC", "-XX:G1HeapRegionSize=32m", "--sun-misc-unsafe-memory-access=allow") | ||
| ) | ||
| ) | ||
1 change: 1 addition & 0 deletions
1
src/sbt-test/sbt-pack/jvm-version-opts/project/build.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| sbt.version=1.10.11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| val pluginVersion = System.getProperty("plugin.version") | ||
| if (pluginVersion == null) | ||
| throw new RuntimeException("""|The system property 'plugin.version' is not defined. | ||
| |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) | ||
| else addSbtPlugin("org.xerial.sbt" % "sbt-pack" % pluginVersion) | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/sbt-test/sbt-pack/jvm-version-opts/src/main/scala/Main.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| object Main { | ||
| def main(args: Array[String]): Unit = { | ||
| // Print Java version and JVM options for testing | ||
| val javaVersion = System.getProperty("java.version") | ||
| println(s"Java version: $javaVersion") | ||
|
|
||
| // Extract major version number | ||
| val majorVersion = if (javaVersion.startsWith("1.")) { | ||
| javaVersion.split("\\.")(1).toInt | ||
| } else { | ||
| javaVersion.split("\\.")(0).split("-")(0).toInt | ||
| } | ||
| println(s"Major version: $majorVersion") | ||
|
|
||
| val runtimeMxBean = java.lang.management.ManagementFactory.getRuntimeMXBean | ||
| val jvmArgs = runtimeMxBean.getInputArguments | ||
|
|
||
| println("JVM Arguments:") | ||
| import scala.jdk.CollectionConverters._ | ||
| val argsList = jvmArgs.asScala.toList | ||
| argsList.foreach { arg => | ||
| println(s" $arg") | ||
| } | ||
|
|
||
| // Validate that we have the base JVM options | ||
| val hasXms = argsList.exists(_.contains("-Xms256m")) | ||
| val hasXmx = argsList.exists(_.contains("-Xmx512m")) | ||
|
|
||
| println("\nValidation:") | ||
| println(s" Has -Xms256m: $hasXms") | ||
| println(s" Has -Xmx512m: $hasXmx") | ||
|
|
||
| // Validate version-specific options based on current Java version | ||
| // Using range-based logic: options are applied from the highest version <= current version | ||
| val expectedVersionOptions = majorVersion match { | ||
| case _ if majorVersion >= 8 && majorVersion < 11 => | ||
| // Java [8,11) should have MaxPermSize | ||
| val hasMaxPermSize = argsList.exists(_.contains("MaxPermSize")) | ||
| println(s" Has MaxPermSize (Java [8,11)): $hasMaxPermSize") | ||
| hasMaxPermSize | ||
| case _ if majorVersion >= 11 && majorVersion < 17 => | ||
| // Java [11,17) should have UseJVMCICompiler | ||
| val hasJVMCI = argsList.exists(_.contains("UseJVMCICompiler")) | ||
| println(s" Has UseJVMCICompiler (Java [11,17)): $hasJVMCI") | ||
| hasJVMCI | ||
| case _ if majorVersion >= 17 && majorVersion < 21 => | ||
| // Java [17,21) should have UseZGC | ||
| val hasZGC = argsList.exists(_.contains("UseZGC")) | ||
| println(s" Has UseZGC (Java [17,21)): $hasZGC") | ||
| hasZGC | ||
| case _ if majorVersion >= 21 && majorVersion < 24 => | ||
| // Java [21,24) should have UseZGC and ZGenerational | ||
| val hasZGC = argsList.exists(_.contains("UseZGC")) | ||
| val hasGenZGC = argsList.exists(_.contains("ZGenerational")) | ||
xerial marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| println(s" Has UseZGC (Java [21,24)): $hasZGC") | ||
| println(s" Has ZGenerational (Java [21,24)): $hasGenZGC") | ||
| hasZGC && hasGenZGC | ||
| case _ if majorVersion >= 24 => | ||
| // Java [24,∞) should have UseG1GC, G1HeapRegionSize, and sun-misc-unsafe-memory-access | ||
| val hasG1GC = argsList.exists(_.contains("UseG1GC")) | ||
| val hasG1HeapRegion = argsList.exists(_.contains("G1HeapRegionSize")) | ||
| val hasUnsafeMemoryAccess = argsList.exists(_.contains("sun-misc-unsafe-memory-access=allow")) | ||
| println(s" Has UseG1GC (Java [24,∞)): $hasG1GC") | ||
| println(s" Has G1HeapRegionSize (Java [24,∞)): $hasG1HeapRegion") | ||
| println(s" Has --sun-misc-unsafe-memory-access=allow (Java [24,∞)): $hasUnsafeMemoryAccess") | ||
| hasG1GC && hasG1HeapRegion && hasUnsafeMemoryAccess | ||
| case _ => | ||
| // For versions before 8 or when no options are defined | ||
| println(s" No version-specific options for Java $majorVersion") | ||
| true | ||
| } | ||
|
|
||
| // Exit with error if validation fails | ||
| if (!hasXms || !hasXmx) { | ||
| println("\nERROR: Base JVM options not found!") | ||
| System.exit(1) | ||
| } | ||
|
|
||
| if (!expectedVersionOptions) { | ||
| println(s"\nERROR: Expected version-specific JVM options for Java $majorVersion not found!") | ||
| System.exit(1) | ||
| } | ||
|
|
||
| println("\nAll validations passed!") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| > 'set version := "0.1"' | ||
| > pack | ||
| $ exists target/pack/bin/test | ||
| $ exists target/pack/bin/test.bat | ||
| # Run the test program to verify it works (this also validates that version detection doesn't break the script) | ||
| $ exec sh ./target/pack/bin/test |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for version-specific JVM options uses an exact version match. This can be inconvenient if you want to set options for a Java version and all subsequent versions (e.g., Java 11+), as implied by the
24+comment in the README. A more flexible approach would be to apply the options for the highest configured version that is less than or equal to the detected Java version.To implement this, I suggest changing
JVM_VERSION_OPTSfrom aMap[Int, String]to a sortedSeq[(Int, String)]. This will allow the launch scripts to iterate through the versions and find the best match. I'll provide related suggestions in other files to complete this change.