Title: RE: Setting up the BuildMonitor listener
This works for me:
 
 
 
 
 
 
import java.io.*;
import org.apache.tools.ant.*;
 
// class declaration
public class AutoBuildListener implements BuildListener
{
 StringBuffer    msgBody = new StringBuffer();
 String       mailRecipient = null;
 String       XtraMsg = "";
 
 public void buildStarted(BuildEvent be)
 {}
 
 // Once the build is done, assemble the message & log/send it...
 public void buildFinished(BuildEvent be)
 {
  boolean thisBuildSucceeded = (be.getException() != null) ? false : true;
  boolean lastBuildSucceeded = AutoBuild.getLastBuildSucceeded();
  String status = (thisBuildSucceeded) ? "succeeded" : "failed";
  String buildInProgress = AutoBuild.getBuildInProgressNum();
  String line = null;
  BufferedReader is = null;
 
  try
  {
   // Recipient depends on build success or failure...
   if (thisBuildSucceeded && ! lastBuildSucceeded)
   {
    mailRecipient = Utils.getEnv("AB.EMAILBUILDFAILED");
    XtraMsg = "The Build is FIXED: ";
   }
   else if (thisBuildSucceeded && lastBuildSucceeded)
   {
    mailRecipient = AutoBuild.p4.getEmailForUser(
         AutoBuild.p4.getUserForCLNum(
          buildInProgress));
   }
   else
   {
    mailRecipient = Utils.getEnv("AB.EMAILBUILDFAILED");
   }
 
   // Load the p4 log into a msg buffer...
   is = new BufferedReader(
     new InputStreamReader(
      new FileInputStream(
       Utils.getEnv("P4.LOGFILE"))));
 
   while((line = is.readLine()) != null)
   {
    msgBody.append(line + "\n");
   }
 
   if (!thisBuildSucceeded)
   {
    AutoBuild.setLastBuildSucceeded(false);
 
    msgBody.append("Build failures...\n\n");
 
    // Load the Ant log into a msg buffer...
    is = new BufferedReader(
      new InputStreamReader(
       new FileInputStream(
        Utils.getEnv("ANT.LOGFILE"))));
 
    while((line = is.readLine()) != null)
    {
     msgBody.append(line + "\n");
    }
 
   }
   else
   {
    AutoBuild.setLastBuildSucceeded(true);
 
    // For autotest...
    PrintStream ps = new PrintStream(
         new FileOutputStream(
          Utils.getEnv("AB.AUTOTESTFILE")));
 
    ps.print(buildInProgress);
    ps.close();
   }
 
   AutoBuild.setLastBuildRun(buildInProgress);
   AutoBuild.setLastBuildStatus(buildInProgress);
   is.close();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 
  try
  {
   // Subject, msg, recipient...
   String[] email = new String[3];
   email[0] = XtraMsg + "build # " + buildInProgress + " " + status;
   email[1] = msgBody.toString();
   email[2] = mailRecipient;
 
   Utils.sendEmail(email);
  }
  catch (Exception e)
  {
   System.out.println("BuildMonitor failed to send email!");
   e.printStackTrace();
  }
 }
 
 
 
 
 
 
 
 
 public static void sendEmail(String[] email) throws Exception
 {
  MailMessage msg = null;
 
  msg = new MailMessage(getEnv("AB.SMTP_RELAY"));
  msg.from(getEnv("AB.MAILFROM"));
  msg.to(getEnv("AB.QAEMAIL"));
  msg.to(email[2]);
  msg.setSubject(email[0]);
  msg.getPrintStream().println(email[1]);
  msg.sendAndClose();
 }
-----Original Message-----
From: Aarti Chandnani [mailto:[EMAIL PROTECTED]]
Sent: Friday, April 27, 2001 9:50 AM
To: '[EMAIL PROTECTED]'
Subject: RE: Setting up the BuildMonitor listener

Exactly the same prb!
I had the same prb while I was doing this:
My code looked like:

 Throwable th = e.getException();
                   System.out.println("here 1");
                   String status = (th !=null)? "failed":"succeeded";
                   System.out.println("here 2");
                   try
                   {
                           if(props.getProperty("build."+status +".notify").equalsIgnoreCase("false"))
                                        return;
                                System.out.println("here 3");
                           Session session = Session.getDefaultInstance(props,null);
                           if(session == null)
                                System.out.println("session is null");
                           else
                                System.out.println("session not null");
                           System.out.println("here 4");

                           MimeMessage message = new MimeMessage(session);
                           if(message == null)
                                System.out.println(" message is null");
                           else
                                System.out.println("message NOT null");

And the o/p that i get looks like:
here 1
here 2
here 3
session not null
here 4
***Build Monitor failed to send mail!***

 caught exception java.lang.NullPointerException
java.lang.NullPointerException
        at BuildMonitor.buildFinished(BuildMonitor.java, Compiled Code)
        at org.apache.tools.ant.Project.fireBuildFinished(Project.java,
 Code)
        at org.apache.tools.ant.Main.runBuild(Main.java, Compiled Code)
        at org.apache.tools.ant.Main.main(Main.java:149)
C:\build>

I think there is a prb with the creation of the session object. It is not null, but maybe it is not set correctly. Thus the message object is not being created and it throws a null pointer exception.

Try and check the properties file and make sure that the protocol and the smtp host etc is set right.


-----Original Message-----
From: Eddie Bernard [mailto:[EMAIL PROTECTED]]
Sent: Friday, April 27, 2001 12:31 AM
To: '[EMAIL PROTECTED]'
Subject: RE: Setting up the BuildMonitor listener


Thanks!!!  I can finally sleep better tonight!!
 
However, now I'm having another problem.....
 
I'm getting another exception thrown which I've narrowed down to the
buildFinished method.  I've added some very crude trace statement and it
appears that it's barfing on instantiating the 'message' object.  Here's the
code snipet (including my trace statements):
 
    public void buildFinished(BuildEvent e) {
        Throwable th = e.getException();
        String status = (th != null) ? "failed" : "succeeded";
 
        try {
            if (props.getProperty("build." + status +
".notify").equalsIgnoreCase("false")) {
                    return;
            }
            System.out.println(props);

            Session session = Session.getDefaultInstance(props, null);
            System.out.println(session);
 
            System.out.println("Before creating message object");
            MimeMessage message = new MimeMessage(session);
            System.out.println("Created the message object");
            System.out.println(message);
            message.addRecipients(Message.RecipientType.TO, parseAddresses(
                props.getProperty("build." + status + ".email.to")));
            message.setSubject(props.getProperty("build." + status +
".email.subject"));
            System.out.println("Trying to print message object");
            System.out.println(message);
 
            BufferedReader br = new BufferedReader(new FileReader(
                props.getProperty("build.log")));
            System.out.println(br);
 
            StringWriter sw = new StringWriter();
 
            String line = br.readLine();
            while (line != null) {
                sw.write(line);
                sw.write("\n");
                line = br.readLine();
            }
            br.close();
 
            message.setText(sw.toString(), "UTF-8");
            sw.close();
 
            Transport transport = session.getTransport();
            transport.connect();
            transport.send(message);
            transport.close();
        } catch (Exception ex) {
            System.out.println("BuildMonitor failed to send email!");
            ex.printStackTrace();
        }
    }

Here's the snipet including the stack trace from the logged output when I
invoke Ant:
 
Total time: 6 seconds
{build.failed.email.to=***@***.com
<mailto:{build.failed.email.to=***@***.com> ,
build.failed.email.subject=Nightly build failed!, mail.from=***@***.com
<mailto:mail.from=***@***.com> , build.succeeded.email.subject=Nightly build
succeeded!, mail.smtp.host=***.***.com, mail.transport.protocol=smtp,
build.log=build.log, build.succeeded.notify=true,
build.succeeded.email.to=***@***.com,build.failed.notify=true
<mailto:build.succeeded.email.to=***@***.com,build.failed.notify=true> }
javax.mail.Session@621187b0 <mailto:javax.mail.Session@621187b0>
Before creating message object
BuildMonitor failed to send email!
java.lang.NullPointerException
        at BuildMonitor.buildFinished(BuildMonitor.java, Compiled Code)
        at org.apache.tools.ant.Project.fireBuildFinished(Project.java,
Compiled
 Code)
        at org.apache.tools.ant.Main.runBuild(Main.java, Compiled Code)
        at org.apache.tools.ant.Main.main(Main.java:149)
 
Again, my trace after the Message message = ... statement does not output,
so I'm assuming that the constructor invocation is thowing an exception.
 
BTW- I'm using Sun JavaMail 1.2.
 
 
 
-----Original Message-----
From: Aarti Chandnani [mailto:[EMAIL PROTECTED]]
Sent: Thursday, April 26, 2001 1:28 PM
To: '[EMAIL PROTECTED]'
Subject: RE: Setting up the BuildMonitor listener



Place the monitor.properties in the build dir?
Im not sure of that. actually i thought the class looks fr the
monitor.properties file in the same dir as the class is. Try putting the
file in the same place as your jar file.

Actually when i had tried this, I had put the class in the same dir as my
build file (alongwith the properties file) and set the classpath to include
the build dir.

Maybe this should help.


-----Original Message-----
From: Eddie Bernard [ mailto:[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> ]
Sent: Thursday, April 26, 2001 12:52 PM
To: [EMAIL PROTECTED]
Subject: Setting up the BuildMonitor listener


Please excuse the newbie question, but this has been driving me bonkers for
the last couple of days....

I'm attempting to configure the BuildMonitor listener provided by Will
Glozer.  Here are the steps I performed:

1.  Compile the BuildMonitor.java code

2.  Create a jar with the BuildMonitor.class and place it in my Ant lib
directory.  Here are the contents
          0 Thu Apr 19 13:36:16 PDT 2001 META-INF/
    71 Thu Apr 19 13:36:16 PDT 2001 META-INF/MANIFEST.MF
   3515 Wed Apr 25 17:53:40 PDT 2001 ./BuildMonitor.class

3.  Create the monitor.properties file in the same directory as my build
file

Now, when I execute the following command:

ant -logfile build.log -listener BuildMonitor

I get the following error in the log file:
Buildfile: build.xml

BUILD FAILED

Unable to instantiate listener BuildMonitor
--- Nested Exception ---
java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:66)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:85)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:74)
        at java.util.Properties.load(Properties.java:176)
        at BuildMonitor.<init>(BuildMonitor.java:27)
        at java.lang.Class.newInstance0(Native Method)
        at java.lang.Class.newInstance(Class.java:241)
        at org.apache.tools.ant.Main.addBuildListeners(Main.java, Compiled
Code)

        at org.apache.tools.ant.Main.runBuild(Main.java, Compiled Code)
        at org.apache.tools.ant.Main.main(Main.java:149)

Total time: 0 seconds

To me, it appears that it's not finding the monitor.properties.  If so, then

my question is where should this property file be located w.r.t to the
invocation of Ant?

Any ideas?

Reply via email to