TL;DR - It merges the service files in the META-INF/services folder.
Long Answer
Some libraries (e.g. Micronaut) create a few service files in the META-INF/services
folder. These files can contain any information useful at runtime. In case of Micronaut framework, it creates a file that lists the Bean References (or beans that are instantiated) in a file called io.micronaut.inject.BeanDefinitionReference
under META-INF/services
.
Usually, if you just have one application, it works fine even without mergeServiceFiles()
. But if there are two micronaut projects that you are bundling into a single jar (e.g. a micronaut lib
project with some utils and a micronaut app
project with core business logic), you will have two io.micronaut.inject.BeanDefinitionReference
. Each one will contain the beans of it's own project.
If you don't use mergeServiceFiles()
, one of the BeanDefinitionReference
files will be overwritten by the other. In that case, you will get a runtime exception saying BeanNotInstantiated
or something of that sort.
Using mergeServiceFiles()
merges (or concatenates in this case) the BeanDefinitionReference
files of both the projects so that at runtime, you get all the beans defined.
More details can be found in the gradle forum topic here.