Class LibraryBuilder
- All Implemented Interfaces:
Generator, Renamable, ResourceProvider, ResourceRetriever
- Direct Known Subclasses:
UberJarBuilder
A Generator for Java libraries packaged as jars. A library jar
is expected to contain class files and supporting resources together
with additional information in META-INF/.
The generator provides two types of resources.
-
A
LibraryJarFile. This type of resource is also returned if a more generalResourceTypesuch asCodeContributionis requested. -
An
AppJarFile. When requesting this special jar type, the generator checks if a main class is specified.
In addition to explicitly adding resources, this generator supports
resource retrieval from added providers. The resources of type ClassTree
and JavaResourceTree that the providers added with
ResourceRetriever.addFrom(ResourceProvider...) supply
are included in the library in addition to the explicitly added resources.
The enables the simple standard pattern for creating a library:
generator(LibraryBuilder::new).addFrom(this);
JPMS support
When the compiled classes contain a module-info.class, the JAR is
a named module. If no module-info.class is present but moduleName
is set (or auto-detected from the project name), an Automatic-Module-Name
manifest attribute is added so the JAR is treated as an automatic module
on the module-path.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddFrom(Stream<ResourceProvider> providers) Adds the given providers as sources for resources.addFrom(ResourceProvider... providers) Adds the given provider(s) as source for resources.protected voidcollectContents(Map<Path, Resources<InputResource>> contents) Add the contents from the added streams as preliminary jar entries.protected voidcollectFromProviders(Map<Path, Resources<InputResource>> contents) Collects the contents from the providers.protected StreamCollector<ResourceProvider> return the cached providers.protected <T extends Resource>
Collection<T> doProvide(ResourceRequest<T> request) Invoked byResourceProviderSpi.provide(ResourceRequest)after checking if the invocation is allowed.Returns the main class.Sets the main class.Returns the module name for theAutomatic-Module-Namemanifest attribute.moduleName(String moduleName) Sets the module name used for theAutomatic-Module-Namemanifest attribute.Set the new name.Methods inherited from class JarBuilder
add, add, add, add, addAttributeValues, addEntries, addManifestAttributes, addTrees, attributes, buildJar, collect, destination, destination, destination, jarName, jarName, jarName, resolveDuplicatesMethods 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
-
LibraryBuilder
Instantiates a new library generator.- Parameters:
project- the project
-
-
Method Details
-
name
Description copied from interface:RenamableSet the new name.- Specified by:
namein interfaceRenamable- Overrides:
namein classJarBuilder- Parameters:
name- the name- Returns:
- the renamable
-
mainClass
-
mainClass
Sets the main class.- Parameters:
mainClass- the new main class- Returns:
- the library builder for method chaining
-
moduleName
Returns the module name for theAutomatic-Module-Namemanifest attribute. If not set, the project name is sanitized for JPMS requirements.- Returns:
- the module name
-
moduleName
Sets the module name used for theAutomatic-Module-Namemanifest attribute. This allows the JAR to be used as an automatic module on the module-path.- Parameters:
moduleName- the module name- Returns:
- this builder
-
addFrom
Description copied from interface:ResourceRetrieverAdds the given provider(s) as source for resources.- Specified by:
addFromin interfaceResourceRetriever- Parameters:
providers- the provider to add- Returns:
- the resource retriever
-
addFrom
Description copied from interface:ResourceRetrieverAdds the given providers as sources for resources. The stream must not be consumed before the generators' provide method is invoked.- Specified by:
addFromin interfaceResourceRetriever- Parameters:
providers- the providers to retrieve resources from- Returns:
- the resource retriever
-
contentProviders
return the cached providers.- Returns:
- the content providers
-
collectContents
Description copied from class:JarBuilderAdd the contents from the added streams as preliminary jar entries. Must be overridden by derived classes that define additional ways to provide contents. The overriding method must invokesuper.collectContents(...).- Overrides:
collectContentsin classJarBuilder- Parameters:
contents- the preliminary contents
-
collectFromProviders
Collects the contents from the providers. This implementation requestsClassTrees andJavaResourceTrees.- Parameters:
contents- the contents
-
doProvide
Description copied from class:AbstractProviderInvoked byResourceProviderSpi.provide(ResourceRequest)after checking if the invocation is allowed.- Overrides:
doProvidein classJarBuilder- Type Parameters:
T- the generic type- Parameters:
request- the request for resources- Returns:
- the stream
-