Differenze tra le versioni di "Programmazione:Java/Script Ant per Versioning"
m  | 
				|||
| (4 versioni intermedie di 2 utenti non mostrate) | |||
| Riga 1: | Riga 1: | ||
| + | [[Programmazione:Java|<< Back to Java]]  | ||
| + | |||
Inserire nel codice informazioni di [http://en.wikipedia.org/wiki/Software_versioning Software Versioning] può risultare spesso utile, sopratutto nel tempo quando un jar è in produzione e si vuole rapidamente rintracciarne il sorgente.  | Inserire nel codice informazioni di [http://en.wikipedia.org/wiki/Software_versioning 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 Ant produce un jar con inserite nel [http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html MANIFEST] delle informazioni di versione.  | + | Il seguente esempio di script [http://ant.apache.org/ Apache Ant] produce un jar con inserite nel [http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html MANIFEST] delle informazioni di versione.  | 
<code xml>  | <code xml>  | ||
<?xml version="1.0" encoding="UTF-8"?>  | <?xml version="1.0" encoding="UTF-8"?>  | ||
| Riga 26: | Riga 28: | ||
	<target name="dist" depends="clean" description="generate the distribution">  | 	<target name="dist" depends="clean" description="generate the distribution">  | ||
| − | |||
| − | |||
		<tstamp>  | 		<tstamp>  | ||
| − | 			<format property="  | + | 			<format property="NOW" pattern="yyyy-MM-dd HH:mm:ss" />  | 
| + | 		</tstamp>  | ||
| + | 		<tstamp>  | ||
| + | 			<format property="DAY_VER" pattern="yyyyMMdd" />  | ||
		</tstamp>  | 		</tstamp>  | ||
| − | 		<mkdir dir="${dist}"/>  | + | 	        <input message="Versione (x.y.x.vYYYYMMDD)" addproperty="version.num" defaultvalue="1.0.0.v${DAY_VER}" />  | 
| − | 		<manifestclasspath property="jar.classpath" jarfile="MyApps.jar" maxparentlevels="1" >  | + | 		<buildnumber file="build.num" />  | 
| + | 		<mkdir dir="${dist}" />  | ||
| + | 		<manifestclasspath property="jar.classpath" jarfile="MyApps.jar" maxparentlevels="1">  | ||
			<classpath>  | 			<classpath>  | ||
				<filelist>  | 				<filelist>  | ||
| − | 					<file name="lib/log4j-1.2.14.jar"/>  | + | 					<file name="lib/log4j-1.2.14.jar" />  | 
				</filelist>  | 				</filelist>  | ||
			</classpath>  | 			</classpath>  | ||
| Riga 46: | Riga 51: | ||
			<attribute name="Main-Class" value="com.netsitech.myapps.Main" />  | 			<attribute name="Main-Class" value="com.netsitech.myapps.Main" />  | ||
			<attribute name="Implementation-Version" value="${version.num}-b${build.number}" />  | 			<attribute name="Implementation-Version" value="${version.num}-b${build.number}" />  | ||
| − | 			<attribute name="Built-Date" value="${  | + | 			<attribute name="Built-Date" value="${NOW}" />  | 
| − | 			<attribute name="Class-Path" value=". ${jar.classpath}"/>  | + | 			<attribute name="Class-Path" value=". ${jar.classpath}" />  | 
		</manifest>  | 		</manifest>  | ||
| − | 		<jar jarfile="${dist}/MyApps.jar" basedir="${build}" includes="**/*.class" excludes="**/test/*.class" manifest="MANIFEST.MF" compress="true"/>  | + | 		<jar jarfile="${dist}/MyApps.jar" basedir="${build}" includes="**/*.class" excludes="**/test/*.class" manifest="MANIFEST.MF" compress="true" />  | 
	</target>  | 	</target>  | ||
</project>  | </project>  | ||
| Riga 77: | Riga 82: | ||
}  | }  | ||
</code>  | </code>  | ||
| + | |||
| + | In alternativa si puo' scrivere un classe Version.java che riporti le informazioni di versione necessarie  con il seguente script  | ||
| + | |||
| + | <code xml>  | ||
| + | 	<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>  | ||
| + | <code>  | ||
Versione attuale delle 19:36, 9 apr 2009
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>