Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j.properties gets overwritten when creating a "fat-JAR" using Gradle

One of my Java projects exports an executable "fat-JAR" file ready for deployment. For creating the JAR file using Gradle, I followed the instructions described here:

task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Title': '<HUMAN_READABLE_TITLE_OF_YOUR_PACKAGE>',  
            'Implementation-Version': version,
            'Main-Class': '<PATH_TO_THE_MAIN_APPLICATION_CLASS>'
    }

    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

The task illustrated will recursively go through dependency tree, unzip JARs if needed, and squash everything together. The only problem so far is that some of my dependencies come with their own log4.properties files, which eventually overwrite the one I have written myself. All of them reside within the same level into their respective resources folders, so when files get merged together, the later ones seem to overwrite the ones that have been added before.

So far, the only solution I have found, is to manually set the path to the right file, using a command-line parameter. I would eventually like to be able to execute the JAR itself, without additional overhead.

What would be a good way to keep my log4.properties and exclude the others from being added to the package?

like image 899
Preslav Rachev Avatar asked Nov 13 '16 07:11

Preslav Rachev


1 Answers

You can exlude the file from the collection of files to be merged into the build JAR.

from {
  configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
} {
  exclude "dir/subdir/the_file_to.exclude".
}
like image 81
SubOptimal Avatar answered Oct 31 '22 22:10

SubOptimal