In this small post, I will expose your simple useful manners to get the version from MANIFEST.MF and display it in an web application.
1st case: loading of the META-INF file contained in a custom library
- Create (or check the presence of) the MANIFEST.MF file in the META-INF folder of your custom library like MyLib\src\META-INF\MANIFEST.MF.
The content of this file could be:1
Manifest-Version: 1.0
2
Class-Path:
3
Implementation-Title: MyApplication
4
Implementation-Version: 1.2.3
5
Implementation-Vendor: JavaBlog.fr
- Create a class named Version with a static attribute in your library MyLib\src\main\java\com\ho\mylib\util\Version.java:
1
public
class
Version {
2
public
final
static
String CURRENT_VERSION = Version.
class
.getPackage().getImplementationVersion();
3
}
- And in your web application, the JSP displaying the library version could be like:
1
<%@ page import=
"com.ho.mylib.util.Version"
%>
2
<h2 class=
"x-panel-header"
><%=Version.CURRENT_VERSION%></h2>
2nd case: loading of the META-INF file contained in a classic web application
- Create (or check the presence of) the MANIFEST.MF file in the META-INF folder of your web application like MyApplication\WebContent\META-INF\MANIFEST.MF or MyApplication\war\META-INF\MANIFEST.MF.
The content of this file could be:1
Manifest-Version: 1.0
2
Class-Path:
3
Implementation-Title: MyApplication
4
Implementation-Version: 1.2.3
5
Implementation-Vendor: JavaBlog.fr
- And in your web application, the JSP displaying the library version could be like:
1
<%
2
//Get version of application
3
java.util.Properties prop =
new
java.util.Properties();
4
prop.load(getServletContext().getResourceAsStream(
"/META-INF/MANIFEST.MF"
));
5
String applVersion = prop.getProperty(
"Implementation-Version"
);
6
%>
7
<h2 class=
"x-panel-header"
><%=applVersion%></h2>
3rd case: loading of the META-INF file contained in a Spring web application
- Create (or check the presence of) the MANIFEST.MF file in the META-INF folder of your web application like MyApplication\WebContent\META-INF\MANIFEST.MF or MyApplication\war\META-INF\MANIFEST.MF.
The content of this file could be:1
Manifest-Version: 1.0
2
Class-Path:
3
Implementation-Title: MyApplication
4
Implementation-Version: 1.2.3
5
Implementation-Vendor: JavaBlog.fr
- Create a singleton class ApplicationVersion which will contain the version of application from “/META-INF/MANIFEST.MF” file:
01
package
com.ho.spring3.security.test1.util;
02
03
import
java.util.Iterator;
04
import
java.util.Properties;
05
06
import
javax.servlet.ServletContext;
07
08
import
org.springframework.web.context.ServletContextAware;
09
10
/**
11
* Bean containing the version of application from "/META-INF/MANIFEST.MF" file.
12
* @author huseyin
13
*
14
*/
15
public
class
ApplicationVersion
implements
ServletContextAware{
16
17
// ----------------------------------- PRIVATE ATTRIBUTES
18
private
static
String CURRENT_VERSION =
null
;
19
private
static
ApplicationVersion instance =
null
;
20
private
static
ServletContext servletContext =
null
;
21
22
// ------------------------------------- PUBLIC FUNCTIONS
23
private
ApplicationVersion(){}
24
25
public
static
synchronized
String getVersion(){
26
if
(instance ==
null
){
27
try
{
28
instance =
new
ApplicationVersion();
29
String name =
"/META-INF/MANIFEST.MF"
;
30
Properties props =
new
Properties();
31
props.load(servletContext.getResourceAsStream(name));
32
33
/*for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) {
34
Object key = iterator.next();
35
Object value = props.get(key);
36
System.out.println(key+"="+value);
37
}*/
38
39
instance.CURRENT_VERSION = (String) props.get(
"Implementation-Version"
);
40
}
catch
(Throwable e) {
41
e.printStackTrace();
42
}
43
}
44
45
return
instance.CURRENT_VERSION;
46
}
47
48
public
void
setServletContext(ServletContext servletContext){
49
this
.servletContext = servletContext;
50
}
51
}
- Modify the Spring context to add the newly created bean:
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04
xmlns:tx
=
"http://www.springframework.org/schema/tx"
05
xmlns:context
=
"http://www.springframework.org/schema/context"
06
xsi:schemaLocation="
13
">
14
15
<
bean
name
=
"applicationVersion"
class
=
"com.ho.spring3.security.test1.util.ApplicationVersion"
/>
16
17
</
beans
>
- And in your web application, the JSP displaying the library version could be like:
1
<%@ page import=
"com.ho.spring3.security.test1.util.ApplicationVersion"
%>
2
3
<%=ApplicationVersion.getVersion() %>
Note: The MANIFEST.MF could be generated or updated manually or via MAVEN, ANT.
That’s all!!!
Best regards,
Huseyin OZVEREN
I went with your 3rd case: loading of the META-INF file contained in a Spring web application and it worked like a charm. Thanks for the examples.
-Jeff