KIC
KIC

Reputation: 6119

How to set JAVA_OPTS with spring boot gradle plugin

I try to set the JAVA_OPTS but the generated script unter build/bootScripts is not accepting the configuration.

plugins {
    id 'java'
    id 'application'
    id 'org.springframework.boot' version '2.0.2.RELEASE'
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
}

version '1.0-SNAPSHOT'

repositories {
    jcenter()
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = "kic.data.server.Server"

applicationDefaultJvmArgs  = [
        '-Dkic.data.persistency.path=./data'
]

In the bootScripts/server I only get an empty variable

# Add default JVM options here. You can also use JAVA_OPTS and SERVER_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""

I have also tried to place a conf file to the jar file in the lib folder which unfortunately does not work either.

Upvotes: 4

Views: 3521

Answers (1)

Tony
Tony

Reputation: 388

Here is how I did it:

Make a file with your JVM options. For example javaOpts.conf:

JAVA_OPTS="$JAVA_OPTS -Dkic.data.persistency.path=./data -Xmx1024m"

The spring boot plugin automatically provides a bootJar task that can be used to generate your runnable jar. You can have that task insert your conf file into the launch script by adding this in your build.gradle file:

bootJar{
    launchScript {
        properties 'inlinedConfScript': 'path/to/your/javaOpts.conf'
    }
}

That will dump anything in your custom conf script into the launch script generated by spring boot. It's not documented here, so maybe it will change, but the default launch script includes the JAVA_OPTS environmental variable when it runs the jar.

Run the bootJar task (gradlew bootJar) and it will create an executable jar you can run from the command line: ./myBootJar.jar.

Looks like you were trying to set the mainClassName for your jar too. You can also use the bootJar task do that:

bootJar{
    mainClassName = 'kic.data.server.Server'
    launchScript {
        properties 'inlinedConfScript': 'path/to/your/javaOpts.conf'
    }
}

Upvotes: 2

Related Questions