Skip to content

Commit 0932c20

Browse files
committed
Add support for multi-release Jar files
If the user configures javacOptions to include Seq("--release", "17"), for example, then multi-release Jar files will be opened and processed accordingly.
1 parent c75e29c commit 0932c20

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

scalalib/src/mill/scalalib/Assembly.scala

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ object Assembly {
124124

125125
def loadShadedClasspath(
126126
inputPaths: Agg[os.Path],
127-
assemblyRules: Seq[Assembly.Rule]
127+
assemblyRules: Seq[Assembly.Rule],
128+
runtimeVersion: Option[Runtime.Version]
128129
): (Generator[(String, UnopenedInputStream)], ResourceCloser) = {
129130
val shadeRules = assemblyRules.collect {
130131
case Rule.Relocate(from, to) => ShadePattern.Rename(List(from -> to)).inAll
@@ -148,13 +149,20 @@ object Assembly {
148149
}
149150

150151
val pathsWithResources = inputPaths.filter(os.exists).map { path =>
151-
if (os.isFile(path)) path -> Some(new JarFile(path.toIO))
152+
if (os.isFile(path)) path -> {
153+
val file = path.toIO
154+
val jarFile = runtimeVersion match {
155+
case Some(version) => new JarFile(file, true, java.util.zip.ZipFile.OPEN_READ, version)
156+
case None => new JarFile(file)
157+
}
158+
Some(jarFile)
159+
}
152160
else path -> None
153161
}
154162

155163
val generators = Generator.from(pathsWithResources).flatMap {
156164
case (path, Some(jarFile)) =>
157-
Generator.from(jarFile.entries().asScala.filterNot(_.isDirectory))
165+
Generator.from(jarFile.versionedStream().iterator().asScala.filterNot(_.isDirectory))
158166
.flatMap(entry => shader(entry.getName, () => jarFile.getInputStream(entry)))
159167
case (path, None) =>
160168
os.walk
@@ -193,7 +201,8 @@ object Assembly {
193201
manifest: mill.api.JarManifest = mill.api.JarManifest.MillDefault,
194202
prependShellScript: String = "",
195203
base: Option[os.Path] = None,
196-
assemblyRules: Seq[Assembly.Rule] = Assembly.defaultRules
204+
assemblyRules: Seq[Assembly.Rule] = Assembly.defaultRules,
205+
runtimeVersion: Option[Runtime.Version]
197206
)(implicit ctx: Ctx.Dest with Ctx.Log): PathRef = {
198207
val tmp = ctx.dest / "out-tmp.jar"
199208

@@ -213,7 +222,8 @@ object Assembly {
213222
manifest.build.write(manifestOut)
214223
manifestOut.close()
215224

216-
val (mappings, resourceCleaner) = Assembly.loadShadedClasspath(inputPaths, assemblyRules)
225+
val (mappings, resourceCleaner) =
226+
Assembly.loadShadedClasspath(inputPaths, assemblyRules, runtimeVersion)
217227
try {
218228
Assembly.groupAssemblyEntries(mappings, assemblyRules).foreach {
219229
case (mapping, entry) =>

scalalib/src/mill/scalalib/JavaModule.scala

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ trait JavaModule
122122
*/
123123
def javacOptions: T[Seq[String]] = T { Seq.empty[String] }
124124

125+
private def extractRuntimeVersion(javacOptions: Seq[String]): Option[Runtime.Version] = {
126+
val releaseOptIndex = javacOptions.indexOf("--release")
127+
val releaseOptValueIndex = releaseOptIndex + 1
128+
if (releaseOptIndex >= 0 && javacOptions.length >= releaseOptValueIndex) {
129+
val runtimeVersionString = javacOptions(releaseOptValueIndex)
130+
val runtimeVersion = Runtime.Version.parse(runtimeVersionString)
131+
println(s"Extracted javac --release version $runtimeVersion")
132+
Some(runtimeVersion)
133+
} else None
134+
}
135+
125136
/** The direct dependencies of this module */
126137
def moduleDeps: Seq[JavaModule] = Seq.empty
127138

@@ -447,7 +458,8 @@ trait JavaModule
447458
Assembly.createAssembly(
448459
upstreamAssemblyClasspath().map(_.path),
449460
manifest(),
450-
assemblyRules = assemblyRules
461+
assemblyRules = assemblyRules,
462+
runtimeVersion = extractRuntimeVersion(javacOptions())
451463
)
452464
}
453465

@@ -461,7 +473,8 @@ trait JavaModule
461473
manifest(),
462474
prependShellScript(),
463475
Some(upstreamAssembly().path),
464-
assemblyRules
476+
assemblyRules,
477+
runtimeVersion = extractRuntimeVersion(javacOptions())
465478
)
466479
}
467480

0 commit comments

Comments
 (0)