Class UberJarBuilder
- All Implemented Interfaces:
Generator, Renamable, ResourceProvider, ResourceRetriever
A Generator for uber jars.
Depending on the request, the generator provides two types of resources.
-
A
JarFile. This type of resource is also returned if a more generalResourceTypesuch asClasspathElementis requested. -
An
AppJarFile. When requesting this special jar type, the generator checks if a main class is specified.
The generator takes the following approach:
- Request
Resources<ClasspathElement>from the providers. Add the resource trees and the jar files to the sources to be processed. Ignore jar files from maven repositories (instances ofMvnRepoJarFile). - 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 UberJarBuilder does deliberately not request the
ClasspathElements as RuntimeResources because this may return
resources twice if a project uses another project as runtime
dependency (i.e. with Intent.Consume. If this rule causes entries
to be missing, simply add them explicitly.
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.resources). Rather, you have to use this slightly more complicated approach to adding providers to the uber jar generator:
generator(UberJarGenerator::new)
.addAll(providers().select(Forward, Expose, Supply));
This requests the same providers from the project as
Project.resources does, but allows the uber jar
generator's LibraryBuilder.addFrom(org.jdrupes.builder.api.ResourceProvider...) method to filter out the uber jar
generator itself from the providers. The given intents 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))), Intent.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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcollectFromProviders(Map<Path, Resources<IOResource>> contents) Collects the contents from the providers.doProvide(ResourceRequest<T> requested) Invoked byResourceProviderSpi.provide(ResourceRequest)after checking if the invocation is allowed.ignoreDuplicates(String... patterns) Ignore duplicates matching the given glob patterns when merging.Set the new name.protected voidresolveDuplicates(Map<Path, Resources<IOResource>> entries) Resolve duplicates.resourceFilter(Predicate<Resource> filter) Apply the given filter to the resources obtained from the provider.Methods inherited from class LibraryBuilder
addFrom, addFrom, collectContents, mainClass, mainClass, providersMethods inherited from class JarBuilder
add, add, add, add, addAttributeValues, addEntries, addManifestAttributes, addTrees, attributes, buildJar, collect, destination, destination, destination, jarName, jarName, jarNameMethods inherited from class AbstractGenerator
cleanup, newResource, project, toStringMethods inherited from class AbstractProvider
context, name, of, rename, vavrStream
-
Constructor Details
-
UberJarBuilder
Instantiates a new uber jar generator.- Parameters:
project- the project
-
-
Method Details
-
name
Description copied from interface:RenamableSet the new name.- Specified by:
namein interfaceRenamable- Overrides:
namein classLibraryBuilder- Parameters:
name- the name- Returns:
- the renamable
-
ignoreDuplicates
Ignore duplicates matching the given glob patterns when merging.- Parameters:
patterns- the patterns- Returns:
- the uber jar builder
-
collectFromProviders
Description copied from class:LibraryBuilderCollects the contents from the providers. This implementation requestsClassTrees andJavaResourceTrees.- Overrides:
collectFromProvidersin classLibraryBuilder- Parameters:
contents- the contents
-
resourceFilter
Apply the given filter to the resources obtained from the provider. The resources can beClasspathElements orMvnRepoResources. This may be required to avoid warnings about duplicates if e.g. a sub-project provides generated resources both asClassTree/JavaResourceTreeand asLibraryJarFile.- Parameters:
filter- the filter. Returnstruefor resources to be included.- Returns:
- the uber jar generator
-
resolveDuplicates
Description copied from class:JarBuilderResolve duplicates. The default implementation outputs a warning and skips the duplicate entry.- Overrides:
resolveDuplicatesin classJarBuilder- Parameters:
entries- the entries
-
doProvide
Description copied from class:AbstractProviderInvoked byResourceProviderSpi.provide(ResourceRequest)after checking if the invocation is allowed.- Overrides:
doProvidein classLibraryBuilder- Type Parameters:
T- the generic type- Parameters:
requested- the requested- Returns:
- the stream
-