aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes How do I handle Build Failure in Ant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How do I handle Build Failure in Ant?" Watch "How do I handle Build Failure in Ant?" New topic
Author

How do I handle Build Failure in Ant?

Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
I would like to be able to send mail (for example) whenever the Ant build process fails.
how can I do it in Ant environment?

The problem is that, as far as I understand the Tasks flow in Ant- whenever a task fails in a given list of tasks in Ant process, it throws an exception that causes the process to exit without continueing to the next tasks. So I need a tool to catch such exception and handle this error flow.

Didn't find so far documented solution to my problem.



10x ahead
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Guy,
There's a concept called a logger that can send an e-mail with the results. That would send it out either way. For only failure, you could implement your own listener. Or you could just do it in the operating shell script that calls Ant for you.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
OK, the logger concept is indeed already a prgoress in the right direction.

I am now having troubles in configuring the content of this email.
I'd like the mail to contain the tail of the last 10-20 lines of the log file that was created during the build
so it will include the last error that caused the build to fail.

the Document about the logger
http://ant.apache.org/manual/listeners.html

doesn't supply a property to handle the Mail's body. They mention there the to, from, subject but not the body and therefore
I got empty email.
I also tried to add to the process ant ... - logger ... -logfile ... but it still didn't change anything in result mail of the build process.
Any suggestions?
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
Anyone?

I'll try to be clearer:
I am using the command line:


where:
acm.nightly is our specific build task

and in addition defined the mandatory property for the use of the maillogger:
MailLogger.mailhost=
MailLogger.from=
MailLogger.failure.to=

The resut in case of build failure:
I get a mail from the MailLogger but its body is empty even though the file log/build.log is not empty. while I thought that by default it should contain the content of log file (or better on the last 20 line of it).

I'll be glad to get any suggestion what I should do.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Guy,
I don't know. I use a UNIX script to send mail. (After doing some filtering to get relevant lines.) I had heard of the logger so shared, but that's as far as my experience with it goes.
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
Jeanne,
1. what is the content of the mail that is sent from Ant - doest it contain the build log file?
2. can you provide the script that runs the Ant build task?
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
So, anyone know how I can get a mail with the build log errors in case the build failed using Ant?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Guy Roth wrote:So, anyone know how I can get a mail with the build log errors in case the build failed using Ant?


Not me. But I'd suggest to take a look at a continuous integration server such as Cruise Control or Hudson.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10267
    
168

There are various ways of doing this. As others have already pointed out, this can be done outside Ant. But if you want it to be part of Ant, then you could you either of these 2 ways:

1) Use the Log4jListener http://ant.apache.org/manual/listeners.html#Log4jListener which will log the task message to a log file. To trigger a mail, you can configure a log4j category and a SMTPAppender in the log4j.xml for the build finished event (org.apache.tools.ant.Project event). Just an example (will need tweaking, because the org.apache.tools.ant.Project event will be triggered both at start and stop of the builds).



2) Or you could implement your own listener (which would probably extend the MailLogger http://ant.apache.org/manual/listeners.html#MailLogger) to even send the logs as the email body.

[My Blog] [JavaRanch Journal]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Guy Roth wrote:Jeanne,
1. what is the content of the mail that is sent from Ant - doest it contain the build log file?

It contains pieces of the build log file.

Guy Roth wrote:2. can you provide the script that runs the Ant build task?

No. It's the intellectual property of my employer. I can tell you that it's basically a bunch of find/grep statements that look for strings of interest to us.
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
Does anyone know to tell me how to configure Ant to send email with the build errors in case the build failed?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Guy Roth wrote:Does anyone know to tell me how to configure Ant to send email with the build errors in case the build failed?

We're back to the original suggestions - write your own logger or write an operating system script. Why don't you pick one and ask when you have more concrete suggestions on the approach?
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
About the last responses:

1. First of all I don't think that I need to extend the 'org.apache.tools.ant.listener.MailLogger' ready made logger. I don't have any requirements different than the statdard.
I just don't understand why when I tried to use this logger it sent mail with empty body although it was suppose to append to an inner buffer every line that is logged and then send it as the body of the mail when the 'buildFinished' event is thrown. There must be something that I wrote wrong that causes this problem and therefore I asked in this forum how others do use the ordinery MailLogger

2. Since I saw I am not progressing, I followed the recommendation that was given here and extended the MailLogger by writing logger that inherit it in Java. But when I tried to use it in the command line:

I got java.lang.ClassNotFoundException so my question is how can I provide a class path to ant command line?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Guy Roth wrote:I got java.lang.ClassNotFoundException so my question is how can I provide a class path to ant command line?

-lib <path>

If you need any other options, I looked it up from the command line page. (I forgot if it was -cp, -lib or -build.) The downside to writing reusable code is you only see it once .
Guy Roth
Ranch Hand

Joined: Aug 09, 2009
Posts: 74
OK, seems like the problem is solved, also thanks to the advices here.

What seems to be the problem was that I am using Ant 1.6.2 and perhaps the MailLogger in this version doesn't work properly.
I found the source file of MailLogger of version 1.7 and added this class as an extension to my project and then the MailLogger return to work properly including sending the content of the log file as the body of the mail.

So great, I can close this issue!

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30973
    
159

Guy Roth wrote:So great, I can close this issue!

Great! I've been on Ant 1.7 for over a year and forgot about the old one .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I handle Build Failure in Ant?