Saturday, June 28, 2014

Apache Ant vs Gradle : The Echo Task

By default, the echo task in Apache Ant prints to the console window (ie. System.out).
The text you want to echo out can either be set as the message attribute:
<?xml version="1.0"?>
   <echo message = "Hello World" />
Or placed inside the body of the echo tag:
<?xml version="1.0"?>
   <echo>Hello World<echo />
If you were to save this to a build.xml file and then run ant from the command line, you would see output similar to the following:
Buildfile: C:\newFolder\build.xml
     [echo] Hello World

Total time: 0 seconds
Since the default logging level in Apache Ant is warning, here's how you would accomplish the same thing in Gradle:
logger.warn "Hello World"
If you were to save this to a build.gradle file and then run gradle from the command line, you would see output similar to the following:
Hello World
Welcome to Gradle 1.10.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help


Total time: 3.51 secs
You can also use the println function in Gradle which is analogous to logger.quiet.

println "Hello World"
logger.quiet "Hello World"

I'm not sure what the equivalent is in Apache Ant.

To change the log level in Apache Ant you use the level attribute:
<?xml version="1.0"?>
   <echo message = "Hello World" level = "debug" />
Now the message will only be displayed if debug option is used, either -debug or -d.

C:\newFolder\ant -debug C:\newFolder\ant -d

Gradle has a similar command line option, except it uses two dashes instead of one for "--debug".

Apache Ant Gradle
C:\newFolder\ant -debug C:\newFolder\gradle --debug

If you want a message to span mutliple lines, in Apache Ant you use ${line.separator}
<?xml version="1.0"?>
   <echo>Hello${line.separator}World<echo />
In Gradle, you can either call the log method multiple times:
logger.warn "first"
logger.warn "second"
Or you can use the newline escape character (ie \n):
logger.warn "first\nsecond"
In Apache Ant, you can send the log message to a file using the file attribute.
<echo file="antLog.txt" append="false"> </echo>
If there's already an existing file with that specified name (ie antLog.txt) in the current directory, then that file will be overwritten. If instead you want to append the message to the end of the file you can use the append attribute, like so:
<?xml version="1.0"?>
   <echo file = "antLog.txt" append = "true">Hello World<echo />
In Gradle, this accomplished with the following:
def gradleLog = new FileOutputStream(file("gradleLog.txt"), true)

   new StandardOutputListener( ) {
      public void onOutput(CharSequence output) {
         gradleLog << output;

logger.warn "Hello World"


No comments:

Post a Comment