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 TypeMethodDescriptionvoidAppend this collection of MANIFEST.MF files.booleancontainsKey(String key) Check if attributes map contains the given key.booleancontainsValue(String value) Check if attributes map contains the given value.static booleanCheck whether attribute exists in any ofMANIFEST.MFfiles.Get attribute value by its key.getAsMap()Get a copy of attributes map.booleanisEmpty()Check if attributes map is empty.keySet()Get a copy of a set of attributes keys.static StringRead one attribute available in one ofMANIFEST.MFfiles.static MfMapGet the singleton.intsize()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:MfMapGet size of attributes map. -
isEmpty
public boolean isEmpty()Description copied from interface:MfMapCheck if attributes map is empty. -
containsKey
Description copied from interface:MfMapCheck if attributes map contains the given key.- Specified by:
containsKeyin interfaceMfMap- Parameters:
key- Attribute name- Returns:
- True if attributes map contains the given key, and false otherwise
-
containsValue
Description copied from interface:MfMapCheck if attributes map contains the given value.- Specified by:
containsValuein interfaceMfMap- Parameters:
value- Attribute value- Returns:
- True if attributes map contains the given value, and false otherwise
-
get
Description copied from interface:MfMapGet attribute value by its key. -
getAsMap
Description copied from interface:MfMapGet a copy of attributes map. -
keySet
Description copied from interface:MfMapGet a copy of a set of attributes keys. -
append
Description copied from interface:MfMapAppend this collection of MANIFEST.MF files. This method changes the original instance.- Specified by:
appendin interfaceMfMap- Parameters:
mfs- Content to append- Throws:
IOException- If fails on I/O problem
-
read
Read one attribute available in one ofMANIFEST.MFfiles.If such an attribute doesn't exist
IllegalArgumentExceptionwill 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.MFfiles.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
TRUEif it exists,FALSEotherwise
-