Class UberJarBuilder
- All Implemented Interfaces:
Generator, Renamable, ResourceProvider, ResourceRetriever
A Generator for uber jars.
Depending on the request, the generator provides one of two resource types.
-
A
JarFile. This type of resource is also returned if a more generalResourceTypesuch asClasspathElementis requested. -
An
AppJarFile. When this specialJarFiletype is requested, the generator requires a main class to be configured.
The generator takes the following approach:
- Request resources of type
ClasspathElementwith all intents from the added providers. Add the class and resource trees to the sources to be processed. JAR files are handled differently depending on their origin. The content of JAR files that are not retrieved from a Maven repository is added to the sources to be processed. ForMvnRepoJarFiles (i.e. JAR files from a Maven repository) only theMvnRepoResourcereference is collected. - Use all
MvnRepoResources obtained in the previous step for a dependency resolution. Add the content from the resulting JAR files to the sources 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 files under
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.
The resource type of the uber jar builder's output is one
of the resource types of its inputs, because uber jars can also be used
as ClasspathElement. Therefore, you cannot add a uber jar builder
to the project like this:
generator(UberJarBuilder::new).addFrom(this); // Circular dependency
This would add the project as provider and thus make the uber jar builder's result a uber jar builder's source (via Project.resources). Instead use the following approach:
generator(UberJarGenerator::new)
.addFrom(providers().select(Forward, Expose, Supply));
This requests the same providers from the project as
Project.resources would, but allows the uber jar
builder's LibraryBuilder.addFrom(org.jdrupes.builder.api.ResourceProvider...) method to filter out the uber jar
builder itself from the providers. The given intents can
vary depending on the requirements.
If the generated uber jar should not be visible to the project's other generators, you can also add it like this:
dependency(new UberJarGenerator(this).addFrom(
providers(EnumSet.of(Forward, Expose, Supply))), Intent.Forward)
In most cases, the simplest solution is to generate the uber jar in a separate project, typically the parent project. This cleanly separates the generation of class and resource trees and library jars from the generation of the uber jar.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcollectFromProviders(Map<Path, Resources<InputResource>> contents) Collects the contents from the providers.protected <T extends Resource>
Collection<T> doProvide(ResourceRequest<T> request) 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<InputResource>> 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, contentProviders, mainClass, mainClassMethods 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, project, toStringMethods inherited from class AbstractProvider
context, name, of, rename, vavrStreamMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ResourceProvider
context, name, of, resources
-
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:
request- the request for resources- Returns:
- the stream
-