Class Manifests
- java.lang.Object
-
- com.jcabi.manifests.Manifests
-
- All Implemented Interfaces:
MfMap
public final class Manifests extends Object implements MfMap
Static reader ofMETA-INF/MANIFEST.MF
files. The class provides convenient methods to read allMANIFEST.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 configuremaven-war-plugin
to add this information toMANIFEST.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 yourMANIFEST.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
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(Mfs mfs)
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
exists(String name)
Check whether attribute exists in any ofMANIFEST.MF
files.String
get(String key)
Get attribute value by its key.Map<String,String>
getAsMap()
Get a copy of attributes map.boolean
isEmpty()
Check if attributes map is empty.Set<String>
keySet()
Get a copy of a set of attributes keys.static String
read(String name)
Read one attribute available in one ofMANIFEST.MF
files.static MfMap
singleton()
Get the singleton.int
size()
Get size of attributes map.
-
-
-
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.
-
isEmpty
public boolean isEmpty()
Description copied from interface:MfMap
Check if attributes map is empty.
-
containsKey
public boolean containsKey(String key)
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
public boolean containsValue(String value)
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
public String get(String key)
Description copied from interface:MfMap
Get attribute value by its key.
-
getAsMap
public Map<String,String> getAsMap()
Description copied from interface:MfMap
Get a copy of attributes map.
-
keySet
public Set<String> keySet()
Description copied from interface:MfMap
Get a 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 interfaceMfMap
- 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 ofMANIFEST.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 useexists(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 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
-
-