I have migrated a Java project to use Gradle, and it works just fine in IntelliJ or using the command line.
However when I package it into a standalone JAR using FatJar, I have some issues accessing the ressources of my project.
Here is the current code. It recursively lists the .sql
files from the queries
folder, and for each file it puts it in a Map.
private static void loadQueriesFrom(final String baseDir) throws IOException {
final InputStream is = Queries.class.getResourceAsStream(baseDir);
if (is == null)
return;
List<String> queryFiles = IOUtils.readLines(is, Charsets.UTF_8);
for (String queryFile: queryFiles) {
final int index = queryFile.lastIndexOf(".sql");
if (index == -1) {
loadQueriesFrom(baseDir + queryFile + "/"); // directory
}
else {
final String queryName = queryFile.substring(0, index).toLowerCase();
queries.put(baseDir.substring(QUERY_DIRECTORY.length()) + queryName, IOUtils.toString(Queries.class
.getResourceAsStream(baseDir + queryFile)));
}
}
}
// First call is
loadQueriesFrom("/queries");
My directory structure is as follows:
- src
- main
- java
- resources
- queries
- test
This piece of code works fine except when it is packaged in a JAR: in this case, is (line 1) is always null. Here's my fatjar configuration:
task fatjar(type: Jar) {
baseName = project.name
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
manifest {
attributes 'Implementation-Title': 'My project name', 'Implementation-Version': version
attributes 'Main-Class': 'org.mypackage.MyMainClass'
}
}
If I open the .jar generated file, the structure looks like
- org
- mypackage
...
- queries
So the queries folder is included in the JAR but for some reason getResourceAsStream can't list it. However, it can access individual files. If I insert the following line, I get the intended result.
Queries.class.getResourceAsStream("/queries/collaborators/get_collab_list.sql")
What I've tried:
changing
/queries
toqueries
changing
getResourceAsStream(...)
bygetClassLoader().getResourceAsStream(...)
both
I've seen this answer, but it looks a bit overkill.
Help appreciated. Thanks!