Class JarBuilder

All Implemented Interfaces:
Generator, Renamable, ResourceProvider
Direct Known Subclasses:
JavadocJarGenerator, LibraryBuilder, SourcesJarGenerator

public class JarBuilder extends AbstractGenerator
A general purpose generator for jars. All contents must be added explicitly using one of the add* methods.
  • Constructor Details

    • JarBuilder

      public JarBuilder(Project project, ResourceType<? extends JarFile> jarType)
      Initializes a new library generator.
      Parameters:
      project - the project
      jarType - the type of jar that the generator generates
  • Method Details

    • name

      public JarBuilder name(String name)
      Description copied from interface: Renamable
      Set the new name.
      Specified by:
      name in interface Renamable
      Overrides:
      name in class AbstractGenerator
      Parameters:
      name - the name
      Returns:
      the renamable
    • destination

      public Path destination()
      Returns the destination directory. Defaults to sub directory libs in the project's build directory (see Project.buildDirectory()).
      Returns:
      the destination
    • destination

      public JarBuilder destination(Path destination)
      Sets the destination directory. The Path is resolved against the project's build directory (see Project.buildDirectory()).
      Parameters:
      destination - the new destination
      Returns:
      the jar generator
    • destination

      public JarBuilder destination(Supplier<Path> destination)
      Sets the destination directory.
      Parameters:
      destination - the new destination
      Returns:
      the jar generator
    • jarName

      public String jarName()
      Returns the name of the generated jar file. Defaults to the project's name followed by its version and .jar.
      Returns:
      the string
    • jarName

      public JarBuilder jarName(Supplier<String> jarName)
      Sets the supplier for obtaining the name of the generated jar file in ResourceProviderSpi.provide(ResourceRequest).
      Parameters:
      jarName - the jar name
      Returns:
      the jar generator
    • jarName

      public JarBuilder jarName(String jarName)
      Sets the name of the generated jar file.
      Parameters:
      jarName - the jar name
      Returns:
      the jar generator
    • addAttributeValues

      Add the given attributes to the manifest.
      Parameters:
      attributes - the attributes
      Returns:
      the library generator
    • addManifestAttributes

      Add the given attributes to the manifest.
      Parameters:
      attributes - the attributes
      Returns:
      the library generator
    • attributes

      Add the given attributes to the manifest.
      Parameters:
      attributes - the attributes
      Returns:
      the library generator
    • addEntries

      public JarBuilder addEntries(Stream<? extends Map.Entry<Path, ? extends IOResource>> entries)
      Adds single resources to the jar. Each entry is added to the jar as entry with the name passed in the key attribute of the Map.Entry with the content from the IOResource in the value attribute.
      Parameters:
      entries - the entries
      Returns:
      the jar generator
    • addTrees

      public JarBuilder addTrees(Stream<? extends FileTree<?>> trees)
      Adds the given FileTrees. Each file in the tree will be added as an entry using its relative path in the tree as name.
      Parameters:
      trees - the trees
      Returns:
      the jar generator
    • add

      public JarBuilder add(FileTree<?>... trees)
      Convenience method for adding entries, see addTrees(Stream).
      Parameters:
      trees - the trees
      Returns:
      the jar generator
    • add

      public JarBuilder add(Path prefix, FileTree<?> tree)
      Adds the file tree with the given prefix for each entry.
      Parameters:
      prefix - the prefix
      tree - the tree
      Returns:
      the jar builder
    • add

      public JarBuilder add(Path prefix, Stream<? extends FileTree<?>> trees)
      For each file tree, add its entries with the given prefix.
      Parameters:
      prefix - the prefix
      trees - the trees
      Returns:
      the jar builder
    • add

      public JarBuilder add(Path path, IOResource resource)
      Convenience method for adding a single entry, see addEntries(Stream).
      Parameters:
      path - the path
      resource - the resource
      Returns:
      the jar generator
    • buildJar

      protected void buildJar(JarFile jarResource)
      Builds the jar.
      Parameters:
      jarResource - the jar resource
    • collectContents

      protected void collectContents(Map<Path, Resources<IOResource>> contents)
      Add 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 invoke super.collectEntries(...).
      Parameters:
      contents - the preliminary contents
    • collect

      protected void collect(Map<Path, Resources<IOResource>> collected, FileTree<?> fileTree)
      Adds the resources from the given file tree to the given contents. May be used by derived classes while collecting contents for the jar.
      Parameters:
      collected - the preliminary contents
      fileTree - the file tree
    • resolveDuplicates

      protected void resolveDuplicates(Map<Path, Resources<IOResource>> entries)
      Resolve duplicates. The default implementation outputs a warning and skips the duplicate entry.
      Parameters:
      entries - the entries
    • doProvide

      protected <T extends Resource> Stream<T> doProvide(ResourceRequest<T> requested)
      Description copied from class: AbstractProvider
      Invoked by ResourceProviderSpi.provide(ResourceRequest) after checking if the invocation is allowed.
      Specified by:
      doProvide in class AbstractProvider
      Type Parameters:
      T - the generic type
      Parameters:
      requested - the requested
      Returns:
      the stream