Could you please share a build.gradle script example with configured folder "conf":
Addition #1:
I've done actions mentioned in answer pointed by @wakjah, files was copied to a zip file and was not put into jar during gradle distZip
, this covers my requirement #1. But this folder is not on classpath in start scripts from bin
folder (requirement #2 is not covered). Files are not on classpath when running gradle clean run
(requirement #3 is not covered).
Addition #2:
I started sample project from scratch and with the help of @wakjah now I have:
config
contains resource file), but this resource is also in lib
folder.config
is on classpath in start script), but classpath additionally contains invalid folder %APP_HOME%\lib\config
Additionally, I tested 3 approaches of loading file contents (as mentioned here), method Thread.currentThread().getContextClassLoader().getResource(name)
succeeded.
Added my sample project zip file here: http://rgho.st/8r2dJjSz7
Addition #3:
When I comment / remove in gradle script:
dependencies {
runtime files('src/dist/config')
}
lib/config
folder is not on CLASSPATH (it's ok), and resource file gets not copied to lib
folder (it's ok). But resource file is failed to load both starting class main() method from Idea and running gradle clean run
from command prompt. After gradle clean distZip
and deploying (unpacking) application, resource gets loaded.
Addition #4:
After replacing
dependencies {
runtime files('src/dist/config')
}
to
tasks.withType(JavaExec) {
classpath += files('src/dist/config')
}
everything went fine, thanks, @wakjah!
There is one more issue, not mentioned in initial requirements: when I start this project inside Idea (not executing gradle run, but starting main() in class directly from Idea), file in config
directory could not be loaded using any of approaches mentioned earlier.
As mentioned here Gradle distZip config files you can just add the folder dist/config
to your src
folder and it will be included by the distZip
task automatically.
As for adding it to the runtime classpath, just add the folder as a dependency (see below).
Requirement #2 is a little harder, due to limitations with startScripts
. This question has an answer that proposes a workaround, however: Adding classpath entries using Gradle's Application plugin
The following code collects all of the above:
// put config files in src/dist/config
dependencies {
runtime files('src/dist/config')
}
startScripts {
classpath += files('src/dist/XxxAPlaceHolderForAConfigxxX')
doLast {
def windowsScriptFile = file getWindowsScript()
def unixScriptFile = file getUnixScript()
windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\XxxAPlaceHolderForAConfigxxX', '%APP_HOME%\\config')
unixScriptFile.text = unixScriptFile.text.replace('$APP_HOME/lib/XxxAPlaceHolderForAConfigxxX', '$APP_HOME/config')
}
}
I have tested this and it appears to work. The correct classpath entries appear in both the run scripts and the command line used by gradle during the run
task.
EDIT: In your comment you implied that you also want this to be available in the classpath of your build script. If that is the case, you need to add a buildscript dependency like this:
buildscript {
dependencies {
classpath files('src/dist/config')
}
}
EDIT #2: In another comment you mention that adding the runtime dependency has the unintended consequence of producing an extra, invalid, entry in the start script classpath. You could fix this by just adding to the classpath of any JavaExec
tasks, rather than using a dependency. So replace the dependencies { runtime { ... } }
block with
tasks.withType(JavaExec) {
classpath += files('src/dist/config')
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With