以Java编程执行ANT脚本

时间:2011-12-23 16:27:26

标签: java eclipse ant

我在eclipse中构建了一个swing gui,它应该运行一堆先前开发的代码,其中一部分涉及运行ant来构建代码。当我在GUI之外运行任何脚本(在原始项目中)时,ant正确执行并构建项目。但是,当我尝试以编程方式运行ant时,它会抛出看起来像项目没有必要的.jars的错误。下面列出了代码,build.xml的顶部和错误。

代码块

File buildFile = new File("lib\\ePlay\\build.xml");
    Project p = new Project();
    p.setUserProperty("ant.file", buildFile.getAbsolutePath());
    DefaultLogger consoleLogger = new DefaultLogger();
    consoleLogger.setErrorPrintStream(System.err);
    consoleLogger.setOutputPrintStream(System.out);
    consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
    p.addBuildListener(consoleLogger);

    try {
        p.fireBuildStarted();
        p.init();
        ProjectHelper helper = ProjectHelper.getProjectHelper();
        p.addReference("ant.projectHelper", helper);
        helper.parse(p, buildFile);
        p.executeTarget(p.getDefaultTarget());
        p.fireBuildFinished(null);
    } catch (BuildException e) {
        p.fireBuildFinished(e);
    }

在build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="EPlay" xmlns:ivy="antlib:org.apache.ivy.ant" default="resolve">

<dirname file="${ant.file}" property="ant.dir" />
<property name="scm.user" value="scmrel"/>
<property name="scm.user.key" value="/.ssh/scmrel/id_rsa"/>
<property name="ivy.jar" value="ivy-2.0.0.jar" />
<property name="ivy.settings.dir" value="${ant.dir}/ivysettings" />
<property name="VERSION" value="LATEST" />
<property name="tasks.dir" value="${ant.dir}/.tasks" />
<property name="deploy.dir" value="${ant.dir}/deploy" />
...
<!-- retrieve the dependencies using Ivy -->
<target name="resolve" depends="_loadantcontrib,_getivy" description=" retrieve the dependencies with Ivy">
  <ivy:settings file="${ivy.settings.dir}/ivysettings.xml" />
  <ivy:resolve file="${ant.dir}/ivy.xml" transitive="false" />
  <ivy:retrieve pattern="${deploy.dir}/[conf]/[artifact].[ext]"/>
</target>

错误

resolve:
BUILD FAILED
H:\eclipse\CLDeploySwing\lib\ePlay\build.xml:66: Problem: failed to create task or type antlib:org.apache.ivy.ant:settings
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet

This appears to be an antlib declaration. 
Action: Check that the implementing library exists in one of:
        -ANT_HOME\lib
        -the IDE Ant configuration dialogs


Total time: 0 seconds

我查看了我的ant安装,出现一切都在那里。就像我说的,如果build.xml在此应用程序之外运行,原始项目将成功构建。谢谢你的帮助

3 个答案:

答案 0 :(得分:1)

我建议这是causde,因为你的java程序没有相同的类路径,它正在运行,正常的ant构建 - 因此ANT_HOME不是正确的。

您可以通过将正确的环境变量传递到JVM中,或者只是调用System.getProperty(“ANT_HOME”)来确保这是正确的,以查看您的ANT_HOME实际驻留的位置。

答案 1 :(得分:0)

我认为您的项目${basedir}未正确计算。

将此行添加到build.xml

<echo message="basedir='${basedir}'/>

看这一行

File buildFile = new File("lib\\ePlay\\build.xml");

它可能是2级(我知道文档说它应该是build.xml的父目录,但是你没有从命令行运行)。

答案 2 :(得分:0)

而不是使用在项目声明中使用命名空间的新方法来加载常春藤库,而不是使用旧学校。

<path id="ivy.lib.path">
    <fileset dir="path/to/dir/with/ivy/jar" includes="*.jar"/>
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
         uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>

我在Gradle中做了一些需要这个的事情

ant.taskdef(name: 'ivy-retrieve', classname: 'org.apache.ivy.ant.IvyRetrieve', classpath: '...path to ivy jar.../ivy-2.2.0.jar')

在蚂蚁中就像是

<taskdef name="ivy-retrieve" classname="org.apache.ivy.ant.IvyRetrieve"/>

我知道它更笨拙,并不像包含名称空间声明一样好,但它确实消除了关于哪些类加载类路径的库的混淆。