Tested on Java LTS versions from 8 to 21.
Tested on Gradle versions from 6.0 to 8.12.
Usage:
plugins {
id 'name.remal.merge-resources' version '4.0.4'
}
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`
}
log4j2PluginsMerger {
enabled = false // To disable merging `META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat`
}
// To merge all `*.jar` files with the same relative path:
addResourceMerger('**/*.jar') { RelativePath relativePath, Collection<Path> paths, OutputStream outputStream ->
outputStream.write(new byte[0])
}
// To merge all `*.text` files with the same relative path:
addTextResourceMerger('**/*.jar', 'US-ASCII') { RelativePath relativePath, Collection<Path> paths, PrintWriter writer ->
writer.println('test')
}
addTextResourceMerger('**/*.jar' /* UTF-8 is default charset */) { RelativePath relativePath, Collection<Path> paths, PrintWriter writer ->
writer.println('test')
}
}
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 biggest number of package annotations is taken.
For the other files, there is a check that validates 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
.
See documentation about Log4j plugin descriptor.
Path
is used for mergers instead of File
.
Resource merges do not return InputStream
anymore.
OutputStream
is provided as a parameter instead for mergeResources.addResourceMerger()
.
PrintWriter
is provided as a parameter instead for mergeResources.addTextResourceMerger()
.
The plugin was fully rewritten. There were no intentions to keep backward compatibility.