I'd like to take a slightly different angle on the <import> issue by getting 
some thoughts on this little experimental task I wrote. I haven't cleaned it 
up at all (lots of unused imports, inner classes etc). Anyway it lets you 
override any target in the build. e.g. This build file

<project name="override" default="test">

  <taskdef name="override" 
classname="org.apache.tools.ant.taskdefs.Override"/>
  <taskdef name="super" 
classname="org.apache.tools.ant.taskdefs.Override$Super"/>

  <override target="test">
    <echo message="pre"/>
    <super/>
    <echo message="post"/>
  </override>

  <override target="test">
    <echo message="pre2"/>
    <super/>
    <echo message="post2"/>
  </override>

  <target name="test">
    <echo message="test"/>
  </target>

</project>

produces the following output
Buildfile: build.xml

test:
     [echo] pre2
     [echo] pre
     [echo] test
     [echo] post
     [echo] post2

BUILD SUCCESSFUL

:-)

I think with a renaming <import> task this would let you override any part of 
the build easily.

BTW, note that simple renaming of targets is not enough. Any use of a target 
name needs to be updated too (e.g. <antcall>). Can be done (at time of use) 
but may require tasks knowing their import prefix. The import task will need 
this to do cascade renaming.

Anyway, just thought I'd throw it out there. Override might even be useful in 
an entity includes approach.

Comments?

Conor
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 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 "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 [EMAIL PROTECTED]
 *
 * 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
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.TaskContainer;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.types.PropertySet;
import org.apache.tools.ant.util.FileUtils;
import java.util.Map;

/*
 * @author Conor MacNeill
 */
public class Override extends Task implements TaskContainer {
    public static class OverrideTarget extends Target {
        private Target original;
    }

    private String targetName;

    private OverrideTarget override = new OverrideTarget();

    public void setTarget(String targetName) {
        this.targetName = targetName;
    }

    public void addTask(Task task) {
        override.addTask(task);
        task.setOwningTarget(override);
    }

    public void execute() {
        Project project = getProject();
        Map targets = project.getTargets();

        override.original = (Target) targets.get(targetName);
        override.setProject(project);
        override.setName(targetName);
        targets.put(targetName, override);

        for (Enumeration e = override.original.getDependencies(); e.hasMoreElements();) {
            String dependency = (String) e.nextElement();
            override.addDependency(dependency);
        }
    }

    public static class Super extends Task {
        public void execute() {
            Target owner = getOwningTarget();
            if (owner instanceof OverrideTarget) {
                Target original = ((OverrideTarget) owner).original;
                original.execute();
            }
        }
    }


}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to