Skip to content

Commit daa2719

Browse files
authored
Add mill-jvm-index-version to build header keys (#6085)
Fixes #6072
1 parent 17d4e5d commit daa2719

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//| mill-jvm-index-version: 0.0.4-88-f9ba3d
2+
//| mill-jvm-version: temurin:11
3+
//
4+
// Mill uses the https://github.com/coursier/jvm-index[Coursier JVM Index] to resolve JVM
5+
// versions to their download location, as well as partially-qualified versions such as
6+
// `temurin:11` to the specific point version such as `temurin:11.0.28`. The version of
7+
// the JVM index is set by the version of Mill you are using, but you can override it
8+
// to use a newer JVM index (e.g. if you want to use a newly-released JVM not yet included
9+
// in Mill's jvm index version) or an older JVM index.
10+
//
11+
// The list of JVM index versions can be seen here:
12+
//
13+
// - https://repo1.maven.org/maven2/io/get-coursier/jvm/indices/[JVM Indices on Maven Central]
14+
//
15+
package build
16+
17+
import mill._
18+
19+
def printJavaVersion() = Task.Command {
20+
println("Java version: " + System.getProperty("java.version"))
21+
}
22+
23+
// This example uses an older `mill-jvm-index-version` to ensure the resolved version
24+
// of `temurin:11` gets appropriately downgraded from `11.0.28` (the most recent `11.x` version)
25+
// to `11.0.26` (the version of `11.x` available when the JVM index `0.0.4-88-f9ba3d` was released)
26+
/** Usage
27+
28+
> ./mill printJavaVersion
29+
Java version: 11.0.26
30+
31+
*/

runner/launcher/src/mill/launcher/CoursierClient.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ object CoursierClient {
4747
artifactsResultOrError.artifacts.map(_._2.toString).toArray
4848
}
4949

50-
def resolveJavaHome(id: String): java.io.File = {
50+
def resolveJavaHome(id: String, jvmIndexVersion: String = null): java.io.File = {
5151
val coursierCache0 = FileCache[Task]()
5252
.withLogger(coursier.cache.loggers.RefreshLogger.create())
53+
54+
val indexVersion =
55+
if (jvmIndexVersion != null) jvmIndexVersion else mill.client.Versions.coursierJvmIndexVersion
5356
val jvmCache = JvmCache()
5457
.withArchiveCache(ArchiveCache().withCache(coursierCache0))
5558
.withIndex(
5659
JvmIndex.load(
5760
cache = coursierCache0,
5861
repositories = Resolve.defaultRepositories,
59-
indexChannel = JvmChannel.module(
60-
JvmChannel.centralModule(),
61-
version = mill.client.Versions.coursierJvmIndexVersion
62-
)
62+
indexChannel = JvmChannel.module(JvmChannel.centralModule(), version = indexVersion)
6363
)
6464
)
6565

runner/launcher/src/mill/launcher/MillProcessLauncher.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ static String millJvmVersion(OutFolderMode outMode) throws Exception {
166166
else return res.get(0);
167167
}
168168

169+
static String millJvmIndexVersion(OutFolderMode outMode) throws Exception {
170+
List<String> res = loadMillConfig(outMode, "mill-jvm-index-version");
171+
if (res.isEmpty()) return null;
172+
else return res.get(0);
173+
}
174+
169175
static String millServerTimeout() {
170176
return System.getenv(EnvVars.MILL_SERVER_TIMEOUT_MILLIS);
171177
}
@@ -176,6 +182,7 @@ static boolean isWin() {
176182

177183
static String javaHome(OutFolderMode outMode) throws Exception {
178184
var jvmId = millJvmVersion(outMode);
185+
var jvmIndexVersion = millJvmIndexVersion(outMode);
179186

180187
String javaHome = null;
181188
if (jvmId == null) {
@@ -184,11 +191,16 @@ static String javaHome(OutFolderMode outMode) throws Exception {
184191

185192
if (jvmId != null) {
186193
final String jvmIdFinal = jvmId;
194+
final String jvmIndexVersionFinal = jvmIndexVersion;
195+
// Include JVM index version in the cache key to invalidate cache when index version changes
196+
String cacheKey = jvmIndexVersion != null ? jvmId + ":" + jvmIndexVersion : jvmId;
187197
javaHome = cachedComputedValue0(
188198
outMode,
189199
"java-home",
190-
jvmId,
191-
() -> new String[] {CoursierClient.resolveJavaHome(jvmIdFinal).getAbsolutePath()},
200+
cacheKey,
201+
() -> new String[] {
202+
CoursierClient.resolveJavaHome(jvmIdFinal, jvmIndexVersionFinal).getAbsolutePath()
203+
},
192204
// Make sure we check to see if the saved java home exists before using
193205
// it, since it may have been since uninstalled, or the `out/` folder
194206
// may have been transferred to a different machine

website/docs/modules/ROOT/pages/cli/build-header.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ crashes and errors.
129129
For configuring different JVM versions for individual modules within your build, see
130130
xref:fundamentals/configuring-jvm-versions.adoc[Configuring JVM Versions].
131131

132+
== mill-jvm-index-version
133+
134+
include::partial$example/cli/header/4-jvm-index-version.adoc[]
135+
132136
== mill-jvm-opts
133137

134138
Mill supports `mill-jvm-opts` to set JVM-level flags

0 commit comments

Comments
 (0)