I’ve a gradle project which has a dependency on an external library say “ext1.jar”. This external library has a folder “f1” which has some class files. When I build my gradle project I want to copy that folder “f1” from “ext1.jar” into my project’s jar.
I see that this works if I want to copy a single file in my src into jar:
jar {
from "sampleFileInSrc.txt"
}
However I want to copy something from a dependent jar. Something like this:
jar {
from library 'ext1.jar' and folder 'f1'
into "src/main/resources"
}
You can use zipTree() to work with the content of the jar rather than the jar file itself. Here is a complete example taking a jar from the classpath and packaging its content into your jar:
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.9'
}
jar {
from provider {
zipTree(configurations.compileClasspath.filter {
it.name == 'commons-lang3-3.9.jar'
}.singleFile)
}
}