reading content from a jar from inside an Ant Javascript script task

Here's another handy little snippet for reading the content from a jar file while inside a javascript Ant <script> tag (in this example, reading the property file that is inside the ant-contrib jar@/net/sf/antcontrib/antcontrib.properties):

<script language="javascript">
    <![CDATA[
    importPackage(Packages.java.io);
    var antprops = project.getClass().getResource('/net/sf/antcontrib/antcontrib.properties');
    var inis = antprops.openStream();
    var br = new BufferedReader(new InputStreamReader(inis));
    while (null != (line = br.readLine())) {
     print(line);
    }
    ]]>

</script>

getting the current version of ant-contrib from inside Ant

Here's a simple little bit of code for getting the version of ant-contrib running in Apache Ant.

This uses the <script> task, although it would be just as easy (technically easier) to use the java task.

Mostly it's just kind of a useful example doing something java-ish (calling another classes main method) from inside the script tag:

<script language="javascript">
    <![CDATA[
    importClass(Packages.net.sf.antcontrib.AntContribVersion);
    var ver = new AntContribVersion.main(java.lang.reflect.Array.newInstance(java.lang.String, 1));
    ]]>

</script>

BlogCFC was created by Raymond Camden. This blog is running version 5.9.3.000. Contact Blog Owner