Use jlink with gradle

Introduction

If you use jlink, you can create a JRE dedicated to the app, which is convenient for distribution. So I tried to find out how to use it with gradle.

Project preparation

For the time being, normally

gradle init --type=java-application

Make with.

Rewrite build.gradle

The minimum requirement is to add the following line to the plugins item.

id 'org.beryx.jlink' version '2.22.0'

Now you can use gradle jlink.

Also, if you delete the dependencies that are included by default, the warning disappears. (It seems that module names ending with numbers should not be used)

In addition, depending on the version of JDK

The module name specified in 'application.mainModule' (null) has not the expected value

Is displayed. This is in the application item

mainModule ='module name'

It disappears if you write. (The module name is specified in module-info.java)

It is also possible to create a jlink item and set an option. For example, you can write as follows.

jlink {
    options = ['--compress', '2', '--no-header-files', '--no-man-pages']
}

You can also create apps for multiple platforms with targetPlatform. In this case, prepare a JDK for each platform.

jlink {
    targetPlatform("mac") {
        jdkHome = "/usr/java/jdk-mac/Contents/Home"
    }
    targetPlatform("linux-x64") {
        jdkHome = "/usr/java/jdk"
    }
    targetPlatform("windows-x64") {
        jdkHome = "/usr/java/jdk-win"
    }
    options = ['--compress', '2', '--no-header-files', '--no-man-pages']
}

Build

gradle build

Now you can jar without using jlink. When jlinking, do as follows.

gradle jlink

Now you have an image under the build directory, and the directory under it is your app. If targetPlatform is specified, a directory of \ <project name >-\ <target platform name > will be created and placed under it.

To execute the application, there is an execution script of \ <project name > in the bin directory, so you can execute it. (Bat file for Windows)

You can distribute the application by distributing the bin, conf, legal, and lib directories together. You can start it by copying it as it is and executing the bin executable file.

Recommended Posts

Use jlink with gradle
Use ProGuard with Gradle
Use WebJars with Gradle
Use log4j2 with YAML + Gradle
Integration Test with Gradle
Use Puphpeteer with Docker
Install Gradle with ubuntu16.04
DataNucleus starting with Gradle
Use ngrok with Docker
How to use Gradle
Get started with Gradle
Use Lambda Layers with Java
Java multi-project creation with Gradle
Use GDAL with Python with Docker
Gradle + Kotlin-Generate jar with DSL
Use pfx certificate with Okhttp3
Lombok not working with Gradle5
Remote debugging with Gradle test
Use multiple databases with Rails 6.0
Use Spring JDBC with Spring Boot
Use Ruby with Google Colab
Use SpatiaLite with Java / JDBC
Hello World with SpringBoot / Gradle
[Docker] Use whenever with Docker + Rails
Use PlantUML with Visual Studio Code
Use Basic Authentication with Spring Boot
Build a Java project with Gradle
Use java with MSYS and Cygwin
Use constructor with arguments in cucumber-picocontainer
Gradle
Use PostgreSQL inet type with DbUnit
I tried using JOOQ with Gradle
Why use orchestration tools with Docker
Use bootstrap 4 with PlayFramework 2.6 (no CDN)
Let's use Amazon Textract with Ruby
Output test coverage with clover + gradle
I can't install lombok with Gradle.
Use Git with SourceTree and Eclipse
Use JDBC with Java and Scala.
Use Java 11 with Google Cloud Functions
How to use mssql-tools with alpine
Beginning with Spring Boot 0. Use Spring CLI
Use cuda11.0 with pytorch using Docker
Develop Processing with IntelliJ + Kotlin + Gradle
Spring Boot gradle build with Docker
[Java] Create an executable module with Gradle
[Rails] How to use rails console with docker
Use Firebase Realtime Database with Cocos Creator.
I want to use DBViewer with Eclipse 2018-12! !!
Use Symbolic Link with Docker multi-stage builds
Use FacesContext as a Mock with PowerMockito
[JaCoCo (Java Code Coverage)] Use with NetBeans
Use cljstyle with Spacemacs on Ubuntu on WSL2
Use Spring Security JSP tags with FreeMarker
Configure a multi-project with subdirectories in Gradle
JUnit 5 gradle test fails with lombok annotation
Use a named format with Ruby's format method
Java automated test implementation with JUnit 5 + Gradle
Download JDK with Gradle and make JRE
Add scripts to distributions created with gradle
(IntelliJ + gradle) Hello World with Spring Boot