Programmazione:Java/Script Ant per Versioning
Inserire nel codice informazioni di Software Versioning può risultare spesso utile, sopratutto nel tempo quando un jar è in produzione e si vuole rapidamente rintracciarne il sorgente.
Il seguente esempio di script Apache Ant produce un jar con inserite nel MANIFEST delle informazioni di versione.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyApps_ANT" default="dist" basedir=".">
<description>
JAR creation for MyApps Project
</description>
<property name="build" location="bin" />
<property name="dist" location="dist" />
<property name="source" location="src" />
<target name="init">
</target>
<target name="compile">
<javac srcdir="${source}" />
</target>
<target name="clean" description="clean up">
<delete file="${dist}/MyApps.jar"/>
</target>
<target name="dist" depends="clean" description="generate the distribution">
<tstamp>
<format property="NOW" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<tstamp>
<format property="DAY_VER" pattern="yyyyMMdd" />
</tstamp>
<input message="Versione (x.y.x.vYYYYMMDD)" addproperty="version.num" defaultvalue="1.0.0.v${DAY_VER}" />
<buildnumber file="build.num" />
<mkdir dir="${dist}" />
<manifestclasspath property="jar.classpath" jarfile="MyApps.jar" maxparentlevels="1">
<classpath>
<filelist>
<file name="lib/log4j-1.2.14.jar" />
</filelist>
</classpath>
</manifestclasspath>
<manifest file="MANIFEST.MF" mode="update">
<attribute name="Built-By" value="${user.name}" />
<attribute name="Specification-Title" value="MyApps Service" />
<attribute name="Specification-Version" value="${version.num}" />
<attribute name="Specification-Vendor" value="Sitech Srl" />
<attribute name="Main-Class" value="com.netsitech.myapps.Main" />
<attribute name="Implementation-Version" value="${version.num}-b${build.number}" />
<attribute name="Built-Date" value="${NOW}" />
<attribute name="Class-Path" value=". ${jar.classpath}" />
</manifest>
<jar jarfile="${dist}/MyApps.jar" basedir="${build}" includes="**/*.class" excludes="**/test/*.class" manifest="MANIFEST.MF" compress="true" />
</target>
</project>
Queste informazioni possono essere lette a runtime mediante la classe Package e utilizzate ad esempio nei log.
Ecco un esempio di classe che legge alcune di queste informazioni:
public class Version {
public static String getImplementationVersion(){
return Version.class.getPackage().getImplementationVersion();
}
public static String getImplementationVendor(){
return Version.class.getPackage().getImplementationVendor();
}
public static String getSpecificationTitle(){
return Version.class.getPackage().getSpecificationTitle();
}
public static String getSpecificationVendor(){
return Version.class.getPackage().getSpecificationVendor();
}
}
In alternativa si puo' scrivere un classe Version.java che riporti le informazioni di versione necessarie con il seguente script
<target name="version" description="Scrive la classe Version.java">
<property name="source" location="src" />
<property name="version.num" value="v1.0" />
<buildnumber file="build.num" />
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<mkdir dir="${source}/com/netsitech/myapp"/>
<echo file="${source}/com/netsitech/myapp/Version.java" >package com.netsitech.myapp;
public class Version {
public static String getVersion(){
return "${version.num}";
}
public static String getSpecificationTitle(){
return "MyApp Name";
}
public static String getSpecificationVersion(){
return "${version.num}";
}
public static String getImplementationVersion(){
return "${version.num}-b${build.number}";
}
public static String getBuiltDate(){
return "${TODAY}";
}
public static String getBuiltBy(){
return "${user.name}";
}
}
</echo>
</target>