在我的插件中调用ant任务并将输出重定向到控制台

时间:2012-02-29 15:32:02

标签: ant eclipse-plugin osgi

在我的eclipse插件中,我正在尝试执行Ant任务并将输出重定向到eclipse控制台 这是我的代码:

    AntRunner runner = new AntRunner();
    runner.addBuildLogger("myplugin.AntLogger");        

    String projectPath = getProject().getLocation().toString();
    String buildFileLocation = projectPath + File.separator + "build.xml";
    runner.setBuildFileLocation(buildFileLocation);
    runner.setExecutionTargets(new String[] { "build-for-device" });

AntLogger代码是:

public class AntLogger extends org.apache.tools.ant.DefaultLogger {

private IOConsole console = new IOConsole("Ant", null);
private IOConsoleOutputStream outputStream = console.newOutputStream();

public AntLogger() {
    ConsolePlugin.getDefault().getConsoleManager()
            .addConsoles(new IConsole[] { console });
}

@Override
protected void printMessage(String message, PrintStream stream, int priority) {
    super.printMessage(message, stream, priority);
    try {
        outputStream.write(message + "\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

我将ant.jar添加到plugin.xml

<extension
     point="org.eclipse.ant.core.extraClasspathEntries">
  <extraClasspathEntry
        library="ant.jar">
  </extraClasspathEntry>
</extension>

无论我做什么,我都会继续上课,但没有找到例外:

java.lang.NoClassDefFoundError: org/apache/tools/ant/DefaultLogger
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.defineClass(DefaultClassLoader.java:188)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClassHoldingLock(ClasspathManager.java:626)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClass(ClasspathManager.java:608)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findClassImpl(ClasspathManager.java:562)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClassImpl(ClasspathManager.java:486)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:459)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216)
    at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:400)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:476)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.eclipse.osgi.internal.loader.BundleLoader.loadClass(BundleLoader.java:345)
    at org.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:229)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadClass(AbstractBundle.java:1207)
    at org.eclipse.ant.core.AntCorePreferences$WrappedClassLoader.findClass(AntCorePreferences.java:116)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.eclipse.ant.internal.core.AntClassLoader.loadClassPlugins(AntClassLoader.java:69)
    at org.eclipse.ant.internal.core.AntClassLoader.findClass(AntClassLoader.java:47)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.eclipse.ant.internal.core.ant.InternalAntRunner.createLogger(InternalAntRunner.java:747)
    at org.eclipse.ant.internal.core.ant.InternalAntRunner.addBuildListeners(InternalAntRunner.java:202)
    at org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:572)
    at org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:498)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.ant.core.AntRunner.run(AntRunner.java:378)
    at org.eclipse.ant.core.AntRunner.run(AntRunner.java:475)

2 个答案:

答案 0 :(得分:3)

在敲了敲我之后,我终于找到了解决办法:

AntLogger类不能成为插件项目的一部分。 我将AntLogger移动到另一个名为EclipseAntLogger的java项目中,然后我将项目jar添加到插件扩展点,如下所示:

<extension
     point="org.eclipse.ant.core.extraClasspathEntries">
  <extraClasspathEntry
        library="lib/EclipseAntLogger.jar">
  </extraClasspathEntry>
</extension>

答案 1 :(得分:0)

ant.jar和其他必要的jar添加到AntRunner实例是否有帮助?

public void setCustomClasspath(URL[] customClasspath)

#wildstabinthedark