How do I specify the resources folder in a Jar?

I am converting a project from ant to gradle. The ant project puts resources in to a resources folder in the jar. I want to do the same in gradle.

This is the project structure

project
+-- sources
+-- resources
    +-- config.properties
+-- test

This is the source sets

sourceSets {
    main {
        java {
            srcDir 'source'
        }
        resources {
            srcDir 'resources'
            include 'config.properties'
        }
    }
    test {
        java {
            srcDir 'test'
        }
    }
}

The resulting jar looks like this

project.jar
+-- com
+-- META-INF
+-- config.properties

I need the resulting jar to put config.properties in resources

project.jar
+-- com
+-- META-INF
+-- resources
    +-- config.properties

I was hoping that by adding an into under the include I could get what I want but that did not work. Is there any simple way to specify the folder resources go into when my project is built? I tried the output.resourcesDir but that only changed the location in the buildDir not the jar. I’m hoping for a simple feature that does this. I’m currently using gradle 2.4 but hope to upgrade to 2.12 soon.

The simplest solution would be to make another nested resources folder and move the existing prop files. Eg:

project
+-- sources
+-- resources
    +-- resources
        +-- config.properties

This would mean that you could interchangably put either the jar file or the (top level) resources folder on the classpath without issues (eg test cases)

Remove the resources block and just configure the jar task appropriately:

jar {
  into 'resources', {
    from 'resources'
  }
}
1 Like

Lance_Java, That is not possible right now. We are trying to replace a legacy ant build and that would break it.

Schalk_Cronje, I will try that and let you know.

Hey this worked great. Thanks for the tip!

Hi,
I have the same issue here, can you please help to understand what goes wrong with my build file reported below?
I am using Gradle 5.2.1
Thanks a lot

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
    }

    test {
        java {
            srcDirs = ['test']
        }
    }
}

dependencies {
    compile 'commons-logging:commons-logging:1.1.3'
    compile 'org.jetbrains:annotations:13.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
}

jar {
    into 'resources', {
        from 'resources'
    }
}

test {
    useJUnitPlatform()
}

What are you seeing?

Thank you! Simply the resources are located at the root level of my jar package while I was expecting them grouped in a “resource” directory.
Should I also manually manage the copy of those resources in the build directory with a custom copy task?

A custom copy task as the one reported below is in fact able to solve the issue and I have obtained what I expected described in my previous post. Anyway, for the sake of completeness, I will be grateful if someone can explain:

  1. does Gradle in default config package resources at root level of the jar file or I can suppose that there is still something inusual in my setup?
  2. there is any philosophical reason to package all the resources at root level of a jar?

Can someone point to a relevant documentation, please?
Thank you

task copyResources(type: Copy) {
    from "${projectDir}/src/main/resources"
    into "${buildDir}/classes/java/main/resources"
}
processResources.dependsOn copyResources

Maybe you should fix this jar task to be

jar {
    from 'resources', {
        into 'resources'
    }
}```