All Projects β†’ scalameta β†’ Sbt Native Image

scalameta / Sbt Native Image

Plugin to generate native-image binaries with sbt

Programming Languages

scala
5932 projects

Labels

Projects that are alternatives of or similar to Sbt Native Image

Scala Play Angular Seed
πŸ€ Scala Play 2.7.x + Angular 8 with Angular CLI seed project with full-fledged build process
Stars: ✭ 85 (-33.59%)
Mutual labels:  sbt
Flyway Sbt
Flyway SBT plugin
Stars: ✭ 101 (-21.09%)
Mutual labels:  sbt
Scalafx Ensemble
scalafx ensemble
Stars: ✭ 116 (-9.37%)
Mutual labels:  sbt
Android Vertical Stepper View
A vertical stepper implementation of the material design specification
Stars: ✭ 87 (-32.03%)
Mutual labels:  sbt
Scala Debugger
Scala libraries and tooling utilizing the Java Debugger Interface.
Stars: ✭ 100 (-21.87%)
Mutual labels:  sbt
Sbt Native Packager
sbt Native Packager
Stars: ✭ 1,480 (+1056.25%)
Mutual labels:  sbt
Proscalafx
Pro JavaFX2 book source codes translated to ScalaFX
Stars: ✭ 83 (-35.16%)
Mutual labels:  sbt
Setup Scala
GitHub Action to install any version of Java (GraalVM, Java 8, Java 11, Java 14, ...) via Jabba. Works for any JVM language including Java, Scala and Kotlin.
Stars: ✭ 123 (-3.91%)
Mutual labels:  sbt
Skeleton
Simple Scala project template
Stars: ✭ 100 (-21.87%)
Mutual labels:  sbt
Sbt Jacoco
JaCoCo Code Coverage plug-in for sbt.
Stars: ✭ 115 (-10.16%)
Mutual labels:  sbt
Sbt Newrelic
New Relic monitoring integration for SBT with sbt-native-packager
Stars: ✭ 87 (-32.03%)
Mutual labels:  sbt
Sbt Jib
sbt version of sbt jib: https://github.com/GoogleContainerTools/jib
Stars: ✭ 97 (-24.22%)
Mutual labels:  sbt
Sbt S3 Resolver
☁️Amazon S3-based resolver for sbt
Stars: ✭ 112 (-12.5%)
Mutual labels:  sbt
Scala Ddd Example
🎯 λ Hexagonal Architecture + DDD + CQRS applied in Scala using Akka HTTP
Stars: ✭ 86 (-32.81%)
Mutual labels:  sbt
Stryker4s
Mutation testing for Scala. Work in progress...
Stars: ✭ 118 (-7.81%)
Mutual labels:  sbt
Sbt Dirty Money
clean Ivy2 cache
Stars: ✭ 84 (-34.37%)
Mutual labels:  sbt
Sbt Prompt
An SBT plugin for making your SBT prompt more awesome
Stars: ✭ 107 (-16.41%)
Mutual labels:  sbt
Sbt Multi Project Example
sbt multi-project example
Stars: ✭ 124 (-3.12%)
Mutual labels:  sbt
Mqperf
Stars: ✭ 122 (-4.69%)
Mutual labels:  sbt
Sbt Unidoc
sbt plugin to create a unified API document across projects
Stars: ✭ 113 (-11.72%)
Mutual labels:  sbt

Generate native-image binaries with sbt

This plugin makes it easy to generate native-image binaries with sbt. Key features:

  • automatic GraalVM native-image installation powered by Coursier, no need to start sbt with a custom $JAVA_HOME or spin up Docker. See One-click install for Scala for more details.
  • automatic support for Scala 2.12.12+ and 2.13.3+, no need to deal with issues like scala/bug#11634.
  • works with Scala 3 (Dotty)
  • get a notification when the binary is ready to use.
  • works on macOS, Windows and Linux.
  • works with Java 8 and Java 11.

Overview:

Getting started

First, add the sbt plugin to your build in project/plugins.sbt.

// project/plugins.sbt
addSbtPlugin("org.scalameta" % "sbt-native-image" % "0.2.2")

Next, enable the plugin to your native-image application in build.sbt and configure the main class.

  // build.sbt
  lazy val myNativeImageProject = project
+   .enablePlugins(NativeImagePlugin)
    .settings(
      // ...
+     Compile / mainClass := Some("com.my.MainClass")
    )

Finally, run the nativeImage task to generate the binary.

$ sbt
> myNativeImageProject/nativeImage
...
[info] Native image ready!
[info] /path/to/your/binary

Optionally, use nativeImageRun to execute the generated binary and manually test that it works as expected.

> myNativeImageProject/nativeImageRun argument1 argument 2
# output from your native-image binary

Configuration

sbt-native-image provides several settings, tasks and input tasks to customize native-image generation and to automate your native-image workflows.

nativeImage

Type: TaskKey[File]

Description: runs native-image and returns the resulting binary file.

Example usage: sbt myProject/nativeImage

nativeImageOptions

Type: TaskKey[Seq[String]]

Description: custom native-image linking options. See native-image --help for available options. Empty by default.

Example usage: nativeImageOptions ++= List("--initialize-at-build-time")

nativeImageRun

Type: InputKey[File]

Description: executes the native-image binary with given arguments. This task can only be used after nativeImage has completed.

Example usage:

  • sbt myProject/nativeImage 'myProject/nativeImageRun hello'
  • Error: sbt clean myProject/nativeImageRun. Crashes because native-image does not exist.

nativeImageCopy

Type: InputKey[File]

