How do I specify the resources folder in a Jar?


(John Mercier) #1

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.


(Lance Java) #2

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)


(Schalk Cronjé) #3

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

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

(John Mercier) #4

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.


(John Mercier) #5

Hey this worked great. Thanks for the tip!