Grokbase Groups Ant user March 2015
FAQ
Hello,

I call Ant (execute a build) from a .bat script (under Windows). If the build fails, I'd like not only to detect that it failed (this is possible via ERRORLEVEL) but also get the failure message (at least its first line if the message contains multiple lines).

Is it possible without parsing the output of Ant? If yes, how?

Thank you!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org

Search Discussions

  • Martin Gainty at Mar 25, 2015 at 12:54 am
    build.xml:

    <target name="caller">
    <!-- pass all properties and pass all references from current environment to callee -->
    <ant inheritAll="true" inheritrefs="true" antfile="subbuild.xml" target="callee" output="output.html">
    </ant>
    </target>
    make sure callee target of ant file subbuild.xml writes all its output to output.html

    https://ant.apache.org/manual/Tasks/ant.html

    Martin
    ______________________________________________


    From: al.le@gmx.de
    To: user@ant.apache.org
    Subject: Pass build failure message to the calling script
    Date: Tue, 24 Mar 2015 23:31:00 +0100

    Hello,

    I call Ant (execute a build) from a .bat script (under Windows). If the build fails, I'd like not only to detect that it failed (this is possible via ERRORLEVEL) but also get the failure message (at least its first line if the message contains multiple lines).

    Is it possible without parsing the output of Ant? If yes, how?

    Thank you!

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
    For additional commands, e-mail: user-help@ant.apache.org
  • Al Le at Mar 25, 2015 at 9:40 am

    Is it possible without parsing the output of Ant? If yes, how?
    output="output.html"
    But doesn't it essentially mean parsing the build output -- something I'd like to avoid?

    I rather thought about Ant setting some kind of an environment variable when it exits. I.e. I'd specify some option (which probably does not exist yet) on the command line which would specify the name of a env variable to set, and Ant would set that variable when it exits.

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
    For additional commands, e-mail: user-help@ant.apache.org
  • Matèrne, Jan (RZF, SG 481) at Mar 25, 2015 at 10:54 am
    Not sure if this would fit your needs:
    - write a custome build logger and store the required information in a new file
    - check the content of that file

    Jan


    package org.apache.ant;

    import org.apache.tools.ant.*;
    import java.io.*;

    public class ResultBuildLogger extends DefaultLogger {

         public void buildFinished(BuildEvent event) {
             super.buildFinished(event);

             try(PrintWriter pw = new PrintWriter("build.result")) {

                 pw.printf("%s=%s%n", "message", event.getMessage());
                 if (event.getException() != null) {
                     pw.printf("%s=%s%n", "exception.message", event.getException().getMessage());
                 }

             } catch (Exception e) {
             }
         }

    }


    <project default="build">

         <property name="build.dir" value="build" />
         <property name="classes.dir" value="${build.dir}/classes" />

         <target name="clean">
             <delete dir="${build.dir}" />
         </target>

         <target name="build">
             <mkdir dir="${classes.dir}" />
             <javac srcdir="src" destdir="${classes.dir}" includeantruntime="true" />
         </target>

         <target name="fail1">
             <fail message="This is my error message" />
         </target>

         <!--
         exception.message=This is my error message
         -->
         <target name="test1">
             <exec executable="cmd.exe">
                 <arg line="/K ant.bat -lib ${classes.dir} -logger org.apache.ant.ResultBuildLogger fail1" />
             </exec>
         </target>

         <!--
         exception.message=Compile failed; see the compiler error output for details.
         -->
         <target name="fail2">
             <echo>Try to compile invalid java code ...</echo>
             <mkdir dir="${build.dir}/gen-src" />
             <echo file="${build.dir}/gen-src/MyClass.java">
               import org.apache.commons.io.IOUtils;
               public class MyClass {}
             </echo>
             <mkdir dir="${build.dir}/gen-classes" />
             <javac srcdir="${build.dir}/gen-src" destdir="${build.dir}/gen-classes" includeantruntime="false" />
         </target>

         <target name="test2">
             <exec executable="cmd.exe">
                 <arg line="/K ant.bat -lib ${classes.dir} -logger org.apache.ant.ResultBuildLogger fail2" />
             </exec>
         </target>


    </project>


    -----Ursprüngliche Nachricht-----
    Von: Al Le
    Gesendet: Mittwoch, 25. März 2015 10:40
    An: user@ant.apache.org
    Betreff: Aw: RE: Pass build failure message to the calling script
    Is it possible without parsing the output of Ant? If yes, how?
    output="output.html"
    But doesn't it essentially mean parsing the build output -- something I'd like to avoid?

    I rather thought about Ant setting some kind of an environment variable when it exits. I.e. I'd specify some option (which probably does not exist yet) on the command line which would specify the name of a env variable to set, and Ant would set that variable when it exits.

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
    For additional commands, e-mail: user-help@ant.apache.org
  • Al Le at Mar 25, 2015 at 8:19 pm
    Thank you for the hint! That would be probably the simplest solution.
    There seems to be no solution without parsing some kind of file.


    Am 25.03.2015 um 11:53 schrieb Matèrne, Jan (RZF, SG 481):
    Not sure if this would fit your needs:
    - write a custome build logger and store the required information in a new file
    - check the content of that file

    Jan


    package org.apache.ant;

    import org.apache.tools.ant.*;
    import java.io.*;

    public class ResultBuildLogger extends DefaultLogger {

    public void buildFinished(BuildEvent event) {
    super.buildFinished(event);

    try(PrintWriter pw = new PrintWriter("build.result")) {

    pw.printf("%s=%s%n", "message", event.getMessage());
    if (event.getException() != null) {
    pw.printf("%s=%s%n", "exception.message", event.getException().getMessage());
    }

    } catch (Exception e) {
    }
    }

    }


    <project default="build">

    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />

    <target name="clean">
    <delete dir="${build.dir}" />
    </target>

    <target name="build">
    <mkdir dir="${classes.dir}" />
    <javac srcdir="src" destdir="${classes.dir}" includeantruntime="true" />
    </target>

    <target name="fail1">
    <fail message="This is my error message" />
    </target>

    <!--
    exception.message=This is my error message
    -->
    <target name="test1">
    <exec executable="cmd.exe">
    <arg line="/K ant.bat -lib ${classes.dir} -logger org.apache.ant.ResultBuildLogger fail1" />
    </exec>
    </target>

    <!--
    exception.message=Compile failed; see the compiler error output for details.
    -->
    <target name="fail2">
    <echo>Try to compile invalid java code ...</echo>
    <mkdir dir="${build.dir}/gen-src" />
    <echo file="${build.dir}/gen-src/MyClass.java">
    import org.apache.commons.io.IOUtils;
    public class MyClass {}
    </echo>
    <mkdir dir="${build.dir}/gen-classes" />
    <javac srcdir="${build.dir}/gen-src" destdir="${build.dir}/gen-classes" includeantruntime="false" />
    </target>

    <target name="test2">
    <exec executable="cmd.exe">
    <arg line="/K ant.bat -lib ${classes.dir} -logger org.apache.ant.ResultBuildLogger fail2" />
    </exec>
    </target>


    </project>


    -----Ursprüngliche Nachricht-----
    Von: Al Le
    Gesendet: Mittwoch, 25. März 2015 10:40
    An: user@ant.apache.org
    Betreff: Aw: RE: Pass build failure message to the calling script
    Is it possible without parsing the output of Ant? If yes, how?
    output="output.html"
    But doesn't it essentially mean parsing the build output -- something I'd like to avoid?

    I rather thought about Ant setting some kind of an environment variable when it exits. I.e. I'd specify some option (which probably does not exist yet) on the command line which would specify the name of a env variable to set, and Ant would set that variable when it exits.

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
    For additional commands, e-mail: user-help@ant.apache.org


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
    For additional commands, e-mail: user-help@ant.apache.org
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
    For additional commands, e-mail: user-help@ant.apache.org

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriesant
postedMar 24, '15 at 10:31p
activeMar 25, '15 at 8:19p
posts5
users3
websiteant.apache.org

People

Translate

site design / logo © 2018 Grokbase