Hello guys, In a macrodef definition,how can i set option2 from a macrodef based on option1 from the same macrodef?Can i use IF Task from ant-contrib.?
Thanks for any help. Hind. -----Original Message----- From: Ivan Ivanov [mailto:[EMAIL PROTECTED] Sent: Wed 6/15/2005 7:14 AM To: [email protected] Subject: RE: Mail a nice pretty html build log... --- [EMAIL PROTECTED] wrote: > > Hello > > Kindly post this information to the group. > > Thanks > srikrishna Hello, Below you will find MailAttachLogger.java. Here are instruction how to test it: 0) Compile and jar MailAttachLogger.java 1) Copy the jar to ${user.home}/.ant/lib 2) Download Java Mail and copy mail.jar to ${user.home}/.ant/lib 3) Download Java Activation Framework and copy avtivation.jar to ${user.home}/.ant/lib 2) and 3) are well described in Library Dependencies Section in Ant manual. 4) Create an Ant build script: <!-- mail.xml --> <project name="mail.logger" default="test-success"> <target name="-init"> <loadproperties srcfile="MailLogger.properties"/> <echoproperties prefix="MailLogger"/> </target> <target name="test-success" depends="-init"> <echo file="${basedir}/success.log">Build Successful</echo> </target> <target name="test-failure" depends="-init"> <echo file="${basedit}/failure.log">Build will fail</echo> <fail message="Deliberate failure"/> </target> </project> 5) The above build script loads the following properties file. Fill its values with real ones. # MailLogger.properties MailLogger.mailhost=smtp.myorg.com MailLogger.user=myname MailLogger.password=mypass [EMAIL PROTECTED] [EMAIL PROTECTED] MailLogger.failure.notify=true MailLogger.success.notify=true [EMAIL PROTECTED] [EMAIL PROTECTED] MailLogger.failure.subject=Build failed MailLogger.success.subject=Build succeeded MailLogger.success.attachments=success.log MailLogger.failure.attachments=failure.log 6) After we have adjusted the values in the above properties file we can test it with the following two commands: ant -logger bg.unisofia.antutils.listeners.MailAttachLogger -f mail.xml and ant -logger bg.unisofia.antutils.listeners.MailAttachLogger test-failure -f mail.xml 7) Here is MailAttachLogger: package bg.unisofia.antutils.listeners; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.Enumeration; import java.util.Hashtable; import java.util.Properties; import java.util.StringTokenizer; import java.util.Vector; import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.DefaultLogger; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.email.EmailAddress; import org.apache.tools.ant.taskdefs.email.Mailer; import org.apache.tools.ant.taskdefs.email.Message; import org.apache.tools.ant.util.DateUtils; import org.apache.tools.ant.util.StringUtils; import org.apache.tools.mail.MailMessage; public class MailAttachLogger extends DefaultLogger { /** Buffer in which the message is constructed prior to sending */ private StringBuffer buffer = new StringBuffer(); /** * Sends an e-mail with the log results. * * @param event the build finished event */ public void buildFinished(BuildEvent event) { super.buildFinished(event); Project project = event.getProject(); Hashtable properties = project.getProperties(); // overlay specified properties file (if any), which overrides project // settings Properties fileProperties = new Properties(); String filename = (String) properties.get("MailLogger.properties.file"); if (filename != null) { InputStream is = null; try { is = new FileInputStream(filename); fileProperties.load(is); } catch (IOException ioe) { // ignore because properties file is not required } finally { if (is != null) { try { is.close(); } catch (IOException e) { // ignore } } } } for (Enumeration e = fileProperties.keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); String value = fileProperties.getProperty(key); properties.put(key, project.replaceProperties(value)); } boolean success = (event.getException() == null); String prefix = success ? "success" : "failure"; try { boolean notify = Project.toBoolean(getValue(properties, prefix + ".notify", "on")); if (!notify) { return; } String mailhost = getValue(properties, "mailhost", "localhost"); int port = Integer.parseInt(getValue(properties, "port", String.valueOf(MailMessage.DEFAULT_PORT))); String user = getValue(properties, "user", ""); String password = getValue(properties, "password", ""); boolean ssl = Project.toBoolean(getValue(properties, "ssl", "off")); String from = getValue(properties, "from", null); String replytoList = getValue(properties, "replyto", ""); String toList = getValue(properties, prefix + ".to", null); String subject = getValue(properties, prefix + ".subject", (success) ? "Build Success" : "Build Failure"); String attachmentsList = getValue(properties, prefix + ".attachments", ""); project.log("raw attachments are " + attachmentsList, Project.MSG_VERBOSE); Vector attachments = vectorizeAttachments(project, attachmentsList); project.log("resolved attachments' names are " + attachments, Project.MSG_VERBOSE); if (user.equals("") && password.equals("") && !ssl && attachments.size() == 0 ) { sendMail(mailhost, port, from, replytoList, toList, subject, buffer.substring(0)); } else { sendMimeMail(event.getProject(), mailhost, port, user, password, ssl, from, replytoList, toList, subject, buffer.substring(0), attachments); } } catch (Exception e) { System.out.println("MailLogger failed to send e-mail!"); e.printStackTrace(System.err); } } /** * Receives and buffers log messages. * * @param message the message being logger */ protected void log(String message) { buffer.append(message).append(StringUtils.LINE_SEP); } /** * Gets the value of a property. * * @param properties Properties to obtain value from * @param name suffix of property name. "MailLogger." will be * prepended internally. * @param defaultValue value returned if not present in the properties. * Set to null to make required. * @return The value of the property, or default value. * @exception Exception thrown if no default value is specified and the * property is not present in properties. */ private String getValue(Hashtable properties, String name, String defaultValue) throws Exception { String propertyName = "MailLogger." + name; String value = (String) properties.get(propertyName); if (value == null) { value = defaultValue; } if (value == null) { throw new Exception("Missing required parameter: " + propertyName); } return value; } /** * Send the mail * @param mailhost mail server * @param port mail server port number * @param from from address * @param replyToList comma-separated replyto list * @param toList comma-separated recipient list * @param subject mail subject * @param message mail body * @exception IOException thrown if sending message fails */ private void sendMail(String mailhost, int port, String from, String replyToList, String toList, String subject, String message) throws IOException { MailMessage mailMessage = new MailMessage(mailhost, port); mailMessage.setHeader("Date", DateUtils.getDateForHeader()); mailMessage.from(from); if (!replyToList.equals("")) { StringTokenizer t = new StringTokenizer(replyToList, ", ", false); while (t.hasMoreTokens()) { mailMessage.replyto(t.nextToken()); } } StringTokenizer t = new StringTokenizer(toList, ", ", false); while (t.hasMoreTokens()) { mailMessage.to(t.nextToken()); } mailMessage.setSubject(subject); PrintStream ps = mailMessage.getPrintStream(); ps.println(message); mailMessage.sendAndClose(); } /** * Send the mail (MimeMail) * @param project current ant project * @param host mail server * @param port mail server port number * @param user user name for SMTP auth * @param password password for SMTP auth * @param ssl if true send message over SSL * @param from from address * @param replyToString comma-separated replyto list * @param toString comma-separated recipient list * @param subject mail subject * @param message mail body * @param attachments vector of Files to be attached */ private void sendMimeMail(Project project, String host, int port, String user, String password, boolean ssl, String from, String replyToString, String toString, String subject, String message, Vector attachments) { // convert the replyTo string into a vector of emailaddresses Mailer mailer = null; try { mailer = (Mailer) Class.forName("org.apache.tools.ant.taskdefs.email.MimeMailer") .newInstance(); } catch (Throwable e) { log("Failed to initialise MIME mail: " + e.getMessage()); return; } Vector replyToList = vectorizeEmailAddresses(replyToString); mailer.setHost(host); mailer.setPort(port); mailer.setUser(user); mailer.setPassword(password); mailer.setSSL(ssl); Message mymessage = new Message(message); mymessage.setProject(project); mailer.setMessage(mymessage); mailer.setFrom(new EmailAddress(from)); mailer.setReplyToList(replyToList); Vector toList = vectorizeEmailAddresses(toString); mailer.setToList(toList); mailer.setCcList(new Vector()); mailer.setBccList(new Vector()); mailer.setFiles(attachments); mailer.setSubject(subject); mailer.send(); } private Vector vectorizeEmailAddresses(String listString) { Vector emailList = new Vector(); StringTokenizer tokens = new StringTokenizer(listString, ","); while (tokens.hasMoreTokens()) { emailList.addElement(new EmailAddress(tokens.nextToken())); } return emailList; } private Vector vectorizeAttachments(Project project, String attachmentsList) { Vector v = new Vector(); StringTokenizer tokens = new StringTokenizer(attachmentsList, ","); while (tokens.hasMoreTokens()) { String filename = project.replaceProperties(tokens.nextToken()); File file = project.resolveFile(filename); if (file.exists() && file.canRead()) { v.add(file); } } return v; } } Regards Ivan __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This message is for the designated recipient only and may contain privileged, proprietary, or otherwise private information. If you have received it in error, please notify the sender immediately and delete the original. Any other use of the email by you is prohibited.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
