Class Manifests

  • All Implemented Interfaces:
    MfMap

    public final class Manifests
    extends Object
    implements MfMap
    Static reader of META-INF/MANIFEST.MF files. The class provides convenient methods to read all MANIFEST.MF files available in classpath and all attributes from them.

    This mechanism may be very useful for transferring information from continuous integration environment to the production environment. For example, you want your site to show project version and the date of WAR file packaging. First, you configure maven-war-plugin to add this information to MANIFEST.MF:

     <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <configuration>
       <archive>
        <manifestEntries>
         <Foo-Version>${project.version}</Foo-Version>
         <Foo-Date>${maven.build.timestamp}</Foo-Date>
        </manifestEntries>
       </archive>
      </configuration>
     </plugin>

    maven-war-plugin will add these attributes to your MANIFEST.MF file and the project will be deployed to the production environment. Then, you can read these attributes where it's necessary (in one of your JAXB annotated objects, for example) and show to users:

      import com.jcabi.manifests.Manifest;
     import java.text.SimpleDateFormat;
     import java.util.Date;
     import java.util.Locale;
     import jakarta.xml.bind.annotation.XmlElement;
     import jakarta.xml.bind.annotation.XmlRootElement;
     @XmlRootElement
     public final class Page {
       @XmlElement
       public String version() {
         return Manifests.read("Foo-Version");
       }
       @XmlElement
       public Date date() {
         return new SimpleDateFormat("yyyy.MM.dd", Locale.ENGLISH).parse(
           Manifests.read("Foo-Date");
         );
       }
     }

    If you want to add more manifests to the collection, use its static instance:

    Manifests.singleton().append(new FilesMfs(new File("MANIFEST.MF")));

    You can also modify the map directly:

    Manifests.singleton().put("Hello", "world");

    The only dependency you need (check the latest version at jcabi-manifests):

     <dependency>
      <groupId>com.jcabi</groupId>
      <artifactId>jcabi-manifests</artifactId>
     </dependency>
    Since:
    0.7
    • Constructor Detail

      • Manifests

        public Manifests()
        Public ctor.
        Since:
        1.0
      • Manifests

        public Manifests​(Map<String,​String> attrs)
        Public ctor.
        Parameters:
        attrs - Attributes to encapsulate
        Since:
        1.0
    • Method Detail

      • singleton

        public static MfMap singleton()
        Get the singleton.
        Returns:
        The singleton
        Since:
        2.0.0
      • size

        public int size()
        Description copied from interface: MfMap
        Get size of attributes map.
        Specified by:
        size in interface MfMap
        Returns:
        Size of attributes map
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: MfMap
        Check if attributes map is empty.
        Specified by:
        isEmpty in interface MfMap
        Returns:
        True if attributes map is empty and false otherwise
      • containsKey

        public boolean containsKey​(String key)
        Description copied from interface: MfMap
        Check if attributes map contains the given key.
        Specified by:
        containsKey in interface MfMap
        Parameters:
        key - Attribute name
        Returns:
        True if attributes map contains the given key and false otherwise
      • containsValue

        public boolean containsValue​(String value)
        Description copied from interface: MfMap
        Check if attributes map contains the given value.
        Specified by:
        containsValue in interface MfMap
        Parameters:
        value - Attribute value
        Returns:
        True if attributes map contains the given value and false otherwise
      • get

        public String get​(String key)
        Description copied from interface: MfMap
        Get attribute value by its key.
        Specified by:
        get in interface MfMap
        Parameters:
        key - Attribute name
        Returns:
        Value of the attribute and null if attribute not found
      • getAsMap

        public Map<String,​String> getAsMap()
        Description copied from interface: MfMap
        Get a copy of attributes map.
        Specified by:
        getAsMap in interface MfMap
        Returns:
        Copy of attributes map
      • keySet

        public Set<String> keySet()
        Description copied from interface: MfMap
        Get a copy of a set of attributes keys.
        Specified by:
        keySet in interface MfMap
        Returns:
        Copy of a set of attributes keys
      • append

        public void append​(Mfs mfs)
                    throws IOException
        Description copied from interface: MfMap
        Append this collection of MANIFEST.MF files. This method changes the original instance.
        Specified by:
        append in interface MfMap
        Parameters:
        mfs - Content to append
        Throws:
        IOException - If fails on I/O problem
      • read

        public static String read​(String name)
        Read one attribute available in one of MANIFEST.MF files.

        If such a attribute doesn't exist IllegalArgumentException will be thrown. If you're not sure whether the attribute is present or not use exists(String) beforehand.

        The method is thread-safe.

        Parameters:
        name - Name of the attribute
        Returns:
        The value of the attribute retrieved
      • exists

        public static boolean exists​(String name)
        Check whether attribute exists in any of MANIFEST.MF files.

        Use this method before read(String) to check whether an attribute exists, in order to avoid a runtime exception.

        The method is thread-safe.

        Parameters:
        name - Name of the attribute to check
        Returns:
        Returns TRUE if it exists, FALSE otherwise