giovedì 19 aprile 2012

Java Web Gestione Versioning

 
Lo scopo di questo post è mostrare una soluzione di aggiornamento versione sw Web, automatizzando la creazione della versione con uno script di ant e quindi mostrando sulle pagine della Web App la versione a cui si riferiscono.

L’effetto finale sarà di questo tipo:




I valori della versione sono presi dal file MANIFEST.MF presente dentro la directory WebContent/META-INF del progetto.  Il MANIFEST  è così definito:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_29-b11 (Sun Microsystems Inc.)
Company: NOME AZIENDA
Author: Mario Rossi
Implementation-Title: PROGETTO TEST
Implementation-Version: 1.0.1
Implementation-Build: 1
Build-time: 19/04/2012 01:58:59


La lettura di questi valori avviene all’interno di una servlet denominata InitServlet  ( utilizzando la classe Manifest definita nel package java.util.jar ) e posta come caricamento allo start up dell’applicazione (parametro <load-on-startup>0</load-on-startup> definito sul web.xml).

Il metodo init della servlet si presenta in questo modo:

public void init(ServletConfig servletConfig) throws ServletException {
      try
      {
      ServletContext servletCtx = servletConfig.getServletContext();
      String contextRoot = servletCtx.getRealPath("");
      String fManifest = contextRoot+File.separator+"META-INF"+File.separator+"MANIFEST.MF";
      InputStream stream =new FileInputStream(fManifest);
      Manifest manifest = new Manifest( stream);
      Attributes attr = manifest.getMainAttributes();
      String SOFTWARE_VERSION = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
      servletCtx.setAttribute( "IMPLEMENTATION_VERSION", SOFTWARE_VERSION);
      String BUIL_TIME = attr.getValue("Build-time");
      servletCtx.setAttribute( "BUILT_TIME", BUIL_TIME);
      }
      catch(Exception ex){
            ex.printStackTrace();
      }
    }

A questo punto definiamo una jsp che si occupi di recuperare i valori salvati nel contesto dell’applicazione e di mostrarli a video.

Di seguito il codice:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
      String webVersion = (String)getServletConfig().getServletContext().getAttribute("IMPLEMENTATION_VERSION");

      if(webVersion == null){
            webVersion = "undefined";
      }
      if (webVersion.equals("${build.version}")){
            webVersion = "sviluppo";
      }

      String buildTime = (String)getServletConfig().getServletContext().getAttribute("BUILT_TIME");
      if(buildTime == null){
            buildTime = "undefined";
      }
      String versione = "Versione "+webVersion +" del "+buildTime;
      request.setAttribute("versione", versione);
%>


<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
            <td>
                  ${versione}<br/><br/><br/>
            </td>
           
      </tr>
</table>


Tale jsp sarà inclusa nelle nostre pagine Web tramite il classico <jsp:include page=”version.jsp”></jsp:include>

Si può automatizzare il processo di creazione versione ricorrendo ad un semplice script di Ant, che chiede all’utente di impostare la versione e quindi aggiorna il manifest. Ho definito quindi allo stesso livello della directory src una directory ant con la seguente struttura:




versionCreator.xml è così definito:

<?xml version="1.0" encoding="UTF-8"?>
<project name="PROGETTO_AGGIORNAMENTO_VERSIONE" basedir="../" >
<property name="myManifest" value="${basedir}/WebContent" />
<tstamp>
                  <format property="date" pattern="ddMMyyyy" locale="it"/>
                  <format property="dateManifest" pattern="dd/MM/yyyy" locale="it"/>
                  <format property="timeManifest" pattern="hh:mm:ss"   locale="it"/>
      </tstamp>
      <target name="init" >
                  <description>Prepare for a build tasks</description>
                  <property file="ant/versioning/build.version"/>
                  <property name="current.build.version" value="${current.build.version}"/>
                  <buildnumber file="ant/versioning/build.number"/>
                  <echo message="${line.separator}x.x.0 - Bug-Fix${line.separator}x.0.x - Nuova funzionalità${line.separator}0.x.x - Nuova Versione"/>
                  <input message="Versione attuale: ${current.build.version}${line.separator}Inserire una nuova versione (di default è quella attuale):"
                           addproperty="build.version"
                           defaultvalue="${current.build.version}"
                  />
<echo file="ant/versioning/build.version">current.build.version=${build.version}</echo>
            </target>
<target name="manifest" depends="init" description="Prepare manifest file" >
                  <mkdir dir="${myMainifest}/META-INF" />
                  <manifest file="${myManifest}/META-INF/MANIFEST.MF">
                        <attribute name="Company" value="NOME AZIENDA"/>
                        <attribute name="Author" value="Mario Rossi"/>
                        <attribute name="Implementation-Title" value="PROGETTO TEST"/>
                        <attribute name="Implementation-Version" value="${build.version}"/>
                        <attribute name="Implementation-Build"   value="${build.number}"/>
                        <attribute name="Build-time"   value="${dateManifest} ${timeManifest}"/>
                  </manifest>
                  <echo message="Created directory ${myManifest}/META-INF" />
            </target>
</project>




Lanciando da Eclipse il target manifest verrà chiesto di impostare la versione, riportando la versione attualmente presente come valore di default  nella casella di testo.
In questo modo:







Nessun commento:

Posta un commento