Differenze tra le versioni di "Programmazione:Java/Script Ant per Versioning"
(Nuova pagina: Inserire nel codice le informazioni di versine può risultare spesso utile, sopratutto nel tempo quando un jar è in produzione e si vuole sapere rapidamente rintracciarne il sorgente....) |
|||
Riga 52: | Riga 52: | ||
</target> | </target> | ||
</project> | </project> | ||
+ | </code> | ||
+ | |||
+ | Queste informazioni possono essere lette a runtime mediante la classe [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Package.html Package] e utilizzate ad esempio nei log. | ||
+ | |||
+ | Ecco un esempio di classe che legge alcune di queste informazioni: | ||
+ | <code java> | ||
+ | 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(); | ||
+ | } | ||
+ | |||
+ | } | ||
</code> | </code> |
Versione delle 12:51, 23 feb 2008
Inserire nel codice le informazioni di versine può risultare spesso utile, sopratutto nel tempo quando un jar è in produzione e si vuole sapere rapidamente rintracciarne il sorgente.
Il seguente esempio di script 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">
<property name="version.num" value="1.0" />
<buildnumber file="build.num" />
<tstamp>
<format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" />
</tstamp>
<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="${TODAY}" />
<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();
}
}