Return-Path: Mailing-List: contact user-help@ant.apache.org; run by ezmlm Delivered-To: mailing list user@ant.apache.org Received: (qmail 51533 invoked from network); 7 Mar 2003 17:07:01 -0000 Received: from k101-11.bas1.dbn.dublin.eircom.net (HELO corvil.com.) (159.134.101.11) by daedalus.apache.org with SMTP; 7 Mar 2003 17:07:01 -0000 Received: from preilly.local.corvil.com (preilly.local.corvil.com [172.18.1.173]) by corvil.com. (8.12.5/8.12.5) with ESMTP id h27H6hpg032243; Fri, 7 Mar 2003 17:06:45 GMT (envelope-from peter.reilly@corvil.com) From: peter reilly Organization: corvil To: user@ant.apache.org, ant-contrib-developers@lists.sourceforge.net Subject: A couple of usefull tasks Date: Fri, 7 Mar 2003 17:07:33 +0000 User-Agent: KMail/1.4.3 MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="------------Boundary-00=_L82ETVXTJ3ONW9HHPDL0" Message-Id: <200303071707.33208.peter.reilly@corvil.com> X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N --------------Boundary-00=_L82ETVXTJ3ONW9HHPDL0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Included is a couple of useful tasks. I am currently moving a load of makefiles to ant and found that there were a number of make targets like: generated.h generated.cpp : mib.mib ../include/common.h mibgen -i ../include mib.mib -c generated.cpp mibgen -i ../include mib.mib -h generated.h In other words multiple targets depend on multiple sources. This is possible to do in ant with uptodate and exec or script, however it is not nice. The two tasks I use are outoutdate and shellscript. Shellscript is a modification of a task submitted to ant-contrib by stephan beal (sgbeal= ) patch number 547018 (modified to use a temp file and not a fixed file) and outofdate is a modification of uptodate which allows multible targets and contains an embedded do element. Using these tasks the make extract above can be encoded as (assuming defines for gen.dir, include.dir and mib.dir): mibgen -i ../include mib.mib -c generated.cpp mibgen -i ../include mib.mib -h generated.h This is a bit more verbose than the make extract, but it is quite easy to understand. Peter --------------Boundary-00=_L82ETVXTJ3ONW9HHPDL0 Content-Type: text/x-java; charset="us-ascii"; name="ShellScriptTask.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ShellScriptTask.java" /* * The Apache Software License, Version 1.1 * * Copyright (c) 2002 Ant-Contrib project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Ant-Contrib project (http://sourceforge.net/projects/ant-contrib)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The name Ant-Contrib must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact * ant-contrib-developers@lists.sourceforge.net. * * 5. Products derived from this software may not be called "Ant-Contrib" * nor may "Ant-Contrib" appear in their names without prior written * permission of the Ant-Contrib project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE ANT-CONTRIB PROJECT OR ITS * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== */ package net.sf.antcontrib.logic; import org.apache.tools.ant.Task; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.taskdefs.Execute; import org.apache.tools.ant.taskdefs.ExecTask; import java.lang.StringBuffer; import java.io.*; /** A generic front-end for passing "shell lines" to any application which can accept a filename containing script input (bash, perl, csh, tcsh, etc.). An example: <taskdef name="shellscript" classname="com.corvil.anttasks.ShellScript"/>
<shellscript shell="bash">
ls -1 /tmp
cd /home
pwd
</shellscript>
Attributes:
shell = name of executable used to run script code. Defaults to bash.
*/ public class ShellScriptTask extends ExecTask { private StringBuffer script = new StringBuffer(); private String shell = "bash"; private File tmpFile; /** * Adds s to the lines of script code. */ public void addText(String s) { script.append(getProject().replaceProperties(s)); } /** * Sets script code to s. */ public void setText(String s) { script.append(s); } /** * Sets the shell used to run the script. * @param shell the shell to use (bash is default) */ public void setShell(String shell) { this.shell = shell; } /** * execute the task */ public void execute() throws BuildException { try { tmpFile = File.createTempFile("script", null); } catch (IOException ex) { throw new BuildException(ex); } try { String src = script.toString(); if (src != null) { this.writeScript(src); this.createArg().setValue(tmpFile.getAbsolutePath()); } super.setExecutable(shell); super.execute(); } finally { if (! tmpFile.delete()) { log("Non-fatal error: could not delete temporary file [" + tmpFile.getAbsolutePath()); } } } /** * Writes the script lines to a temp file. */ protected void writeScript(String scr) throws BuildException { try { java.io.FileOutputStream os = new java.io.FileOutputStream( tmpFile); os.write(scr.getBytes(), 0, scr.length()); os.close(); } catch( Exception e ) { throw new BuildException( e ); } } } --------------Boundary-00=_L82ETVXTJ3ONW9HHPDL0 Content-Type: text/x-java; charset="us-ascii"; name="OutOfDate.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="OutOfDate.java" /* * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Ant", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ package org.apache.tools.ant.taskdefs; import java.io.*; import java.util.*; import org.apache.tools.ant.*; import org.apache.tools.ant.taskdefs.*; import org.apache.tools.ant.taskdefs.condition.*; import org.apache.tools.ant.util.*; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Mapper; /** * Task to help in calling tasks if generated files are older * than source files. * Sets a given property or runs an internal task. * * Based on and uses * org.apache.org.apache.tools.ant.taskdefs.UpToDate */ public class OutOfDate extends Task implements Condition { public static class NameFile { private File file; public void setName(File file) { this.file = file; } public File getFile() { return file; } } private Sequential doTask = null; private UpToDate upToDate = new UpToDate(); private String property; private String value = "true"; private Vector sourceFiles = new Vector(); private Vector sourceFileSets = new Vector(); private Vector targetFiles = new Vector(); private Mapper mapper; /** * Defines the FileNameMapper to use (nested mapper element). */ public Mapper createMapper() { mapper = upToDate.createMapper(); return mapper; } /** * The property to set if any of the target files are outofdate with * regard to any of the source files. * * @param property the name of the property to set if Target is outofdate. */ public void setProperty(String property) { this.property = property; } /** * The value to set the named property to the target files * are outofdate * * @param value the value to set the property */ public void setValue(String value) { this.value = value; } /** * The file which must be more outofdate than (each of) the source file(s) * if the property is to be set. * * @param file the file we are checking against. */ public void setTargetFile(File targetFile) { targetFiles.addElement(targetFile); } /** * The file that must be newer than the target files * if the property is to be set. * * @param file the file we are checking against the target files. */ public void setSrcfile(File sourceFile) { this.sourceFiles.addElement(sourceFile); } /** * Nested file that must be newer than the target files * if the property is to be set. * * @param file the file we are checking against the target files. */ public void addConfiguredSrcFile(NameFile nameFile) { if (nameFile.getFile() == null) throw new BuildException("Need to specify the name"); sourceFiles.addElement(nameFile.getFile()); } /** * Nested file set of source files. * * @param file the file we are checking against the target files. */ public void addSrcfiles(FileSet fs) { sourceFileSets.addElement(fs); this.upToDate.addSrcfiles(fs); } /** * Nested target file. * * @param file the file we are checking against the source files. */ public void addConfiguredTargetFile(NameFile nameFile) { if (nameFile.getFile() == null) throw new BuildException("Need to specify the name", getLocation()); targetFiles.addElement(nameFile.getFile()); } /** * Embedded do sequential. */ public void addDo(Sequential doTask) { if (this.doTask != null) { throw new BuildException( "You must not nest more that one " + " into "); } this.doTask = doTask; } /** * Evaluate (all) target and source file(s) to * see if the target(s) is/are outoutdate. */ public boolean eval() { upToDate.setProject(getProject()); upToDate.setTaskName(getTaskName()); if (sourceFileSets.size() == 0 && sourceFiles.size() == 0) throw new BuildException( "At least one srcfile or a nested " + " or element must be set."); if (targetFiles.size() == 0) throw new BuildException("At least one targetfile or a nested " + " element must be set."); // if any of the source files are not present - throw an exception for (Enumeration e = sourceFiles.elements(); e.hasMoreElements();) { File sourceFile = (File) e.nextElement(); if (! sourceFile.exists()) { throw new BuildException(sourceFile.getAbsolutePath() + " not found."); } } for (Enumeration e = targetFiles.elements(); e.hasMoreElements();) { File targetFile = (File) e.nextElement(); if (targetNeedsGen(targetFile)) return true; } return false; } private boolean targetNeedsGen(File targetFile) { upToDate.setTargetFile(targetFile); if (sourceFiles.size() == 0) return (! upToDate.eval()); for (Enumeration e = sourceFiles.elements(); e.hasMoreElements();) { upToDate.setSrcfile((File) e.nextElement()); if (! upToDate.eval()) return true; } return false; } /** * Sets property to true and/or executes embedded do * if any of the target file(s) do not have a more recent timestamp * than (each of) the source file(s). */ public void execute() throws BuildException { if (property == null && doTask == null) throw new BuildException( "property attribute or do element is required."); if (! eval()) return; if (property != null) this.getProject().setNewProperty(property, value); if (doTask != null) doTask.perform(); } } --------------Boundary-00=_L82ETVXTJ3ONW9HHPDL0--