Skip to content

A Gradle plugin that configures all AbstractCopyTask tasks to merge files with the same relative path

License

Notifications You must be signed in to change notification settings

remal-gradle-plugins/merge-resources

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tested on Java LTS versions from 11 to 21.

Tested on Gradle versions from 7.0 to 8.13.

name.remal.merge-resources plugin

configuration cache: supported from v2

Usage:

plugins {
    id 'name.remal.merge-resources' version '5.0.1'
}

 

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

Configuration

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')
  }
}

Built-in mergers

META-INF/services/*

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.

**/package-info.class

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.

**/module-info.class

Different sections of module-info are merged.

It's required that all merged files have the same module name.

META-INF/spring.factories

See org.springframework.core.io.support.SpringFactoriesLoader.

META-INF/spring/*.imports

See META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports files.

See org.springframework.boot.context.annotation.ImportCandidates.

META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat

See documentation about Log4j plugin descriptor.

Migration guide

Version 4.* to 5.*

The minimum Java version is 11 (from 8). The minimum Gradle version is 7.0 (from 6.0).

Version 3.* to 4.*

Path is used for mergers instead of File.

Version 2.* to 3.*

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().

Version 1.* to 2.*

The plugin was fully rewritten. There were no intentions to keep backward compatibility.