Execute Ant Script with Ant Tasks Programmatically

I have an Ant Script which uses Ant Task (I use this Task to execute QVTo Transformations).

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:

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:

Exception in thread "AWT-EventQueue-0" C:pathtoAntTaskAntQVTo.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.

The problem could be that Ant Script executed within Java Code isn't run as "Run in the same JRE as project".

I have that Ant Task defined in the plugin.xml which runs the Eclipse Application in the classpath, and also as an extension:

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

  • Download the jar and install Ant (eg, C:AppsToolsapache-ant-1.9).

  • Download the jar and extract Ivy (eg, C:UsersUserNameDownloadsapache-ivy-2.4)

  • Copy C:UsersUserNameDownloadsapache-ivy-2.4ivy-2.4.jar into C:AppsToolsapache-ant-1.9lib.


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

    链接地址: http://www.djcxy.com/p/7720.html

    上一篇: 在Microsoft SQL Server 2005中使用concat MySQL函数?

    下一篇: 以编程方式使用Ant任务执行Ant脚本