Date: 2004-07-07T16:51:12 Editor: 68.229.240.25 <> Wiki: Ant Wiki Page: AntOddities URL: http://wiki.apache.org/ant/AntOddities
/me looks confused Change Log: ------------------------------------------------------------------------------ @@ -13,7 +13,7 @@ {{{<target name="compile-groovy-scripts"> <exec executable="groovy.bat"> - <arg value="C:/path/to/scripts/*.groovy"/> + <arg value="C:/path/to/scripts/*.groovy"/> <arg line="-d C:/path/to/classes/destination"/> </exec> </target>}}} @@ -40,19 +40,19 @@ CheckStyle - like many other programs - uses the java.util.ResourceBundle.getBundle() method which returns the appropriate bundle for the '''default''' Locale. So I will set the default Locale to the US value. Before that I store the actual one (or the 'key') as property and restore that after invoking CheckStyle. Because I need (simple) access to the Java API, I write that inside <script> tasks: -{{{ <script language="javascript"> <![CDATA[ - importClass(java.util.Locale); - actualDefault = Locale.getDefault(); - project.setProperty("---actual-default-locale---", actualDefault); - Locale.setDefault(Locale.US); - ]]></script> - - <ant .../> - - <script language="javascript"> <![CDATA[ - importClass(java.util.Locale); - actualDefault = project.getProperty("---actual-default-locale---"); - Locale.setDefault(new Locale(actualDefault)); +{{{ <script language="javascript"> <![CDATA[ + importClass(java.util.Locale); + actualDefault = Locale.getDefault(); + project.setProperty("---actual-default-locale---", actualDefault); + Locale.setDefault(Locale.US); + ]]></script> + + <ant .../> + + <script language="javascript"> <![CDATA[ + importClass(java.util.Locale); + actualDefault = project.getProperty("---actual-default-locale---"); + Locale.setDefault(new Locale(actualDefault)); ]]></script> }}} @@ -62,8 +62,8 @@ == Using your own classes inside <script> == When you use <script language="javascript"> you are using the Java API. Ok so far. Itīs simple to use java.io.File for getting information about a particular file or creating complex strings with java.util.StringBuffer. But there are two problems: - * How to import other (non java.*) classes, e.g. Antīs own classes? - * How to import classes which are '''not''' on Antīs classpath? + * How to import other (non java.*) classes, e.g. Antīs own classes? + * How to import classes which are '''not''' on Antīs classpath? === How to import other (non java.*) classes, e.g. Antīs own classes? === @@ -71,9 +71,12 @@ {{{ importClass(Packages.org.apache.tools.ant.types.Path); }}} + +''I'm confused. I was told that Java (static typing, compiled to bytecodes, etc.) is a totally different language than JavaScript (dynamic typing, interpreted, etc.). Are we using Java for scripting here ? Or are we actually using JavaScript ? Or both ?'' + === How to import classes which are '''not''' on Antīs classpath? === -Ok, now we can use Antīs classes and the whole javax.*-stuff. But if I need some custom classes? For example I have to create a reverse sorted list of files. Getting the list of files is no problem. Sorting can be done by java.util.TreeSet. But I need a custom Comparator which does the specific comparison. So I implement one and store it in ${basedir}/ext. +Ok, now we can use Antīs classes and the whole javax.*-stuff. But if I need some custom classes? For example I have to create a reverse sorted list of files. Getting the list of files is no problem. Sorting can be done by java.util.TreeSet. But I need a custom Comparator which does the specific comparison. So I implement one and store it in ${basedir}/ext. And now the trick: Antīs Project class provides methods for getting classloader. And there is one which includes specified paths. So we * get the ''ext'' directory @@ -82,16 +85,16 @@ * load the class * instantiate that -{{{ <script language="javascript"> <![CDATA[ - importClass(java.util.TreeSet); - importClass(Packages.org.apache.tools.ant.types.Path); - - loaderpath = new Path(project, project.getProperty("ext.dir")); - classloader = project.createClassLoader(loaderpath); - comparatorClass = classloader.loadClass("ReportDateComparator"); - comparator = comparatorClass.newInstance(); +{{{ <script language="javascript"> <![CDATA[ + importClass(java.util.TreeSet); + importClass(Packages.org.apache.tools.ant.types.Path); + + loaderpath = new Path(project, project.getProperty("ext.dir")); + classloader = project.createClassLoader(loaderpath); + comparatorClass = classloader.loadClass("ReportDateComparator"); + comparator = comparatorClass.newInstance(); - list = new TreeSet(comparator); + list = new TreeSet(comparator); ]]></script> }}} @@ -151,7 +154,7 @@ While downloading the Milestone 4 of Eclipse I got an idea: why should I download all the files without knowing if there are corrupt? Ok, the following scenario: * define a list with all names of the files to be downloaded * download the file and its MD5 check file - * get the MD5 checksum for the downloaded file + * compute the MD5 checksum for the downloaded file * compare that value with the one stored in the MD5-file And for faster handling: * donīt download files if there are already downloaded and valid @@ -166,84 +169,84 @@ * ''proxy.port'': proxy settings And the final buildfile is: -{{{ <project default="main"> +{{{ <project default="main"> - <taskdef resource="net/sf/antcontrib/antcontrib.properties"/> + <taskdef resource="net/sf/antcontrib/antcontrib.properties"/> - <property name="result.file" value="check-downloads-results.properties"/> - <property file="check-downloads.properties"/> - <property file="${result.file}"/> - - <target name="main"> - <setproxy proxyHost="${proxy.host}" proxyPort="${proxy.port}"/> - <foreach list="${file.list}" param="file" target="checkFile"/> - </target> - - <target name="checkFile" depends="check.download,check.md5-1,check.md5-2" if="file"/> - - <target name="check.init"> - <property name="zip.file" value="${file}"/> - <property name="md5.file" value="${file}.md5"/> - <condition property="md5-ok"><isset property="${zip.file}.isValid"/></condition> - <condition property="download-ok"> - <and> - <available file="${dest.dir}/${zip.file}"/> - <available file="${dest.dir}/${md5.file}"/> - </and> - </condition> - </target> - - <target name="check.download" unless="download-ok" depends="check.init"> - <echo>Download ${md5.file}</echo> - <get src="${download.md5.dir}/${md5.file}" dest="${dest.dir}/${md5.file}"/> - <echo>Download ${zip.file}</echo> - <get src="${download.zip.dir}/${zip.file}" dest="${dest.dir}/${zip.file}"/> - </target> - - <target name="check.md5-1" if="md5-ok" depends="check.init"> - <echo>${zip.file}: just processed</echo> - </target> - - <target name="check.md5-2" unless="md5-ok" depends="check.init"> - <trycatch><try> - <!-- what is the valid md5 value specified in the md5 file --> - <loadfile srcFile="${md5.file}" property="md5.valid"> - <filterchain> - <striplinebreaks/> - <tokenfilter> - <stringtokenizer/> - <replaceregex pattern="${zip.file}" replace=''''''/> - </tokenfilter> - <tokenfilter> - <trim/> - </tokenfilter> - </filterchain> - </loadfile> - <!-- what is the actual md5 value --> - <checksum file="${zip.file}" property="md5.actual"/> - <!-- compare them --> - <condition property="md5.isValid"> - <equals arg1="${md5.valid}" arg2="${md5.actual}"/> - </condition> - <property name="md5.isValid" value="false"/> - <!-- print the result --> - <if> - <istrue value="${md5.isValid}"/> - <then> - <echo>${zip.file}: ok</echo> - <echo file="${result.file}" - append="true" - message="${zip.file}.isValid=true${line.separator}"/> - </then> - <else> - <echo>${zip.file}: Wrong MD5 checksum !!!</echo> - <echo>- expected: ${md5.valid}</echo> - <echo>- actual : ${md5.actual}</echo> - <move file="${zip.file}" tofile="${zip.file}.wrong-checksum"/> - </else> - </if> - </try><catch/></trycatch> - </target> + <property name="result.file" value="check-downloads-results.properties"/> + <property file="check-downloads.properties"/> + <property file="${result.file}"/> + + <target name="main"> + <setproxy proxyHost="${proxy.host}" proxyPort="${proxy.port}"/> + <foreach list="${file.list}" param="file" target="checkFile"/> + </target> + + <target name="checkFile" depends="check.download,check.md5-1,check.md5-2" if="file"/> + + <target name="check.init"> + <property name="zip.file" value="${file}"/> + <property name="md5.file" value="${file}.md5"/> + <condition property="md5-ok"><isset property="${zip.file}.isValid"/></condition> + <condition property="download-ok"> + <and> + <available file="${dest.dir}/${zip.file}"/> + <available file="${dest.dir}/${md5.file}"/> + </and> + </condition> + </target> + + <target name="check.download" unless="download-ok" depends="check.init"> + <echo>Download ${md5.file}</echo> + <get src="${download.md5.dir}/${md5.file}" dest="${dest.dir}/${md5.file}"/> + <echo>Download ${zip.file}</echo> + <get src="${download.zip.dir}/${zip.file}" dest="${dest.dir}/${zip.file}"/> + </target> + + <target name="check.md5-1" if="md5-ok" depends="check.init"> + <echo>${zip.file}: just processed</echo> + </target> + + <target name="check.md5-2" unless="md5-ok" depends="check.init"> + <trycatch><try> + <!-- what is the valid md5 value specified in the md5 file --> + <loadfile srcFile="${md5.file}" property="md5.valid"> + <filterchain> + <striplinebreaks/> + <tokenfilter> + <stringtokenizer/> + <replaceregex pattern="${zip.file}" replace=''''''/> + </tokenfilter> + <tokenfilter> + <trim/> + </tokenfilter> + </filterchain> + </loadfile> + <!-- what is the actual md5 value --> + <checksum file="${zip.file}" property="md5.actual"/> + <!-- compare them --> + <condition property="md5.isValid"> + <equals arg1="${md5.valid}" arg2="${md5.actual}"/> + </condition> + <property name="md5.isValid" value="false"/> + <!-- print the result --> + <if> + <istrue value="${md5.isValid}"/> + <then> + <echo>${zip.file}: ok</echo> + <echo file="${result.file}" + append="true" + message="${zip.file}.isValid=true${line.separator}"/> + </then> + <else> + <echo>${zip.file}: Wrong MD5 checksum !!!</echo> + <echo>- expected: ${md5.valid}</echo> + <echo>- actual : ${md5.actual}</echo> + <move file="${zip.file}" tofile="${zip.file}.wrong-checksum"/> + </else> + </if> + </try><catch/></trycatch> + </target> </project> }}} I got very nice results when starting with -quiet mode. @@ -252,11 +255,11 @@ == Windows XP exec task : use os="Windows XP" instead of "os="Windows NT" despite docs == -using ant 1.6.1 with j2sdk1.4.2 +using ant 1.6.1 with j2sdk1.4.2 Doc incorrectly says you should use os="Windows NT" for the exec task to lauch a batch file -If you try to launch a batch file with 'exec' task, +If you try to launch a batch file with 'exec' task, you should try to use instead : {{{ <exec dir="bat" executable="cmd" os="Windows XP" failonerror="true"> @@ -292,11 +295,11 @@ [exec] Executing 'cmd' with arguments: [exec] '/c' [exec] 'batchXP.bat' - [exec] + [exec] [exec] The ' characters around the executable and arguments are [exec] not part of the command. - [exec] F:\trucking\ant\bat>dir ..\ 1>dirXP.txt + [exec] F:\trucking\ant\bat>dir ..\ 1>dirXP.txt [available] Found: dirXP.txt in F:\trucking\ant\bat @@ -319,7 +322,7 @@ <target name="init"> <condition property="osIsXP"> - <equals arg1="${env.HOMEDRIVE}\WINDOWS" + <equals arg1="${env.HOMEDRIVE}\WINDOWS" arg2="${env.windir}"/> </condition> </target> @@ -364,15 +367,15 @@ public class HelloWorld { public static void main(String arg[]){ - + System.out.println("HELLO WORLD"); } - + } }}} -Maybe you will run a code beautyfier on that so youīll delete the empty lines ... But you can see that the expected code is generated. And the class file is beautified :-) +Maybe you will run a code beautyfier on that so youīll delete the empty lines ... But you can see that the expected code is generated. And the class file is beautified :-) Here the buildfile for the example. Place that in a project root directory and the original java source in "src" subdirectory. {{{ --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]