Giter VIP home page Giter VIP logo

graalvm-native-plugin's Introduction

Graalvm Native Gradle plugin

Supports for building Java applications as GraalVM native images.

https://plugins.gradle.org/plugin/com.formkiq.gradle.graalvm-native-plugin

Quick start

Apply Gradle plugin

Groovy

Using the plugins DSL:

plugins {
    id 'com.formkiq.gradle.graalvm-native-plugin' version '1.4.0'
}

Using legacy plugin application:

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "com.formkiq.gradle:graalvm-native-plugin:1.4.0"
  }
}

apply plugin: "com.formkiq.gradle.graalvm-native-plugin"

Kotlin

Using the plugins DSL:

plugins {
    id("com.formkiq.gradle.graalvm-native-plugin") version "1.4.0"
}

Using legacy plugin application:

buildscript {
  repositories {
    maven {
      url = uri("https://plugins.gradle.org/m2/")
    }
  }
  dependencies {
    classpath("com.formkiq.gradle:graalvm-native-plugin:1.4.0")
  }
}

apply(plugin = "com.formkiq.gradle.graalvm-native-plugin")

Specify build arguments

This plugin uses the following example Gradle extension for configuration:

nativeImage {
    mainClassName = 'com.example.Application'

    enableHttp          = true
    enableHttps         = true
}

More configuration options can be found here.

Build GraalVM Native Image

  1. Run the Gradle task graalvmNativeImage
  2. The native image can be located at <buildDir>/graalvm

Sample project

samples contains various samples that demonstrate the basic usage of this Gradle plugin.

Configuration

Property Type Description
imageVersion String The GraalVM Community Edition version to download. Default to 20.2.0.
javaVersion String The JDK version to be downloaded with GraalVM Community Edition. Default to 11.
dockerImage String Graalvm Docker Image build using.
imageFile String A local Image File to instead of downloading a file based on imageVersion/javaVersion/architecture.
mainClassName (Required) String The fully qualified name of the Java class that contains a main method for the entry point of the Native Image executable.
enableTraceClassInitialization boolean Provides useful information to debug class initialization issues.
enableRemoveSaturatedTypeFlows boolean Reduces build time and decrease build memory consumption, especially for big projects.
enableReportExceptionStackTraces boolean Provides more detail should something go wrong.
enablePrintAnalysisCallTree boolean Helps to find what classes, methods, and fields are used and why. You can find more details in GraalVM reports documentation.
enableAllSecurityServices boolean Adds all security service classes to the generated image. Required for HTTPS and crypto.
enableHttp boolean Enables HTTP support in the generated image.
enableHttps boolean Enables HTTPS support in the generated image.
enableVerbose boolean Makes image building output more verbose.
enableAllowIncompleteClasspath boolean Allow image building with an incomplete class path.
enableNoFallback boolean Build stand-alone image or report failure.
enableAutoFallback boolean Build stand-alone image if possible.
enableForceFallback boolean Force building of fallback image.
enableForceFallback boolean Force building of fallback image.
enableInstallExitHandlers boolean Provide java.lang.Terminator exit handlers for executable images.
enableShared boolean Build shared library.
enableStatic boolean Build statically linked executable.
enableCheckToolchain boolean Check if native-toolchain is known to work with native-image.
enableReportUnsupportedElementsAtRuntime boolean Report usage of unsupported methods and fields at run time.
initializeAtBuildTime List<String> Use it with specific classes or package to initialize classes at build time.
initializeAtRunTime List<String> Use it with specific classes or package to initialize classes at run time.
reflectionConfig String GraalVM Reflection Configuration File to enable Java reflection support.
systemProperty List<String> Java System Properties to use when building Graalvm Image.
addClasspath String Additional Classpaths comma separated.
features String a comma-separated list of fully qualified Feature implementation classes.
outputFileName String Output File Name.

Using with Windows

Using the Graalvm Native Gradle plugin under Windows requires installing and using the Microsoft C++ toolset.

Here are some instructions (partially) plagiarized from https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line.

To open a GraalVM suitable developer command prompt window

On the desktop, open the Windows Start menu, and then scroll to find and open the folder for your version of Visual Studio, for example, Visual Studio 2019.

In the folder, choose the "x64 Native Tools Command Prompt" for your version of Visual Studio. This shortcut starts a developer command prompt window that uses the default build architecture of 64-bit, x86-native tools to build 64-bit, x86-native code. There are other build architectures available, these do not work with GraalVM.

An even faster way to open the appropriate developer command prompt, enter "x64 Native Tools Command Prompt" in the desktop search box.

These command prompt shortcuts are installed in a version-specific Visual Studio folder in your Start menu. The Start menu folder and shortcut names may vary depending on the installed version of Visual Studio. If you set one, they also depend on the installation Nickname. For example, suppose you installed Visual Studio 2019, and you gave it a nickname of Latest. The developer command prompt shortcut is named "x64 Native Tools Command Prompt for VS 2019 (Latest)", in a folder named Visual Studio 2019.

