Jacob
Jacob

Reputation: 217

Change how Gradle's build directory is organized

So, I've recently (partially) completed a Java project with Gradle. Importantly, the project uses absolute pathing to access files in my resources folder, since those files will change after the JAR is made. When I use Eclipse's "export as runnable JAR" functionality, I get something that works perfectly - putting the .jar file in my main directory lets it find everything. However, using Gradle's build function doesn't, because Gradle adds extra layers between the .jar and the resources. To demonstrate, here's my "normal" directory:

./program root directory
|_program.jar
|_resources
  |_[actual resources]

And here's the directory Gradle makes:

./build folder
|_libs
| |_program.jar
|_resources
  |_main
    |_[actual resources]

What I want from Gradle is:

./build folder
|_program.jar
|_resources
  |_[actual resources]

Yes, I could manually move the resources and program.jar around in the directory to achieve this, but that feels wrong - this is exactly what Gradle is supposed to do for me, right? I know there has to be SOME way to do it. I just don't know how. So that's why I'm asking for help - how do I do this?

Upvotes: 2

Views: 2676

Answers (1)

Slaw
Slaw

Reputation: 46170

To change the output of resources:

sourceSets.main.output.resourcesDir = "$buildDir/resources"

To change where the JAR file is put:

jar {
    // use destinationDir for Gradle < 5.1
    destinationDirectory = buildDir
}

If all your resources are meant to be external you may want to exclude them from the JAR file:

jar {
    include '**/*.class'
    destinationDirectory = buildDir
}

That will only include .class files from the jar task's input. You can customize this using the include and exclude options.

Upvotes: 1

Related Questions