Skip to content
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

[WIP] Experiment with jpackage to create executable binaries #3201

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion example/scalamodule/10-assembly-config/build.sc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mill._, scalalib._
import mill.scalalib.Assembly._

object foo extends ScalaModule {
object foo extends ScalaModule with JpackageModule {
def moduleDeps = Seq(bar)
def scalaVersion = "2.13.8"
def ivyDeps = Agg(ivy"com.lihaoyi::os-lib:0.9.1")
Expand Down Expand Up @@ -43,4 +43,10 @@ Loaded application.conf from resources:...
...Foo Application Conf
...Bar Application Conf

> mill show foo.jpackageAppImage
".../out/foo/jpackageAppImage.dest/image"

> ./out/foo/jpackageAppImage.dest/image/foo/bin/foo
Loaded application.conf from resources: Foo Application Conf

*/
9 changes: 8 additions & 1 deletion scalalib/src/mill/scalalib/JavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,19 @@ trait JavaModule
}

/**
* The transitive version of `localClasspath`
* The transitive version of [[localClasspath]].
*/
def transitiveLocalClasspath: T[Agg[PathRef]] = T {
T.traverse(transitiveModuleCompileModuleDeps)(_.localClasspath)().flatten
}

/**
* Almost the same as [[transitiveLocalClasspath]], but using the [[jar]]s instead of [[localClasspath]].
*/
def transitiveJars: T[Seq[PathRef]] = T {
T.traverse(transitiveModuleCompileModuleDeps)(_.jar)()
}

/**
* Same as [[transitiveLocalClasspath]], but with all dependencies on [[compile]]
* replaced by their non-compiling [[bspCompileClassesPath]] variants.
Expand Down
71 changes: 71 additions & 0 deletions scalalib/src/mill/scalalib/JpackageModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package mill.scalalib

import mill._

/**
* Support building native binaries with ` jpackage` tools, which chomes bundles with Java SDK since version 16.
*/
trait JpackageModule extends JavaModule {

/** The application name */
def jpackageName: T[String] = T { artifactName() }

/** The main class to use as the native binary entry point. */
def jpackageMainClass: T[String] = T { finalMainClass() }

/**
* The classpath used for the `jpackage`` tool. The first entry needs to be the main jar.
* In difference to [[runClasspath]], it contains the built jars of all dependent modules.
*/
def jpackageRunClasspath: T[Seq[PathRef]] = T {
val recLocalClasspath = (localClasspath() ++ transitiveLocalClasspath()).map(_.path)

val runCp = runClasspath().filterNot(pr => recLocalClasspath.contains(pr.path))

val mainJar = jar()
val recJars = transitiveJars()

mainJar +: (recJars ++ runCp)
}

/** Builds a native binary image of the main application. */
def jpackageAppImage: T[PathRef] = T {
// materialize all jars into a "lib" dir
val libs = T.dest / "lib"
val cp = jpackageRunClasspath().map(_.path)
var counter = 1
val jars = cp.filter(os.exists).map { p =>
val dest = libs / s"${counter}-${p.last}"
os.copy(p, dest, createFolders = true)
counter += 1
dest
}

val appName = jpackageName()
val mainClass = jpackageMainClass()
val mainJarName = jars.head.last

// TODO: runtime java options, e.g. env and stuff

val args: Seq[String] = Seq(
"jpackage",
"--type",
"app-image",
"--name",
appName,
"--input",
libs.toString(),
"--main-jar",
mainJarName,
"--main-class",
mainClass
)

// run jpackage tool
val outDest = T.dest / "image"
os.makeDir.all(outDest)
os.proc(args).call(cwd = outDest)
PathRef(outDest)
}

}