Class UberJarGenerator

All Implemented Interfaces:
Generator, ResourceProvider, ResourceRetriever

public class UberJarGenerator extends LibraryGenerator

A Generator for uber jars.

Depending on the request, the generator provides two types of resources.

  1. A JarFile. This type of resource is also returned if a more general ResourceType such as ClasspathElement is requested.

  2. An AppJarFile. When requesting this special jar type, the generator checks if a main class is specified.

The generator takes the following approach:

  • Request all ClasspathElements from the providers. Add the resource trees and the jar files to the sources to be processed. Ignore jar files from maven repositories (instances of MvnRepoJarFile).
  • Request all MvnRepoResources from the providers and use them for a dependency resolution. Add the jar files from the dependency resolution to the resources to be processed.
  • Add resources from the sources to the uber jar. Merge the files in META-INF/services/ that have the same name by concatenating them.
  • Filter out any other duplicate direct child files of META-INF. These files often contain information related to the origin jar that is not applicable to the uber jar.
  • Filter out any module-info.class entries.

Note that the resource type of the uber jar generator's output is one of the resource types of its inputs, because uber jars can also be used as ClasspathElement. Therefore, if you want to create an uber jar from all resources provided by a project, you must not add the generator to the project like this:

    generator(UberJarGenerator::new).add(this); // Circular dependency

This would add the project as provider and thus make the uber jar generator as supplier to the project its own provider (via Project.provide). Rather, you have to use this slightly more complicated approach to adding providers to the uber jar generator:

    generator(UberJarGenerator::new)
        .addAll(providers(EnumSet.of(Forward, Expose, Supply)));

This requests the same providers from the project as Project.provide does, but allows the uber jar generator's LibraryGenerator.from(org.jdrupes.builder.api.ResourceProvider...) method to filter out the uber jar generator itself from the providers. The given intends can vary depending on the requirements.

If you don't want the generated uber jar to be available to other generators of your project, you can also add it to a project like this:

    dependency(new UberJarGenerator(this)
        .from(providers(EnumSet.of(Forward, Expose, Supply))), Intend.Forward)

Of course, the easiest thing to do is separate the generation of class trees or library jars from the generation of the uber jar by generating the uber jar in a project of its own. Often the root project can be used for this purpose.