首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Ant任务以编程方式执行Ant脚本

通过Ant任务以编程方式执行Ant脚本
EN

Stack Overflow用户
提问于 2017-07-14 00:02:44
回答 2查看 508关注 0票数 0

我有一个使用Ant Task的Ant脚本(我使用这个Task来执行QVTo转换)。

Ant脚本如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="default" xmlns:qvto="http://www.eclipse.org/qvt/1.0.0/Operational">
    <target name="default">

    <taskdef name="http://www.eclipse.org/qvt/1.0.0/Operational:transformation" classname="org.eclipse.m2m.internal.qvt.oml.runtime.ant.QvtoAntTransformationTask">
        <classpath>
           <pathelement location="${basedir}/libAnt/antTasks.jar"/>
        </classpath>
    </taskdef>

    <qvto:transformation uri="platform:/resource/QVToTransformation/transforms/QVTTransformation.qvto">

            <in uri="platform:/resource/QVToTransformation/In/In.ecp" />  
            <out uri="platform:/resource/QVToTransformation/Out/Out.uml" />

            <trace uri="platform:/resource/QVToTransformation/Trace/trace.qvtotrace"
                generate="true" incrementalUpdate="false"  />

        </qvto:transformation>

    </target>
</project>

我在Java中用来执行Ant脚本的代码如下:

代码语言:javascript
复制
File AntFile = new File(this.getClass().getResource("qvto/AntQVTo.xml").getFile());
Project p = new Project();
p.setUserProperty(
    "ant.file", 
    this.getClass().getResource("qvto/AntQVTo.xml").getFile()
    );

p.init();

ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, AntFile);
p.executeTarget(p.getDefaultTarget());

当我运行我的Java代码时的问题是,Ant Task似乎根本无法识别,当我运行时,返回以下错误:

代码语言:javascript
复制
Exception in thread "AWT-EventQueue-0" C:\path\to\AntTask\AntQVTo.xml:5: Problem: failed to create task or type http://www.eclipse.org/qvt/1.0.0/Operational:transformation
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

如果我直接在Eclipse中执行Ant脚本,就不会有任何问题,因为这个Task在Eclipse首选项中默认定义为Ant->Runtime->Tasks。

问题可能是在Java代码中执行的Ant脚本不是“在与项目相同的JRE中运行”。

我在plugin.xml中定义了Ant Task,它在类路径中运行Eclipse Application,也是作为一个扩展:

代码语言:javascript
复制
<extension point="org.eclipse.ant.core.antTasks">
    <antTask
        class="org.eclipse.m2m.internal.qvt.oml.runtime.ant.QvtoAntTransformationTask"
        eclipseRuntime="true"
        headless="true"
        library="libAnt/antTasks.jar"
        name="transformation"
        uri="http://www.eclipse.org/qvt/1.0.0/Operational"
        />
</extension>

有人知道我该如何解决我的问题吗?

提前感谢并致以问候。

EN

回答 2

Stack Overflow用户

发布于 2017-07-14 01:08:49

缺少Apache IVY库,请从此处- apache下载该库,然后将jar复制到ant lib目录中并添加到类路径中。

Ant下载jar并安装Ant(例如,C:\Apps\Tools\apache-ant-1.9).

  • Download该jar并解压缩常春藤(例如,C:\Users\UserName\Downloads\apache-ivy-2.4)

  • Copy C:\Users\UserName\Downloads\apache-ivy-2.4\ivy-2.4.jar C:\Apps\Tools\apache-ant-1.9\lib.

票数 0
EN

Stack Overflow用户

发布于 2017-07-14 08:09:30

你能试着换一下吗?

发自:

代码语言:javascript
复制
<taskdef name="http://www.eclipse.org/qvt/1.0.0/Operational:transformation" classname="org.eclipse.m2m.internal.qvt.oml.runtime.ant.QvtoAntTransformationTask">

至:

代码语言:javascript
复制
<taskdef name="transformation" classname="org.eclipse.m2m.internal.qvt.oml.runtime.ant.QvtoAntTransformationTask">

并删除transformation任务/元素的名称空间前缀。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45085884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档