[Application Plugin] Obtaining APP_HOME within an application?

This issue has been itching for quite some time :wink:

We use the Gradle Application plugin for all of our standalone java processes. So far, this works well with the exception of 2 issues.

The first one is adding a folder as classpath entry and is already being discussed here: http://forums.gradle.org/gradle/topics/classpath_in_application_plugin_is_bilding_always_relative_to_app_home_lib_directory

The second has not yet been discussed (at least I can’t seem to find anything about it). It is about getting ‘APP_HOME’ from within an application: Let’s say we have a config file located in ‘$APP_HOME/etc/config.properties’. Now, either we don’t want to add the whole ‘$APP_HOME/etc/’ directory to the classpath, or we don’t want to load the file through the Classloader but need direct file system access to it.

How can we target the file from within the application’s code? Prior to using Gradle, we used to set the Current Working Directory to the ‘APP_HOME’ dir. We then used something like

File configFile = new File(System.getProperty("user.dir") + SEPARATOR + "etc" + SEPARATOR + "config.properties");

to target the file from the CWD.

I understand that Gradle doesn’t set the CWD to ‘APP_HOME’ by default as that would be bad practice and not suitable for all applications. But since there is no option to do it, we are unable to reliably obtain a full path to a file somewhere beneath ‘APP_HOME’.

I see a couple of options here. Depending on the application itself, the one or the other might be better suited: *

Adding an option to set the CWD to ‘APP_HOME’ in the start scripts before launching the JVM. *

Adding an option to pass ‘APP_HOME’ to the JVM in a System Property in the start scripts. *

Read the ‘APP_HOME’ environment variable from within the Application code (requires fixing http://forums.gradle.org/gradle/topics/linux_launcher_script_should_export_app_home_before_starting_the_application)

I’d be happy to hear your comments and suggestions…

** Edit: I found this post http://forums.gradle.org/gradle/topics/linux_launcher_script_should_export_app_home_before_starting_the_application which suggests just reading the ‘APP_HOME’ environment variable, but reports an issue with a missing ‘export’ in the linux script. I added this as a third option in my suggestions.

I think I have the same problem. I need file access to the APP_HOME and I need to pass file path into a system property that is relative located to APP_HOME.

I tried it that way:

  defaultJvmOpts = ["-Dapp.home=%APP_HOME%", "-Djava.security.policy=%APP_HOME%\etc\client.policy"]

There are two reasons why this doesn’t work:

  • APP_HOME is defined after DEFAULT_JVM_OPTS * the % character is replaced by %% in order to suppress variable substitution


You’ll have to add some stuff to the script to do this. Here’s an example of a similar kind of thing: https://github.com/ratpack/ratpack/blob/master/ratpack-gradle/src/main/groovy/ratpack/gradle/RatpackPlugin.groovy#L95