Tested on Java LTS versions from 8 to 21.
Tested on Gradle versions from 6.0 to 8.11-rc-3.
This plugin configures all AbstractCopyTask
tasks to merge files with the same RelativePath
.
The main use cases are:
- building fat JARs
- generating some files via annotation processors and configuring the same files manually
mergeResources {
metaInfServices {
enabled = false // To disable merging `META-INF/services/*`
}
packageInfo {
enabled = false // To disable merging `META-INF/services/*`
}
metaInfServices {
enabled = false // To disable merging `**/package-info.class`
}
moduleInfo {
enabled = false // To disable merging `**/module-info.class`
}
springFactories {
enabled = false // To disable merging `META-INF/spring.factories`
}
springImports {
enabled = false // To disable merging `META-INF/spring/*.imports`
}
// To merge all `*.jar` files with the same relative path:
addResourceMerger('**/*.jar') { RelativePath relativePath, Collection<File> files ->
return new ByteArrayInputStream(new byte[0]) // `InputStream` should be returned `addResourceMerger`
}
// To merge all `*.text` files with the same relative path:
addTextResourceMerger('**/*.jar', 'US-ASCII') { RelativePath relativePath, Collection<File> files ->
return '' // `String` should be returned for `addTextResourceMerger`
}
addTextResourceMerger('**/*.jar' /* UTF-8 is default charset */) { RelativePath relativePath, Collection<File> files ->
return '' // `String` should be returned for `addTextResourceMerger`
}
}
Excluding:
META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
These files are used for Java's java.util.ServiceLoader
.
When these files are merged, all comments and duplications are removed.
The file with the most number of package annotations is taken.
For the other files, there is a check that validates that their package annotations present in the result file.
Different sections of module-info
are merged.
It's required that all merged files have the same module name.
See org.springframework.core.io.support.SpringFactoriesLoader
.
See META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
files.
See org.springframework.boot.context.annotation.ImportCandidates
.