通过eclipse插件执行ant Target

时间:2012-11-16 08:44:05

标签: java ant eclipse-plugin

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目标。这个build 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/

获得

任何人都可以帮我解决这个问题;(

1 个答案:

答案 0 :(得分:0)

在meachine中安装ant-contrib。

Instructoins在链接

下面给出

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