简体   繁体   English

以编程方式使用Ant任务执行Ant脚本

[英]Execute Ant Script with Ant Tasks Programmatically

I have an Ant Script which uses Ant Task (I use this Task to execute QVTo Transformations). 我有一个使用Ant任务的Ant脚本(我使用此任务执行QVTo转换)。

The Ant Script is the following one: 蚂蚁脚本如下:

<?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>

The code I use in Java to execute Ant Script is the following one: 我在Java中用来执行Ant脚本的代码如下:

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());

The problem when I run my Java code is that it seems that Ant Task is not recognized at all, when I run the following error is returned: 运行Java代码时出现的问题是,当我运行以下错误时,似乎根本无法识别Ant Task:

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

I don't have any problem if I execute my Ant Script directly in Eclipse, as this Task is by default defined in the Eclipse Preferences, into Ant->Runtime->Tasks. 如果直接在Eclipse中执行我的Ant脚本,我没有任何问题,因为默认情况下,此任务是在Eclipse首选项中定义的,并变成了Ant-> Runtime-> Tasks。

The problem could be that Ant Script executed within Java Code isn't run as "Run in the same JRE as project". 问题可能是在Java代码中执行的Ant脚本未按“在与项目相同的JRE中运行”的方式运行。

I have that Ant Task defined in the plugin.xml which runs the Eclipse Application in the classpath, and also as an extension: 我在plugin.xml中定义了该Ant任务,该插件在类路径中运行Eclipse Application,并且作为扩展:

<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>

Does anyone know how could I fix my issue? 有谁知道我该如何解决我的问题?

Thanks in advance and regards. 在此先感谢和问候。

missing Apache IVY library, Download this library from here - apache and Copy the jar in your ant lib directory and add into class path. 缺少Apache IVY库,请从此处下载该库-apache并将jar复制到您的ant lib目录中,并添加到类路径中。

  • Download the jar and install Ant (eg, C:\\Apps\\Tools\\apache-ant-1.9). 下载jar并安装Ant(例如C:\\ Apps \\ Tools \\ apache-ant-1.9)。

  • Download the jar and extract Ivy (eg, C:\\Users\\UserName\\Downloads\\apache-ivy-2.4) 下载jar并提取常春藤(例如C:\\ Users \\ UserName \\ Downloads \\ apache-ivy-2.4)

  • Copy C:\\Users\\UserName\\Downloads\\apache-ivy-2.4\\ivy-2.4.jar into C:\\Apps\\Tools\\apache-ant-1.9\\lib. 将C:\\ Users \\ UserName \\ Downloads \\ apache-ivy-2.4 \\ ivy-2.4.jar复制到C:\\ Apps \\ Tools \\ apache-ant-1.9 \\ lib。

Can you try changing 你能尝试改变吗

From: 从:

<taskdef name="http://www.eclipse.org/qvt/1.0.0/Operational:transformation" classname="org.eclipse.m2m.internal.qvt.oml.runtime.ant.QvtoAntTransformationTask">

To: 至:

<taskdef name="transformation" classname="org.eclipse.m2m.internal.qvt.oml.runtime.ant.QvtoAntTransformationTask">

And remove the namespace prefix to transformation task/element. 并删除transformation任务/元素的名称空间前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM