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.
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)
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()
}
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:
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?
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