Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify an extra folder to be on the classpath for gradle's application plugin 'run' task?

I've successfully configured my gradle build script to create a zip distribution of my application with an extra 'config' folder at the root. This folder contains (at least right now) only one properties file in use by the application, and is on the classpath for the application.

What I'm looking for now, however, is 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 fails to run because of a class trying to access this properties file on the root of the classpath.

A bonus would be if I could get IntelliJ or Eclipse to also add this folder to its classpath just like the other folders (src/main/java, src/main/resources, ...) so I can run and debug my code from within the IDE without invoking a gradle task. I want to try to avoid as much as possible tying this code to any one IDE, so that when anybody needs to work on the project, they just need to import the build.gradle file and have the IDE make the appropriate config files it needs.

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 {
    ...
}

Likely what needs to happen is that I need to have the /src/dist/config folder to be copied into the build directory and added to the classpath, or have its contents be copied into a folder that is already on the classpath.

like image 872
Mirrana Avatar asked May 27 '15 19:05

Mirrana


1 Answers

I ended up taking Opal's suggestion as a hint, and came up with the following solution. I added the following to my 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')
}

Alternatively, a simpler approach would be to add a runtime dependency to my project as such:

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

I didn't end up doing it this way, however, because my distribution package ended up having .properties files in the lib folder... and I'm just picky that way.

like image 52
Mirrana Avatar answered Oct 18 '22 09:10

Mirrana