Lukas Grygar's

Personal enterprise Java web development blog.

Liferay Portlet: How to get version information from Maven's POM.xml

In case you developing Liferay 6.2 Portlet for 3rd party it’s useful to check if correct version of portlet is deployed in case you want to be sure if everything works correctly and correct version is deployed.

Directory structure of Liferay 6.2 Portlet which is build by Apache Maven (note that I’m using Eclipse):

Foobar
├── src/main/java
|    └── com.lukasgrygar.foobar
|        └── servlet
|            └── VersionServlet.java
├── src/main/resources
|	└── version.properties
├── src
|	└── main
|	    └── webapp
|	        └── web.xml
└── pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

	<!-- rest of POM ommited -->

	<version>1.0.1-SNAPSHOT</version>

	<!-- rest of POM ommited -->

	<build>

		<!-- rest of POM ommited -->

        <resources>

			<!-- rest of POM ommited -->

	        <resource>
	          <directory>src/main/resources</directory>
	          <filtering>true</filtering>
	        </resource>

			<!-- rest of POM ommited -->

        </resources> 

		<!-- rest of POM ommited -->

    </build>

    <!-- rest of POM ommited -->

</project>

version.properties

foobar.version=${project.version}

VersionServlet.java

 1 package com.lukas.grygar.foobar.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.PrintWriter;
 6 import java.util.Properties;
 7 
 8 import javax.servlet.ServletException;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 import org.apache.log4j.Logger;
14 
15 public class VersionServlet extends HttpServlet {
16 
17     private static final long serialVersionUID = 3666732306189421573L;
18 
19     private static Logger log = Logger.getLogger(VersionServlet.class);
20 
21     private Properties prop;
22 
23     public String getPortletVersion() {
24         return prop.getProperty("foobar.version");
25     }
26 
27     @Override
28     public void init() {
29 
30         prop = new Properties();
31         InputStream input = null;
32 
33         try {
34             String filename = "version.properties";
35             input = VersionServlet.class.getClassLoader().getResourceAsStream(filename);
36             if (input == null) {
37                 if (log.isDebugEnabled()) {
38                     log.debug("init():: Sorry, unable to find " + filename);
39                 }
40             }
41 
42             //load a properties file from class path, inside static method
43             prop.load(input);
44 
45         } catch (IOException ex) {
46             log.error("init():: Error during input load: " + ex);
47 
48         } finally {
49             if (input != null) {
50                 try {
51                     input.close();
52                 } catch (IOException e) {
53                     log.error("init():: Error during input close: " + e);
54                 }
55             }
56         }
57     }
58 
59     @Override
60     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
61 
62         response.setContentType("text/html;charset=UTF-8");
63         PrintWriter printWriter = response.getWriter();
64 
65         printWriter.println("<!DOCTYPE html>");
66         printWriter.println("<html>");
67         printWriter.println("<head>");
68         printWriter.println("<meta name=\"robots\" content=\"noindex, nofollow\">");
69         printWriter.println("<title>Version info</title>");
70         printWriter.println("</head>");
71         printWriter.println("<body>");
72         printWriter.println("<p>Foobar portlet version: <strong>" + getPortletVersion() + "</strong></p>");
73         printWriter.println("</body>");
74         printWriter.println("</html>");
75     }
76 }

web.xml

 1 <?xml version="1.0"?>
 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 3 
 4 	<!-- rest of configuration ommited -->
 5 
 6 	<servlet>
 7 	    <servlet-name>version</servlet-name>
 8 	    <servlet-class>com.lukas.grygar.foobar.servlet.VersionServlet</servlet-class>
 9   	</servlet>
10   	
11   	<servlet-mapping>
12 	    <servlet-name>version</servlet-name>
13 	    <url-pattern>/version</url-pattern>
14   	</servlet-mapping>
15 
16   	<!-- rest of configuration ommited -->
17 
18 </web-app>

Then version info is accessible through relative URL /Foobar-portlet/version


Tags: liferay  liferay-portal 

Share on: Twitter  Facebook  Google+ 


Previous post: Liferay Theme: How to put version information from Maven's POM.xml to header meta tag

Next post: How to uninstall (remove) Oracle Database 11g on Microsoft Windows 7

Related posts: