How to specify an additional folder for the class path for gradle application plugin task?

I successfully configured my gradle build script to create a zip distribution for my application with an extra β€œconfig” folder in the root. This folder contains (at least right now) only one properties file used by the application and is located in the class path for the application.

However, now I am looking for a way to do the same with the run task in the application plugin. When I try to run my application this way (for testing), my program does not start because of a class trying to access this properties file in the root of the class path.

The bonus would be if I could force IntelliJ or Eclipse to also add this folder to my classpath, like other folders (src / main / java, src / main / resources, ...) so that I can run and debug my code from the IDE without calling the gradle task. I want to try to avoid linking this code as much as possible with any IDE, so when someone needs to work on a project, they just need to import the build.gradle file and provide the IDE with the configuration files they need.

Here is my build.gradle file:

apply plugin: 'application' mainClassName = "MainClass" startScripts { // Add config folder to classpath. Using workaround at // https://discuss.gradle.org/t/classpath-in-application-plugin-is-building-always-relative-to-app-home-lib-directory/2012 classpath += files('src/dist/config') doLast { def windowsScriptFile = file getWindowsScript() def unixScriptFile = file getUnixScript() windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\config', '%APP_HOME%\\config') unixScriptFile.text = unixScriptFile.text.replace('$APP_HOME/lib/config', '$APP_HOME/config') } } repositories { ... } dependencies { ... } 

It is likely that what should happen is that I need the / src / dist / config folder to be copied to the assembly directory and added to the class path, or its contents will be copied to a folder that is already in the class path.

+8
java classpath eclipse intellij-idea gradle
source share
2 answers

In the end, I made Opal's suggestion as a hint and came up with the following solution. I added the following to the build.gradle file:

 task processConfig(type: Copy) { from('src/main/config') { include '**/*' } into 'build/config/main' } classes { classes.dependsOn processConfig } run { classpath += files('build/config/main') } 

As an alternative, a simpler approach would be to add an execution dependency for my project as such:

 dependencies { ... runtime files('src/main/config') } 

I did not do this like that because my distribution was in the lib folder with the .properties files ... and I'm just picky.

+15
source share

As you can see in docs run job of type JavaExec . So the classpath for it can be changed. Try adding the configuration folder to the classpath. See here .

+2
source share

All Articles