ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Hatcher <jakarta-...@ehatchersolutions.com>
Subject Re: Conditional if/unless echo
Date Thu, 10 Apr 2003 14:03:03 GMT
Gotcha.  You could still use an Ant property for the value of the 
'level' attribute, so essentially you could hide it by putting it at 
the debug level.  But your solution is grand also!

	Erik


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

> The problem with that approach is that you can only set the level once 
> per
> run of the build script, not conditionally upon execution of the 
> target. I
> mostly use this for targets that MAY get called multiple times from
> different sources, and I don't want a header message displaying every 
> time
> the event gets triggered. It seems to work nicely in conjunction with 
> the
> nobanner logger.
>
>
>
> -----Original Message-----
> From: Erik Hatcher [mailto:jakarta-ant@ehatchersolutions.com]
> Sent: Thursday, April 10, 2003 9: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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


Mime
View raw message