Class Manifests
- All Implemented Interfaces:
MfMap
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Append this collection of MANIFEST.MF files.boolean
containsKey
(String key) Check if attributes map contains the given key.boolean
containsValue
(String value) Check if attributes map contains the given value.static boolean
Check whether attribute exists in any ofMANIFEST.MF
files.Get attribute value by its key.getAsMap()
Get a copy of attributes map.boolean
isEmpty()
Check if attributes map is empty.keySet()
Get a copy of a set of attributes keys.static String
Read one attribute available in one ofMANIFEST.MF
files.static MfMap
Get the singleton.int
size()
Get size of attributes map.
-
Constructor Details
-
Manifests
public Manifests()Public ctor.- Since:
- 1.0
-
Manifests
Public ctor.- Parameters:
attrs
- Attributes to encapsulate- Since:
- 1.0
-
-
Method Details
-
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. -
isEmpty
public boolean isEmpty()Description copied from interface:MfMap
Check if attributes map is empty. -
containsKey
Description copied from interface:MfMap
Check if attributes map contains the given key.- Specified by:
containsKey
in interfaceMfMap
- Parameters:
key
- Attribute name- Returns:
- True if attributes map contains the given key, and false otherwise
-
containsValue
Description copied from interface:MfMap
Check if attributes map contains the given value.- Specified by:
containsValue
in interfaceMfMap
- Parameters:
value
- Attribute value- Returns:
- True if attributes map contains the given value, and false otherwise
-
get
Description copied from interface:MfMap
Get attribute value by its key. -
getAsMap
Description copied from interface:MfMap
Get a copy of attributes map. -
keySet
Description copied from interface:MfMap
Get a copy of a set of attributes keys. -
append
Description copied from interface:MfMap
Append this collection of MANIFEST.MF files. This method changes the original instance.- Specified by:
append
in interfaceMfMap
- Parameters:
mfs
- Content to append- Throws:
IOException
- If fails on I/O problem
-
read
Read one attribute available in one ofMANIFEST.MF
files.If such an attribute doesn't exist
IllegalArgumentException
will be thrown. If you're not sure whether the attribute is present or not useexists(String)
beforehand.The method is thread-safe.
- Parameters:
name
- Name of the attribute- Returns:
- The value of the attribute retrieved
-
exists
Check whether attribute exists in any ofMANIFEST.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
-