public final class Manifests extends AbstractMap<String,String>
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"); ); } }
The only dependency you need (check the latest version at jcabi-manifests):
<dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-manifests</artifactId> </dependency>
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
Manifests()
Public ctor.
|
Manifests(Map<String,String> attrs)
Public ctor.
|
Modifier and Type | Method and Description |
---|---|
static void |
append(File file)
Append attributes from the file.
|
static void |
append(InputStream stream)
Append attributes from input stream.
|
Manifests |
append(Mfs streams)
Append this collection of MANIFEST.MF files.
|
static void |
append(ServletContext ctx)
Append attributes from the web application
MANIFEST.MF . |
Set<Map.Entry<String,String>> |
entrySet() |
static boolean |
exists(String name)
Check whether attribute exists in any of
MANIFEST.MF files. |
static String |
read(String name)
Read one attribute available in one of
MANIFEST.MF files. |
public Manifests append(Mfs streams) throws IOException
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
otherwisepublic static void append(ServletContext ctx) throws IOException
MANIFEST.MF
.
You can call this method in your own
Filter
or
ServletContextListener
,
in order to inject MANIFEST.MF
attributes to the class.
The method is thread-safe.
ctx
- Servlet contextIOException
- If some I/O problem insideManifests()
public static void append(File file) throws IOException
The method is thread-safe.
file
- The file to load attributes fromIOException
- If some I/O problem insidepublic static void append(InputStream stream) throws IOException
The method is thread-safe.
stream
- Stream to useIOException
- If some I/O problem insideCopyright © 2012–2014 jcabi.com. All rights reserved.