Description: identical to nativeImage except the resulting binary is additionally copied to the provided file. This task is helpful when configuring CI to generate the binary in a specific place.

Example usage:

  • sbt 'myProject/nativeImageCopy mybinary-x86_64-apple-darwin'.
  • sbt 'myProject/nativeImageCopy mybinary-x86_64-pc-linux'.

nativeImageVersion

Type: SettingKey[String]

Description: the GraalVM version to use.

Default: 20.2.0

Example usage: nativeImageVersion := "19.3.3"

nativeImageJvm

Type: SettingKey[String]

Description: the GraalVM JVM version to use.

Default: "graalvm-java11". Must be one of: "graalvm-java11", "graalvm" (Java 8).

Example usage: nativeImageJvm := "graalvm"

nativeImageJvmIndex

Type: SettingKey[String]

Description: the index to use for resolving the JVM version. By default, uses the Cousier JVM index.

Default: "cs". Must be one of: "cs", "jabba".

Example usage: nativeImageJvmIndex := "jabba".

nativeImageCommand

Type: TaskKey[Seq[String]]

Description: the base command that is used to launch native-image.

Default: resolves the command using nativeImageGraalHome task.

Example usage: nativeImageCommand := List("/path/to/native-image")

nativeImageReady

Type: SettingKey[() => Unit]

Description: a side-effecting callback that is called the native image is ready.

Default: alerts the message "Native image ready!" via the say command-line tool on macOS. Does nothing by default on Linux and Windows.

nativeImageCoursier

Type: TaskKey[File]

Description: the path to a coursier binary.

Default: copies a slim bootstrap binary from sbt-native-image resources. This setting is ignored if you customize nativeImageCommand to use something else than Coursier.

nativeImageOutput

Type: SettingKey[File]

Description: the path to the native-image binary that is generated.

Default: target/native-image/NAME where NAME is the name of the sbt project. for available options.

Example usage: nativeImageOutput := file("target") / "my-binary"

nativeImageInstalled

Type: SettingKey[Boolean]

Description: whether GraalVM is manually installed or should be downloaded with coursier.

Default: checks if NATIVE_IMAGE_INSTALLED / GRAALVM_INSTALLED environment variables or native-image-installed / graalvm-installed properties are set to true.

nativeImageGraalHome

Type: TaskKey[Path]

Description: path to GraalVM home directory.

Default: if nativeImageInstalled is true, then tries to read the path from environment variables 1) GRAAL_HOME, 2) GRAALVM_HOME or 3) JAVA_HOME (in given order). Otherwise, automatically installs GraalVM via Coursier. Customize this setting if you prefer to not to use environment variables.

Example usage: nativeImageGraalHome := file("/path/to/graalvm/base/directory").toPath

nativeImageRunAgent

Type: InputKey[Unit]

Description: run application, tracking all usages of dynamic features of an execution with native-image-agent.

Example usage:

First, add the reflection configuration to the native image options

  // build.sbt

lazy val myNativeProject = project
  .settings(
+    nativeImageOptions +=
+        s"-H:ReflectionConfigurationFiles=${target.value / "native-image-configs" / "reflect-config.json"}"
  )
  .enablePlugins(NativeImagePlugin)

Then, make sure to generate the reflection configuration with nativeImageRunAgent before running nativeImage.

# Step 0: Start sbt shell.
$ sbt
# Step 1: Run application on the JVM with native-image agent.
> myProject/nativeImageRunAgent " arg1 arg2"
# Step 2: Create native-image binary with assisted configuration.
> myProject/nativeImage
# Step 3: Run native-image that was generated with assisted configuration.
> myProject/nativeImageRun arg1 arg2

nativeImageAgentOutputDir

Type: SettingKey[File]

Description: directory where native-image-agent should put generated configurations.

Default: baseDirectory.value / "native-image-configs"

Example usage: nativeImageAgentOutputDir := baseDirectory.value / "native-image-agent" / "out"

nativeImageAgentMerge

Type: SettingKey[Boolean]

Description: whether native-image-agent should merge generated configurations.

Default: false

Example usage: nativeImageAgentMerge := true

Generate native-image from GitHub Actions

The easiest way to distribute native-image binaries for Linux and macOS is to build the binaries in CI with GitHub Actions.

  1. Copy the native.yml file from this repo into the .github/workflows/ directory in your project.

    mkdir -p .github/workflows && \
      curl -L https://raw.githubusercontent.com/scalameta/sbt-native-image/master/.github/workflows/native.yml > .github/workflows/native.yml
    
  2. Edit the file to replace "example" with the name of your binary.

  3. Commit your changes.

  4. Push your commit to GitHub and see the binary get uploaded as an artifact to the CI job.

  5. Create a GitHub release and see the binary get uploaded as assets to the release page.

Comparison with sbt-native-packager

The sbt-native-packager plugin provides similar support to generate native-image binaries. Check out their documentation at https://sbt-native-packager.readthedocs.io/en/stable/formats/graalvm-native-image.html

The key differences between sbt-native-packager and sbt-native-image are:

  • sbt-native-image automatically installs GraalVM native-image by default. You don't need to configure a docker image or manually install a correct GraalVM JDK before starting sbt.
  • sbt-native-image automatically works out-of-the-box with Scala 2.12.12+ and 2.13.3+. You don't need custom settings to work around issues like like scala/bug#11634.
  • sbt-native-image displays live progress output from the native-image while it's generating the binary. For some reason, sbt-native-packager only displays output from native-image after the process completes (see issue #1345).
  • sbt-native-packager has Docker support, which is helpful if you need more fine-grained control over the linking environment. There are no plans to add Docker support in sbt-native-image.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].