-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make JVM version configurable per-module (#3716)
Fixes #3480. Changes: - adds `coursier-jvm` as a dependency to the `util` module, this library is for fetching JVMs using coursier and the coursier [jvm-index](https://github.com/coursier/jvm-index) - add a `def javaHome: Task[Option[PathRef]]` task to `ZincWorkerModule`. This defaults to `None`, which is the existing behavior of using mill's java home. - Users who want to use a custom JVM need to define a custom `ZincWorkerModule` and override `def jvmId`, optionally `def jvmIndexVersion` - updates the `mockito` and `commons-io` examples to use the new configuration options. Now these examples should run even when mill itself is not running on java 11. - This also required adding `-encoding utf-8` to the projects' javac options. - update the `run` and `test` tasks to use the `zincWorker`'s java home if a different one specified Notes: * `JavaModule#compile` forks a new JVM for each compilation to support custom JVM versions, and `ScalaModule#compile` does not support custom JVM versions at all. This would require a deeper refactoring of `ZincWorkerModule` to fix * --------- Co-authored-by: Li Haoyi <[email protected]>
- Loading branch information
1 parent
1a04585
commit a458d47
Showing
34 changed files
with
930 additions
and
159 deletions.
There are no files selected for viewing
This file contains 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 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 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
8 changes: 8 additions & 0 deletions
8
docs/modules/ROOT/pages/fundamentals/configuring-jvm-versions.adoc
This file contains 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,8 @@ | ||
= Configuring JVM Versions | ||
|
||
By default, Mill uses the same JVM that it itself is running on to compile/test/run | ||
Java/Scala/Kotlin modules. This page goes into more detail about downloading | ||
and using a custom Java home on a per-module basis. | ||
|
||
include::partial$example/depth/javahome/1-custom-jvms.adoc[] | ||
|
This file contains 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 @@ | ||
package bar | ||
|
||
object Bar { | ||
def main(args: Array[String]): Unit = { | ||
println(s"Bar running on Java ${sys.props("java.version")}") | ||
} | ||
} |
This file contains 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,125 @@ | ||
// :link-jvm-indices: https://github.com/coursier/jvm-index/blob/master/indices/linux-arm64.json | ||
// To override the default Java home, you can create a custom | ||
// `ZincWorkerModule` and override the `zincWorker` method in your | ||
// `JavaModule` or `ScalaModule` by pointing it to that custom object: | ||
|
||
// == Downloading Jvm By Version | ||
// | ||
// The `ZincWorkerModule.ForJvm` class takes a Jvm dependency string as its | ||
// contructor argument and configures the module to fetch and cache the Jvm | ||
// using coursier and use it for the compile, run, and test tasks. | ||
// | ||
// The string has the form: | ||
// | ||
// ---- | ||
// "{name}:{version}" | ||
// ---- | ||
// | ||
// To see what Jvms are available for download look at the index for your os | ||
// and architecture {link-jvm-indices}[here]. | ||
import mill._, javalib._ | ||
import mill.define.ModuleRef | ||
|
||
object ZincWorkerJava11 extends ZincWorkerModule { | ||
def jvmId = "temurin:18.0.2" | ||
} | ||
|
||
object foo extends JavaModule { | ||
def zincWorker = ModuleRef(ZincWorkerJava11) | ||
|
||
object test extends JavaTests with TestModule.Junit4 | ||
} | ||
|
||
/** Usage | ||
|
||
> mill foo.run | ||
Foo running on Java 18.0.2 | ||
|
||
> mill foo.test | ||
Testing with JVM version: 18.0.2 | ||
Test foo.FooTest.testSimple finished... | ||
|
||
*/ | ||
|
||
// Selecting a custom JVM via `ZincWorkerModule.ForJvm` means that JVM is used for | ||
// compiling, testing, and running that module via Mill. Note that `.assembly` is not | ||
// affected, as JVM assembly jars do not bundle a JVM and have to be run using a | ||
// JVM installed on the target host machine. Configuration is done via `ZincWorkerModule` | ||
// as the https://github.com/sbt/zinc[Zinc] incremental compiler is used for compiling | ||
// Java and Scala sources. | ||
// | ||
// == Selecting JVM Index Versions | ||
// | ||
|
||
// | ||
// By default, Mill comes bundled with a version of the JVM index that was published when | ||
// each version of Mill is released. This ensures that the JVM versions you pick are stable, | ||
// but means that the latest JVM versions may not be available. You can pass in the JVM | ||
// index version explicitly via `def jvmIndexVersion` below, choosing a published | ||
// index version from the Maven Central (https://repo1.maven.org/maven2/io/get-coursier/jvm/indices/index-linux-arm64/[link]) | ||
// | ||
|
||
import scalalib._ | ||
|
||
object ZincWorkerJava11Latest extends ZincWorkerModule { | ||
def jvmId = "temurin:23.0.1" | ||
def jvmIndexVersion = "latest.release" | ||
} | ||
|
||
object bar extends ScalaModule { | ||
def scalaVersion = "2.13.12" | ||
def zincWorker = ModuleRef(ZincWorkerJava11Latest) | ||
|
||
} | ||
|
||
/** Usage | ||
|
||
> mill bar.run | ||
Bar running on Java 23.0.1 | ||
|
||
*/ | ||
|
||
// == Explicit JVM Download URLs | ||
// | ||
// You can also pass in the JVM download URL explicitly. Note that if you do so, you need | ||
// to ensure yourself that you are downloading the appropriate JVM distribution for your | ||
// operating system and CPU architecture. In the example below we switch between Mac/ARM and | ||
// Linux/X64, but you may have additional cases if you need to support Windows or other | ||
// OS/CPU combinations | ||
|
||
import kotlinlib._ | ||
|
||
object ZincWorkerJava11Url extends ZincWorkerModule { | ||
def jvmId = | ||
if (sys.props("os.name") == "Mac OS X") { | ||
"https://github.com/adoptium/temurin22-binaries/releases/download/jdk-22.0.2%2B9/OpenJDK22U-jdk_aarch64_mac_hotspot_22.0.2_9.tar.gz" | ||
} else { | ||
"https://github.com/adoptium/temurin22-binaries/releases/download/jdk-22.0.2%2B9/OpenJDK22U-jdk_x64_linux_hotspot_22.0.2_9.tar.gz" | ||
} | ||
|
||
} | ||
|
||
object qux extends KotlinModule { | ||
def kotlinVersion = "2.0.20" | ||
def zincWorker = ModuleRef(ZincWorkerJava11Url) | ||
} | ||
|
||
/** Usage | ||
|
||
> mill qux.run | ||
Qux running on Java 22.0.2 | ||
|
||
*/ | ||
|
||
// == Locally-Installed JVMs | ||
// | ||
// Lastly, you can point Mill at any JVM distribution installed locally on disk via: | ||
|
||
object ZincWorkerLocalJvm extends ZincWorkerModule { | ||
def javaHome = Some(PathRef(os.Path("/my/java/home"), quick = true)) | ||
} | ||
|
||
object baz extends JavaModule { | ||
def zincWorker = ModuleRef(ZincWorkerLocalJvm) | ||
|
||
} |
This file contains 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,8 @@ | ||
package foo; | ||
|
||
public class Foo { | ||
|
||
public static void main(String[] args) throws Exception { | ||
System.out.println("Foo running on Java " + System.getProperty("java.version")); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
example/depth/javahome/1-custom-jvms/foo/test/src/foo/FooTest.java
This file contains 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,13 @@ | ||
package foo; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class FooTest { | ||
@Test | ||
public void testSimple() { | ||
System.out.println("Testing with JVM version: " + System.getProperty("java.version")); | ||
assertEquals(System.getProperty("java.version"), "18.0.2"); | ||
} | ||
} |
This file contains 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,8 @@ | ||
package qux; | ||
|
||
public class Qux { | ||
|
||
public static void main(String[] args) throws Exception { | ||
System.out.println("Qux running on Java " + System.getProperty("java.version")); | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.