Exclude resource file from jar file


(Mike Rodent) #1

I’ve seen these and read through them…

… they don’t seem to answer my question.

I have a “production” logback configuration file logback.xml under src/main/resources… but that directory also contains the “testing” logback configuration file logback-test.xml (which logback looks for first).

When creating an executable jar I want to delete the “testing” xml file.

I tried this

jar {
	manifest {
        // PS this is the correct line for Shadow Plugin...
		attributes 'Class-Path': '/libs/a.jar'
		attributes 'Main-Class': 'core.MyMainClass'
	}
	exclude("**/resources/*test*")
}

and I tried this

jar {
	manifest {
		attributes 'Class-Path': '/libs/a.jar'
		attributes 'Main-Class': 'core.MyMainClass'
	}
	doLast {
		exclude("**/resources/*test*")
	}
}

… what am I doing wrong?

I find [here][1] that I was probably making life difficult for myself in putting these xmls under /src/main/resources … so I created a new directory under src, /logback, and put the files in there instead. I added this to the classpath (as logback says that’s where it looks for these files) by doing this:

test {
	classpath += files( 'src/logback' )
}

Interestingly, as well as meaning that logging during testing happens OK, this is enough to get the resulting executable jar to use logback OK when run.

Unfortunately, configuring the “shadowJar” task like this

shadowJar {
	 baseName = 'DocumentIndexer'
	 classifier = null
	 version = project.version
     exclude("logback/*test*")
}

or configuring “jar” task like this:

jar {
	manifest {
		attributes 'Class-Path': '/libs/a.jar'
		attributes 'Main-Class': 'core.ConsoleIOHandler'
	}
	exclude("logback/*test*")
}

… just refuses to work: the file logback-test.xml is still there in the jar.
[1]: How to exclude src/main/resources files from the final WAR?


(uklance) #2

First and foremost, test resources should go in src/test/resources then they won’t go in the jar.

Secondly, I think you need to remove the “resources” section from your exclude as this is the root folder, include / exclude patterns are relative to the root(s)


(Mike Rodent) #3

Thanks so much for answering so fast!