ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steve Cohen" <SCo...@sportvision.com>
Subject RE: Conditional if/unless echo
Date Thu, 10 Apr 2003 14:01:30 GMT
Erik, that really isn't a solution - especially when the thing you want
to echo may not be defined requiring a whole nest of cumbersome logic to
allow for that possibility without breaking the script.  How about a
task that would simply echo a property if it is defined and "undefined"
if it isn't?
<echoifdefined property=${property}/>
Would be highly useful in debugging scripts.

-----Original Message-----
From: Erik Hatcher [mailto:jakarta-ant@ehatchersolutions.com] 
Sent: Thursday, April 10, 2003 8:37 AM
To: Ant Users List
Subject: Re: Conditional if/unless echo


But <echo> supports the 'level' attribute, so you can control what gets 
echo'd by using -verbose, -debug, -quiet command-line switches.

	Erik


On Thursday, April 10, 2003, at 09:25  AM, David McTavish wrote:

> I've created my own Echo task that does something similar to what
> you're
> looking for, however, I pass in a true/false value, and display the 
> message
> ONLY if the verbose flag is set to true. By default, the property is 
> always
> true. If anyone wants to add this to Ant, feel free.
>
> usage:
> <taskdef name="svecho" classname="Echo" classpathref="ant.classpath"/>

> <property name="verbose" value="false"/> <svecho message="Please 
> display this message" verbose="${verbose}"/> <svecho message="Please 
> display this message"/>
>
> d.
>
> -------------------------------------------------------------------
>
> import org.apache.tools.ant.Task;
> import org.apache.tools.ant.Project;
> import org.apache.tools.ant.BuildException;
>
> import org.apache.tools.ant.types.EnumeratedAttribute;
> import java.io.File;
> import java.io.FileWriter;
> import java.io.IOException;
>
> /**
>  * Writes a message to the Ant logging facilities.
>  *
>  * @author costin@dnt.ro
>  *
>  * @since Ant 1.1
>  *
>  * @ant.task category="utility"
>  */
> public class Echo extends Task
> {
>     protected String message = ""; // required
>     protected File file = null;
>     protected boolean append = false;
>     protected boolean display = true;
>
>     // by default, messages are always displayed
>     protected int logLevel = Project.MSG_WARN;
>
>     /**
>      * Does the work.
>      *
>      * @exception BuildException if someting goes wrong with the build
>      */
>     public void execute() throws BuildException
>     {
>         if (display)
>         {
>             if (file == null)
>             {
>                 log(message, logLevel);
>             }
>             else
>             {
>                 FileWriter out = null;
>                 try
>                 {
>                     out = new FileWriter(file.getAbsolutePath(),
> append);
>                     out.write(message, 0, message.length());
>                 }
>                 catch (IOException ioe)
>                 {
>                     throw new BuildException(ioe, location);
>                 }
>                 finally
>                 {
>                     if (out != null)
>                     {
>                         try
>                         {
>                             out.close();
>                         }
>                         catch (IOException ioex) {}
>                     }
>                 }
>             }
>         }
>     }
>
>     public void setVerbose(boolean verbose)
>     {
>         display = verbose;
>     }
>
>
>     /**
>      * Message to write.
>      *
>      * @param msg Sets the value for the message variable.
>      */
>     public void setMessage(String msg) {
>         this.message = msg;
>     }
>
>     /**
>      * File to write to.
>      */
>     public void setFile(File file) {
>         this.file = file;
>     }
>
>     /**
>      * If true, append to existing file.
>      */
>     public void setAppend(boolean append) {
>         this.append = append;
>     }
>
>     /**
>      * Set a multiline message.
>      */
>     public void addText(String msg) {
>         message += project.replaceProperties(msg);
>     }
>
>     /**
>      * Set the logging level. Level should be one of
>      * <ul>
>      *  <li>error</li>
>      *  <li>warning</li>
>      *  <li>info</li>
>      *  <li>verbose</li>
>      *  <li>debug</li>
>      * </ul>
>      * <p>The default is &quot;warning&quot; to ensure that messages
> are
>      * displayed by default when using the -quiet command line 
> option.</p>
>      */
>     public void setLevel(EchoLevel echoLevel) {
>         String option = echoLevel.getValue();
>         if (option.equals("error")) {
>             logLevel = Project.MSG_ERR;
>         } else if (option.equals("warning")) {
>             logLevel = Project.MSG_WARN;
>         } else if (option.equals("info")) {
>             logLevel = Project.MSG_INFO;
>         } else if (option.equals("verbose")) {
>             logLevel = Project.MSG_VERBOSE;
>         } else {
>             // must be "debug"
>             logLevel = Project.MSG_DEBUG;
>         }
>     }
>
>     public static class EchoLevel extends EnumeratedAttribute {
>         /**
>          * @see EnumeratedAttribute#getValues
>          */
>         public String[] getValues() {
>             return new String[] {"error", "warning", "info",
>                                  "verbose", "debug"};
>         }
>     }
> }
>
>
>
>
> -----Original Message-----
> From: Karsten Wutzke [mailto:kwutzke@starconcept.de]
> Sent: Thursday, April 10, 2003 9:17 AM
> To: Ant Users List
> Subject: Conditional if/unless echo
>
>
> Hello all!
>
> I still don't see why there is no if/unless support for the <echo> 
> task...! It's even something I'd find *mandatory* for many other 
> tasks, if not all. The <fail> task has that feature already, beats me 
> why it's not in <echo>.
>
> Anyway, is there really no other solution but to create conditional 
> targets that are only used to echo? Is there at least a solution to 
> use a target as an entry point for all conditional echos, so that the 
> condition (==value of property) and message get passed to the target 
> e.g. via <antcall>
>
> I tried:
>
> <target name="echo-ifset" if="${echo.cond}">
>    <echo message="${echo.msg}"/>
> </target>
>
> <target name="echo-ifnotset" unless="${echo.cond}">
>    <echo message="${echo.msg}"/>
> </target>
>
> <!-- Try to reach local Tomcat -->
> <target name="check-reach-local" depends="check-inst-local" 
> description="Tries to reach the local Tomcat server.">
>    <waitfor timeoutproperty="local.unreachable" maxwait="2000" 
> maxwaitunit="millisecond">
>      <socket server="${local.server}" port="${local.port}"/>
>    </waitfor>
>
>    <antcall target="echo-ifset">
>      <param name="echo.cond" value="${local.unreachable}"/>
>      <param name="echo.msg"  value="Local Tomcat server *not* 
> reachable at '${local.server}:${local.port}'."/>
>    </antcall>
>
>    <antcall target="echo-ifnotset">
>      <param name="echo.cond" value="${local.unreachable}"/>
>      <param name="echo.msg"  value="Local Tomcat server reachable at 
> '${local.server}:${local.port}'."/>
>    </antcall>
>
> </target>
>
>
> Which obviously doesn't work... My guess is, that during the <antcall>

> the value "${local.unreachable}" is always assigned to echo.cond 
> property, which leads to always omitting the execution of the isset 
> (a.k.a if condition) target...
>
> Isn't there an easier solution? I simply want to print the status of 
> sometimes, but I have no (good) idea. Do I really have to create a 
> task for each conditional echo I want?
>
> Thanks!
>
> Karsten
>
>
>
> ---------------------------------------------------------------------
> 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


Mime
View raw message