2
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;
    }

}

我使用上面的代码在给定的 build.xml 中运行 ant 目标。这个构建 xml 使用 ant-contrib-1.0b1.jar 中的一些任务。

projectHelper.parse(project, buildFile);

此解析方法返回以下异常

构建失败/home/sg40304/Projects/modularization/modules/core/system-testing/build.xml:19:执行此行时发生以下错误:/home/sg40304/Projects/modularization/project-properties.xml:183 :执行此行时发生以下错误:/home/sg40304/Projects/modularization/project-deps.xml:17:执行此行时发生以下错误:/home/sg40304/Tools/build-tools/build/deps .xml:18:无法在 ${ant.home}/lib 中找到 ant-contrib(1.0b2 或更高版本)。ant-contrib 可从http://ant-contrib.sourceforge.net/获得

谁能帮我解决这个问题;(

4

1 回答 1

0

在机器中安装 ant-contrib。

教程在下面的链接中给出

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

于 2012-11-16T08:52:11.247 回答