You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Dominique Devienne <DD...@lgc.com> on 2002/04/11 19:49:30 UTC

RE: My solution on how to commands asyncronously in Window s

Look in the archives (wherever they are) for posts with the subject "RE:
Running <exec> tasks asynchronously on UNIX also on Windows?" for a patch to
ANT that supports it already. It just extends <exec> with a new attribute.
--DD

-----Original Message-----
From: Marcus Ludvigson [mailto:Marcus.Ludvigson@intentia.se] 
Sent: Thursday, April 11, 2002 9:33 AM
To: Ant Users List
Subject: My solution on how to <exec> commands asyncronously in Windows

Hi fellow Ant'ers.

I originally started reading this list because I had problems with 
blocking tasks when
using Ant, unfortunatly there seems like more have the same problem. 
This is my first post and my solution to this problem.
I have modified code from Apache's Cactus-project to start a blocking task 
(a server for example) and fork it of in a thread so Ant can continue with 
other task. 
Hope this helps. /Marcus 
This i how my solution looks like in the build.xml-file:
--------%<-----[build.xml]---------------- 
<taskdef name="startServer" classname="org.myorg.StartServerTask" 
classpath="${classpath}"/>

<target name="BlockingTask">
            <exec dir="C:\Server_dir" executable="cmd.exe">
            <arg line="/c start server.cmd"/>
            </exec>
</target>

<target name="startServerAsync">
            <startServer startTarget="BlockingTask"/>
</target>
--------%<-----[build.xml]---------------- 
--------%<-----[StartServerTask.java]---------------- 
package org.myorg;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * An Ant Task that does the following :
 * 
 *  create a java thread,
 *
 *  start another Ant target in that thread. This target must be a
 *  blocking target. 
 *
 *
 */
public class StartServerTask extends Task {
    /**
     * Helper class
     */
    private StartServerHelper helper;

    /**
     * Initialization
     */
    public void init() {
        this.helper = new StartServerHelper(this);
    }

    /**
     * @see Task#execute()
     */
    public void execute() throws BuildException {
        this.helper.execute();
    }

    /**
     * Ant will automatically call this method when the "startTarget" 
attribute
     * of our task is used.
     *
     * @param theStartTarget the Ant target to call
     */
    public void setStartTarget(String theStartTarget) {
        this.helper.setStartTarget(theStartTarget);
    }

}
--------%<-----[StartServerTask.java]----------- 
--------%<-----[StartServerHelper.java]----------- 
package org.myorg;;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.CallTarget;

/**
 * A helper class for an Ant Task that does the following :
 *
 *  create a java thread,
 *
 *  start another Ant target in that thread. This target must be a
 *  blocking target. 
 *
 *
 */
public class StartServerHelper implements Runnable {
    /**
     * The Ant target name that will start the blocking target.
     */
    private String startTarget;

    /**
     * The tasks that wraps around this helper class
     */
    private Task task;

    /**
     * @param theTask the Ant task that is calling this helper
     */
    public StartServerHelper(Task theTask) {
        this.task = theTask;
    }

    /**
     * @see Task#execute()
     */
    public void execute() throws BuildException {

        // Verify that a start target has been specified
        if (this.startTarget == null) {
            throw new BuildException("A startTarget Ant target name must " 
+
                    "be specified");
        }

        // Call the target in another thread. The called
        // target must be blocking.
        Thread thread = new Thread(this);

        thread.start();

        // We're done ... Ant will continue processing other tasks
    }

    /**
     * The thread that calls the Ant target that starts, for example, a 
server.
     * Must be a blocking target.
     */
    public void run() {
        // Call the Ant target using the "antcall" task.
        CallTarget callee;
        callee = (CallTarget) 
(this.task.getProject().createTask("antcall"));
        callee.setOwningTarget(this.task.getOwningTarget());
        callee.setTaskName(this.task.getTaskName());
        callee.setLocation(this.task.getLocation());

        callee.init();

        callee.setTarget(this.startTarget);
        callee.execute();

        // Should never reach this point as the target is blocking, unless 
the
        // server is stopped.
    }

    /**
     * @param theStartTarget the Ant target to call
     */
    public void setStartTarget(String theStartTarget) {
        this.startTarget = theStartTarget;
    }

}

--------%<-----[StartServerHelper.java]----------- 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>