This is to permit things like this:

<target name="init"> 
    <exec executable="cleartool" outputproperty="buildnum">
        <arg line="describe -aattr buildnum ${ant.file}"/>
    </exec>
    ...
</target>

Which allows me to fetch (and even potentially fetch+set) the buildnum 
attribute on the build.xml file, which saves me from having to create a
gazillion
versions of a file just to update the build number with each build, and to 
ensure that the build number is stored in the source tree.

With CVS, I do the same thing by looking at the tags and finding the latest
(highest) build number tag.

There are lots of other potential uses for this sort of thing, someone
recently 
asked on this list or the ant-user list how to get the name of the host on
which they were running ant:

<target name="init">
    <exec executable="hostname" outputproperty="build.host"/>
    ...
</target>

Since hostname is available just about anywhere, but environment variables
differ from OS to OS in how they encode (or even if they encode) the 
hostname...

-Peter <<outputproperty_patch.txt>> 
diff -ru ant-original/jakarta-ant-1.3/docs/manual/CoreTasks/exec.html 
ant-patch/jakarta-ant-1.3/docs/manual/CoreTasks/exec.html
--- ant-original/jakarta-ant-1.3/docs/manual/CoreTasks/exec.html        Fri Mar 
 2 04:46:34 2001
+++ ant-patch/jakarta-ant-1.3/docs/manual/CoreTasks/exec.html   Sun Jun 10 
23:37:06 2001
@@ -49,6 +49,12 @@
     <td align="center" valign="top">No</td>
   </tr>
   <tr>
+    <td valign="top">output</td>
+    <td valign="top">the name of a property in which the output of the 
+      command should be stored.</td>
+    <td align="center" valign="top">No</td>
+  </tr>
+  <tr>
     <td valign="top">timeout</td>
     <td valign="top">Stop the command if it doesn't finish within the
       specified time (given in milliseconds).</td>
diff -ru 
ant-original/jakarta-ant-1.3/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
 ant-patch/jakarta-ant-1.3/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
--- 
ant-original/jakarta-ant-1.3/src/main/org/apache/tools/ant/taskdefs/ExecTask.java
   Fri Mar  2 04:46:37 2001
+++ 
ant-patch/jakarta-ant-1.3/src/main/org/apache/tools/ant/taskdefs/ExecTask.java  
    Mon Jun 11 00:05:14 2001
@@ -79,6 +79,8 @@
     private Environment env = new Environment();
     protected Commandline cmdl = new Commandline();
     private FileOutputStream fos = null;
+    private ByteArrayOutputStream baos = null;
+    private String outputprop;
 
     /**
      * Timeout in milliseconds after which the process will be killed.
@@ -126,6 +128,14 @@
     }
 
     /**
+     * Property name whose value should be set to the output of
+     * the process
+     */
+    public void setOutputproperty(String outputprop) {
+       this.outputprop = outputprop;
+    }
+
+    /**
      * Throw a BuildException if process returns non 0.
      */
     public void setFailonerror(boolean fail) {
@@ -233,6 +243,19 @@
                     log("Result: " + err, Project.MSG_ERR);
                 }
             }
+           if (baos != null) {
+               BufferedReader in = 
+                   new BufferedReader(new StringReader(baos.toString()));
+               String line;
+               String val;
+               Project myproj;
+               val = "";
+               while ((line = in.readLine()) != null) {
+                   val = val + line;
+               }
+               myproj = getProject();
+               myproj.setUserProperty(outputprop, val);
+           }
         } catch (IOException e) {
             throw new BuildException("Execute failed: " + e, e, location);
         } finally {
@@ -255,6 +278,11 @@
             } catch (IOException ioe) {
                 throw new BuildException("Cannot write to "+out, ioe, 
location);
             }
+        } else if (outputprop != null) {
+           //      try {
+           baos = new ByteArrayOutputStream();
+           log("Output redirected to ByteArray", Project.MSG_VERBOSE);
+           return new PumpStreamHandler(baos);
         } else {
             return new LogStreamHandler(this,
                                         Project.MSG_INFO, Project.MSG_WARN);
@@ -275,6 +303,7 @@
     protected void logFlush() {
         try {
             if (fos != null) fos.close();
+            if (baos != null) baos.close();
         } catch (IOException io) {}
     }
 

Reply via email to