JavaFx dependency in Sbt and Mill

I don’t know if this applies to other libraries, but JavaFx went from being part of the JVM in Java 8 to an external platform specific library in Java 11+. At that point my build stopped working in Mill and I have just been using Sbt for running JavaFx.

However I’m now wondering if the Linux, Windows and Mac versions should be encoded as separate sub projects in Sbt. Presumably as the Jar artefacts need to be released separately for each platform. How would I convert the line below to platform specific?

libraryDependencies += "org.openjfx" % "javafx-controls" % "15"

Here is an SBT sample that detects current OS and generates sequence of needed JavaFX libraries

  libraryDependencies ++= {
    // Determine OS version of JavaFX binaries
    val osName = System.getProperty("os.name") match {
      case n if n.startsWith("Linux")   => "linux"
      case n if n.startsWith("Mac")     => "mac"
      case n if n.startsWith("Windows") => "win"
      case _                            => throw new Exception("Unknown platform!")
    }
    Seq("base", "controls", "fxml", "graphics", "media", "swing", "web")
      .map(m => "org.openjfx" % s"javafx-$m" % "15.0.1" classifier osName)
  }

In Mill it will be similar:

  // Determine OS version of JavaFX binaries
  lazy val osName = System.getProperty("os.name") match {
    case n if n.startsWith("Linux")   => "linux"
    case n if n.startsWith("Mac")     => "mac"
    case n if n.startsWith("Windows") => "win"
    case _                            => throw new Exception("Unknown platform!")
  }

  // Add dependency on JavaFX libraries, OS dependent
  val javaFXModules = List("base", "controls", "fxml", "graphics", "media", "swing", "web")
    .map(m => ivy"org.openjfx:javafx-$m:15.0.1;classifier=$osName")
2 Likes

Thanks I’ll experiment with that.

Mill is currently not working. Only seems to work prior 0.5.3. Can you tell me what version you used? TIA

2 Likes

Sorry, I use SBT (that definitely works). Mill suggestion was sent to ScalaFX ReadMe, may need to be corrected.

2 Likes

So with the help of Andrew Richards on the Mill Gitter channel, I’ve managed to get it working with:

def unmanagedClasspath = T{
    import coursier._, parse.DependencyParser
    val fxMod = Dependency(Module(org"org.openjfx", ModuleName("javafx-controls")), "15.0.1")
    val files = Fetch().addDependencies(fxMod).run()
    val pathRefs = files.map(f => PathRef(os.Path(f)))
    Agg(pathRefs : _*)
  }

def forkArgs = Seq(
   "--module-path", sys.env("JAVAFX_HOME") + ":" +
     "/Users/ajr/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/openjfx/javafx-controls/15.0.1",

    "--add-modules", "javafx.controls"
  )
2 Likes

@RichType

Can you tell me what your JAVAFX_HOME looks like? More concretely is is a single or multiple paths?

TIA

Take look at a new sample Mill project here GitHub - rom1dep/scalafx-millproject: minimal mill project for scalafx with scala3 it requires com-lihaoyi/mill/pull/775. It is described here scalafx/issues/343

1 Like