public final class Manifests extends Object implements 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 javax.xml.bind.annotation.XmlElement; import javax.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.DEFAULT.append(new FilesMfs(new File("MANIFEST.MF")));
You can also modify the map directly:
Manifests.DEFAULT.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>
Modifier and Type | Field and Description |
---|---|
static MfMap |
DEFAULT
Default singleton.
|
Constructor and Description |
---|
Manifests()
Public ctor.
|
Manifests(Map<String,String> attrs)
Public ctor.
|
Modifier and Type | Method and Description |
---|---|
static void |
append(File file)
Deprecated.
Use
append(Mfs) and FilesMfs instead |
static void |
append(InputStream stream)
Deprecated.
Use
append(Mfs) and StreamsMfs instead |
MfMap |
append(Mfs streams)
Append this collection of MANIFEST.MF files.
|
static void |
append(ServletContext ctx)
Deprecated.
Use
append(Mfs) and ServletMfs instead |
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<String,String>> |
entrySet() |
static boolean |
exists(String name)
Check whether attribute exists in any of
MANIFEST.MF files. |
String |
get(Object key) |
boolean |
isEmpty() |
Set<String> |
keySet() |
String |
put(String key,
String value) |
void |
putAll(Map<? extends String,? extends String> attrs) |
static String |
read(String name)
Read one attribute available in one of
MANIFEST.MF files. |
String |
remove(Object key) |
int |
size() |
Collection<String> |
values() |
public static final MfMap DEFAULT
public boolean containsKey(Object key)
containsKey
in interface Map<String,String>
public boolean containsValue(Object value)
containsValue
in interface Map<String,String>
public MfMap append(Mfs streams) throws IOException
MfMap
append
in interface MfMap
streams
- Files to appendIOException
- If fails on I/O problempublic static String read(String name)
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.
name
- Name of the attributepublic static boolean exists(String name)
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.
name
- Name of the attribute to checkTRUE
if it exists, FALSE
otherwise@Deprecated public static void append(ServletContext ctx) throws IOException
MANIFEST.MF
.
The method is deprecated. Instead, use this code:
Manifests.DEFAULT.append(new ServletMfs());
ctx
- Servlet contextIOException
- If some I/O problem insideManifests()
@Deprecated public static void append(File file) throws IOException
The method is deprecated. Instead, use this code:
Manifests.DEFAULT.append(new FilesMfs(file));
file
- The file to load attributes fromIOException
- If some I/O problem inside@Deprecated public static void append(InputStream stream) throws IOException
The method is deprecated. Instead, use this code:
Manifests.DEFAULT.append(new StreamsMfs(stream));
stream
- Stream to useIOException
- If some I/O problem insideCopyright © 2012–2014 jcabi.com. All rights reserved.