[SOLVED] Need help with loading app config and gradle build


This is a Gradle noob question but I could not resolve it via google. I hope someone here can help me. Thanks

I want to create an external configuration file for my app to load it with Java Properties at runtime. To do so, I have added


in “applicationDefaultJvmArgs” inside build.gradle file.

then added this at the end of the file

startScripts.doLast {
    unixScript.text = unixScript.text.replace('[MY_APP_HOME]', '\$APP_HOME')
    windowsScript.text = windowsScript.text.replace('[MY_APP_HOME]', '%~dp0..')

I am using Linux and building the app with Gradle “distZip” task. After that, extract the zip file (it contains the “bin” and “lib” directories) there I create a new directory “conf” and add “application.properties” into that directory.

But on app execution, I am getting this error

File $APP_HOME/conf/application.properties could not be resolved.

at this line:

I also tried to put it inside the “bin” directory but no difference.

Where should I put the config file? Where is the ‘$APP_HOME’ directory?


What do the actual scripts look like after the replace?

The Linux script:

# Attempt to set APP_HOME

# Resolve links: $0 may be a link

# Need this for daisy-chained symlinks.
    APP_HOME=${app_path%"${app_path##*/}"}  # leaves a trailing /; empty if no leading path
    [ -h "$app_path" ]
    ls=$( ls -ld "$app_path" )
    link=${ls#*' -> '}
    case $link in             #(
      /*)   app_path=$link ;; #(
      *)    app_path=$APP_HOME$link ;;

APP_HOME=$( cd "${APP_HOME:-./}.." && pwd -P ) || exit


# Add default JVM options here. You can also use JAVA_OPTS and MODEL_VIEWER_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-XX:+PrintCommandLineFlags" "-XX:+UnlockExperimentalVMOptions" "-Xms35m" "-Xmx128m" "-XX:MaxDirectMemorySize=300m" "--add-opens=java.base/jdk.internal.ref=ALL-UNNAMED" "-Dapplication.name=Model Viewer" "-Dcom.jme3.BufferAllocatorImplementation=com.jme3.util.LWJGLBufferAllocator\$ConcurrentLWJGLBufferAllocator" "-Dapplication.configurationFile=$APP_HOME/conf/application.properties" "-XX:+UseG1GC" "-XX:MaxGCPauseMillis=10"'

# Use the maximum available, or set MAX_FD != -1 to use that value.

warn () {
    echo "$*"
} >&2

die () {
    echo "$*"
    exit 1
} >&2

# OS specific support (must be 'true' or 'false').
case "$( uname )" in                #(
  CYGWIN* )         cygwin=true  ;; #(
  Darwin* )         darwin=true  ;; #(
  MSYS* | MINGW* )  msys=true    ;; #(
  NONSTOP* )        nonstop=true ;;


This is how the -Dapplication.configurationFile VM option looks like after replace:


And here is the windows script:

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and MODEL_VIEWER_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-XX:+PrintCommandLineFlags" "-XX:+UnlockExperimentalVMOptions" "-Xms35m" "-Xmx128m" "-XX:MaxDirectMemorySize=300m" "--add-opens=java.base/jdk.internal.ref=ALL-UNNAMED" "-Dapplication.name=Model Viewer" "-Dcom.jme3.BufferAllocatorImplementation=com.jme3.util.LWJGLBufferAllocator$ConcurrentLWJGLBufferAllocator" "-Dapplication.configurationFile=%~dp0../conf/application.properties" "-XX:+UseG1GC" "-XX:MaxGCPauseMillis=10"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute

echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

@rem Setup the command line

set CLASSPATH=%APP_HOME%\lib\ModelViewer.jar;%APP_HOME%\lib\jme3-lwjgl3-3.5.0-beta6.jar;%APP_HOME%\lib\jme3-desktop-3.5.0-beta6.jar;%APP_HOME%\lib\jme3-effects-3.5.0-beta6.jar;%APP_HOME%\lib\lemur-proto-1.12.0.jar;%APP_HOME%\lib\lemur-props-1.1.1.jar;%APP_HOME%\lib\sim-fx-1.0.1-SNAPSHOT.jar;%APP_HOME%\lib\Heart-7.1.0.jar;%APP_HOME%\lib\jme3-jogg-3.5.0-beta6.jar;%APP_HOME%\lib\jme3-plugins-3.5.0-beta6.jar;%APP_HOME%\lib\lemur-1.15.1-SNAPSHOT.jar;%APP_HOME%\lib\jme3-core-3.5.0-SNAPSHOT.jar;%APP_HOME%\lib\webp-imageio-0.2.2.jar;%APP_HOME%\lib\jme-utils-1.0.0-SNAPSHOT.jar;%APP_HOME%\lib\log4j-slf4j-impl-2.17.1.jar;%APP_HOME%\lib\slf4j-api-1.7.32.jar;%APP_HOME%\lib\log4j-core-2.17.1.jar;%APP_HOME%\lib\log4j-api-2.17.1.jar;%APP_HOME%\lib\groovy-all-2.4.11.jar;%APP_HOME%\lib\guava-19.0.jar;%APP_HOME%\lib\commons-text-1.9.jar;%APP_HOME%\lib\j-ogg-all-1.0.1.jar;%APP_HOME%\lib\gson-2.8.1.jar;%APP_HOME%\lib\lwjgl-glfw-3.3.0.jar;%APP_HOME%\lib\lwjgl-glfw-3.3.0-natives-windows.jar;%APP_HOME%\lib\lwjgl-glfw-3.3.0-natives-windows-x86.jar;%APP_HOME%\lib\lwjgl-glfw-3.3.0-natives-linux.jar;%APP_HOME%\lib\lwjgl-glfw-3.3.0-natives-linux-arm32.jar;%APP_HOME%\lib\lwjgl-glfw-3.3.0-natives-linux-arm64.jar;%APP_HOME%\lib\lwjgl-glfw-3.3.0-natives-macos.jar;%APP_HOME%\lib\lwjgl-jemalloc-3.3.0.jar;%APP_HOME%\lib\lwjgl-jemalloc-3.3.0-natives-windows.jar;%APP_HOME%\lib\lwjgl-jemalloc-3.3.0-natives-windows-x86.jar;%APP_HOME%\lib\lwjgl-jemalloc-3.3.0-natives-linux.jar;%APP_HOME%\lib\lwjgl-jemalloc-3.3.0-natives-linux-arm32.jar;%APP_HOME%\lib\lwjgl-jemalloc-3.3.0-natives-linux-arm64.jar;%APP_HOME%\lib\lwjgl-jemalloc-3.3.0-natives-macos.jar;%APP_HOME%\lib\lwjgl-openal-3.3.0.jar;%APP_HOME%\lib\lwjgl-openal-3.3.0-natives-windows.jar;%APP_HOME%\lib\lwjgl-openal-3.3.0-natives-windows-x86.jar;%APP_HOME%\lib\lwjgl-openal-3.3.0-natives-linux.jar;%APP_HOME%\lib\lwjgl-openal-3.3.0-natives-linux-arm32.jar;%APP_HOME%\lib\lwjgl-openal-3.3.0-natives-linux-arm64.jar;%APP_HOME%\lib\lwjgl-openal-3.3.0-natives-macos.jar;%APP_HOME%\lib\lwjgl-opencl-3.3.0.jar;%APP_HOME%\lib\lwjgl-opengl-3.3.0.jar;%APP_HOME%\lib\lwjgl-opengl-3.3.0-natives-windows.jar;%APP_HOME%\lib\lwjgl-opengl-3.3.0-natives-windows-x86.jar;%APP_HOME%\lib\lwjgl-opengl-3.3.0-natives-linux.jar;%APP_HOME%\lib\lwjgl-opengl-3.3.0-natives-linux-arm32.jar;%APP_HOME%\lib\lwjgl-opengl-3.3.0-natives-linux-arm64.jar;%APP_HOME%\lib\lwjgl-opengl-3.3.0-natives-macos.jar;%APP_HOME%\lib\lwjgl-3.3.0.jar;%APP_HOME%\lib\lwjgl-3.3.0-natives-windows.jar;%APP_HOME%\lib\lwjgl-3.3.0-natives-windows-x86.jar;%APP_HOME%\lib\lwjgl-3.3.0-natives-linux.jar;%APP_HOME%\lib\lwjgl-3.3.0-natives-linux-arm32.jar;%APP_HOME%\lib\lwjgl-3.3.0-natives-linux-arm64.jar;%APP_HOME%\lib\lwjgl-3.3.0-natives-macos.jar;%APP_HOME%\lib\commons-lang3-3.11.jar;%APP_HOME%\lib\jmf-2.1.1e.jar

@rem Execute ModelViewer

@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

rem Set variable MODEL_VIEWER_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if  not "" == "%MODEL_VIEWER_EXIT_CONSOLE%" exit 1
exit /b 1

if "%OS%"=="Windows_NT" endlocal


This is how the -Dapplication.configurationFile VM option looks like after replace:


Maybe because DEFAULT_JVM_OPTS wraps things inside '' the $APP_HOME gets ignored and read as string?


See this comment in the script

# Add default JVM options here. You can also use JAVA_OPTS and MODEL_VIEWER_OPTS to pass JVM options to this script.

I manually added this without '' and it worked


Next, I need to find out how to set MODEL_VIEWER_OPTS in Gradle?

Any hint on how I can prevent runtimeOnly files('...') dependency from being included in the lib directory when distributing zip using the “distZip” task?

I know I can comment those lines when distributing but I am looking for an alternative way.

Can you describe why your app needs it but doesn’t need it need it?

…might be helpful with some context.

I think you could control this distZip task to be dependent upon a deleteTask for example that deletes the unwanted files after building them and before packaging.

EDIT : Another solution :

  • Move your ‘files’ to another gradle module (:foo) and include the module inside your gradle.settings.
  • Do implementation project(path: ':foo') instead of runtime files('...')
  • Create a mobilizeOut and a mobilizeIn tasks,
    mobilizeOut : should move the files inside foo module to a hidden temporary folder in the root Dir for example (.temp) before the distZip runs.
    mobilizeIn : should move the files back to the :foo module from the .temp folder after the distZip task executes leaving the project unaffected.

I think there are probably a hundred ways to solve this problem… but it would be easier to find the best one if we understood context.

How does the app need these but not need these? Is the user expected to download them separately?

…or is this a runtime determined dependency that gradle is being hacked to simulate during “development time” but the app ignores during “real run time”?

I feel like there is a missing piece that might have nothing to do with gradle but it’s hard to say.

Edit: and to be blunt, when gradle ultimately provides you complete control over how configurations and dependencies are mapped to tasks… moving files around on disk is the kind of hack that feels like banging sticks and rocks together.

My app needs the file. The file is inside src/dist/config. Running ditZip will automatically pack it into the zip file (inside the “config” directory).

I use runtimeOnly files('src/dist/config') for development time only and If I forget to remove that runtime dependency before running distZip, the file will be also included in the lib directory. So I will have duplicate files, one inside the config directory and one in the lib directory.

A boolean flag switched to true to not include this line when running the distZip may work.

1 Like

Something like this?

def isDistributing = false
dependencies {
    if (!isDistributing) {
        runtimeOnly files('src/dist/config')

distZip {
    doFirst {
        isDistributing = true

It did not work.

Can you check for the boolean using println() at the beginning of the dependencies block ?

As far as i know, you need to make the dependency block dependent upon the switching task, so the switching task runs before the configuration phase.

So, something like :

def isDistributing = false
dependencies {
    if (!isDistributing) {
        runtimeOnly files('src/dist/config')

task switchOff() {
    doFirst {
        isDistributing = true

compileJava.dependsOn(switchOff) // or setDependsOn()

If you run the distZip manually from ./gradlew :distZip then you may add the line :
dependsOn: switchOff as a parameter for distZip.

Because you want the app run task to have it?

Why not just give it as a dependency to the app run task then instead of making it a runtime dependency for the whole application?


How can I do that?

My own gradle-fu might be too old to be of direct use here but the application plugin is just configuring its own exec task, etc… looking at the docs it should be possible to reference that task and add something to its classpath.

Is it really the case that the normal app loads it as a file on disk and the ‘development mode’ app loads it from the classpath?

Nope, both of them load it from the classpath. The config directory in the distributed app is already in the classpath.

@pspeed thanks for the hint. This did the job

run {
    classpath += files('src/dist/config')
1 Like