2
votes
package testrunner.popup.actions;

import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.types.Path;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class AntExecuter {

    public boolean executeAntTask(String buildXmlFileFullPath,String className,String methodName,String mode) {

        System.getProperty("ANT_HOME");

        boolean success = false;

        MessageConsole myConsole = findConsole("Scenario Test");
        MessageConsoleStream out = myConsole.newMessageStream();

        MyLogger myLogger = new MyLogger();
        myLogger.setErrorPrintStream(System.err);
        myLogger.setOutputPrintStream(System.out);
        myLogger.setMessageOutputLevel(Project.MSG_INFO);

        Project project = new Project();
        File buildFile = new File(buildXmlFileFullPath);



        project.setUserProperty("ant.file", buildFile.getAbsolutePath());
        project.addBuildListener(myLogger);


        try {
            project.fireBuildStarted();
            project.init();
            ProjectHelper projectHelper = ProjectHelper.getProjectHelper();
            project.addReference("ant.projectHelper", projectHelper);

            projectHelper.parse(project, buildFile);
            project.setNewProperty("test", className.trim());
            if (!(methodName.equals(""))) {
                project.setNewProperty("method", methodName.trim());
            }
            if ("debug".equals(mode)) {
                project.setNewProperty("debug", "true");
            }   

            project.executeTarget("test");
            project.fireBuildFinished(null);
            project.getBuildListeners();
            out.println("------------- Start Run Test Case -------------");
            out.println( MyLogger.completeMessage);
            out.println("-------------  End Run Test Case  -------------");

            success = true;
        } catch (BuildException buildException) {
            project.fireBuildFinished(buildException);
        }

        return success;
    }

    private MessageConsole findConsole(String name) {
        ConsolePlugin plugin = ConsolePlugin.getDefault();
        IConsoleManager conMan = plugin.getConsoleManager();
        IConsole[] existing = conMan.getConsoles();
        for (int i = 0; i < existing.length; i++)
            if (name.equals(existing[i].getName()))
                return (MessageConsole) existing[i];
        MessageConsole myConsole = new MessageConsole(name, null);
        conMan.addConsoles(new IConsole[] { myConsole });
        return myConsole;
    }

}

I use above code to run ant target in given build.xml.This build xml use some task in ant-contrib-1.0b1.jar.

projectHelper.parse(project, buildFile);

This parse method return below exception

BUILD FAILED /home/sg40304/Projects/modularization/modules/core/system-testing/build.xml:19: The following error occurred while executing this line: /home/sg40304/Projects/modularization/project-properties.xml:183: The following error occurred while executing this line: /home/sg40304/Projects/modularization/project-deps.xml:17: The following error occurred while executing this line: /home/sg40304/Tools/build-tools/build/deps.xml:18: Could not locate ant-contrib (1.0b2 or higher) in ${ant.home}/lib. ant-contrib is available from http://ant-contrib.sourceforge.net/

Can any one help me to resolve this problem ;(

1

1 Answers

0
votes

Install ant-contrib in meachine.

Instructoins are given below link

http://ant-contrib.sourceforge.net/