License

Apache 2

References

graalvm-native-plugin's People

Contributors

formkiqmike avatar mfriesen avatar ruffcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

graalvm-native-plugin's Issues

Incorrect path? ... build/graalvm/graalvm-ce-java11-20.2.0/Contents/Home/bin/gu

Execution failed for task ':graalvmNativeImage'.
> A problem occurred starting process 'command '/home/rob/github/other/example-graal/hello/build/graalvm/graalvm-ce-java11-20.2.0/Contents/Home/bin/gu''

So this path does not exist. What does exist is build/graalvm/graalvm-ce-java11-20.2.0/bin/gu
So removing the Contents/Home part makes the path correct.

GraalVM not finding cl.exe compiler on Windows 10

After making this correction.
When I run this on my Windows 10 environment I get the following error.

 ./gradlew graalvmNativeImage

> Task :app:graalvmNativeImage
Downloading: Component catalog from www.graalvm.org
Processing Component: Native Image
Downloading: Component native-image: Native Image  from github.com
[#                   ]ECUTING [1m 25s]      [##                  ]                      [###     Installing new component: Native Image (org.graalvm.native-image, version 21.0.0)
Warning: Ignoring server-mode native-image argument --no-server.
[app:13084]    classlist:   3,043.27 ms,  0.96 GB
[app:13084]        setup:   1,946.53 ms,  0.96 GB
Error: Default native-compiler executable 'cl.exe' not found via environment variable PATH
Error: To prevent native-toolchain checking provide command-line option -H:-CheckToolchain
com.oracle.svm.core.util.UserError$UserException: Default native-compiler executable 'cl.exe' not found via environment variable PATH
To prevent native-toolchain checking provide command-line option -H:-CheckToolchain
        at com.oracle.svm.core.util.UserError.abort(UserError.java:139)
        at com.oracle.svm.hosted.c.codegen.CCompilerInvoker.addSkipCheckingInfo(CCompilerInvoker.java:104)
        at com.oracle.svm.hosted.c.codegen.CCompilerInvoker.<init>(CCompilerInvoker.java:72)
        at com.oracle.svm.hosted.c.codegen.CCompilerInvoker$WindowsCCompilerInvoker.<init>(CCompilerInvoker.java:110)
        at com.oracle.svm.hosted.c.codegen.CCompilerInvoker.create(CCompilerInvoker.java:84)
        at com.oracle.svm.hosted.NativeImageGenerator.setupNativeImage(NativeImageGenerator.java:877)
        at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:561)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:476)
        at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
        at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
        at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
        at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
        at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Error: Image build request failed with exit status 1

BUILD SUCCESSFUL in 1m 52s
8 actionable tasks: 8 executed

How is the cl.exe expected to be located?
I have looked at https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line but I do not see how to get that to work with gradle.

Kotlin support

Hi I hope you are doing well
Does this plugin supports Kotlin?

I faced this issue when trying to use with a src/main/kotlin project:

A problem was found with the configuration of task ':graalvmNativeImage' (type 'GraalvmNativeTask').

  • In plugin 'com.formkiq.gradle.graalvm-native-plugin' type 'com.formkiq.gradle.GraalvmNativeTask' property 'sourceFileDir' specifies directory '/myproject/src/main/java' which doesn't exist.

Thanks

Misplaced `nativeImage` flags

In sample/helloword the enable switches are (incorrectly) placed in the application configuration.

// creates binary file 'app/build/graalvm/app'
nativeImage {
    mainClassName = 'helloworld.App'
    enableCheckToolchain = true
    enableReportExceptionStackTraces = true
}

java.nio.file.AccessDeniedException under Linux using Docker Image

When building Graalvm Image using Docker Image under Linux, getting java.nio.file.AccessDeniedException. The plugin is mounting volume directories, if the directory doesn't exist Docker creates the directory but the directory is created as root and the user running the build does not have access to it.

java.lang.ClassNotFoundException: com.demo.DemoMain

./openJFXdemo
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: com.demo.DemoMain
at javafx.application.Application.launch(Application.java:304)
at com.demo.DemoMain.main(DemoMain.java:26)
Caused by: java.lang.ClassNotFoundException: com.demo.DemoMain
at com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:60)
at java.lang.Class.forName(DynamicHub.java:1292)
at javafx.application.Application.launch(Application.java:292)
... 1 more

Gradle configruation

apply plugin: "java"
apply plugin: "application"
apply plugin: "org.openjfx.javafxplugin"
apply plugin: "com.formkiq.gradle.graalvm-native-plugin"

javafx {
    version = "15"
    modules = ["javafx.base", "javafx.controls", "javafx.fxml"
//               , "javafx.graphics"
//               , "javafx.media", "javafx.swing", "javafx.web"
    ]
}

nativeImage {
    mainClassName = "com.demo.DemoMain"
    javaVersion = "java11"
    imageVersion = "20.3.0"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.