Eclipse platform provides different useful Ant tasks to interact with the workspace. If you have added any such Ant task (say eclipse.refreshLocal) in your ant build script and started getting "Problem: failed to create task or type eclipse.refreshLocal. Cause: The name is undefined.", then this is the post worth looking into.

Tools and Technologies used in this article :

  1. Spring Tool Suite 3.2
  2. JDK 7

eclipse.refreshLocal is Eclipse Platform provided ant task to refresh a specific folder in the workspace.

eclipse.refreshLocal

This task is a wrapper to the IResource.refreshLocal() method. Example:
<eclipse.refreshLocal resource="MyProject/MyFolder" depth="infinite"/>
resource is a resource path relative to the workspace
depth can be one of the following: zero, one or infinite

Problem

To reproduce the problem, I have written one build.xml file and included eclipse.refreshLocal Ant task to refresh 'src' folder of the dummy project called 'MyProject'.

File : build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="refresh">
  <target name="msg">
      <echo message="Reproducing Problem: failed to create task or type eclipse.refreshLocal"/>
  </target>
  <target name="refresh" depends="msg">
      <eclipse.refreshLocal resource="MyProject/src" depth="infinite"/>
  </target>
</project>

Overall project structure
Overall project structure
Now, if we execute the Ant build script then it'll be failed with following message
Console

Buildfile: C:\projects\self_dev\workspaces\srccodes\examples-codebase\MyProject\build.xml
msg:
     [echo] Reproducing Problem: failed to create task or type eclipse.refreshLocal
refresh:
 
BUILD FAILED
C:\projects\self_dev\workspaces\srccodes\examples-codebase\MyProject\build.xml:7: Problem: failed to create task or type eclipse.refreshLocal
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.
 
 
Total time: 579 milliseconds

Why?

By default, Ant runs in a VM which is different from the VM where Eclipse is already running. As a result, it is not aware of the Eclipse Platform and fails to create that particular task.

Solution

Run Ant inside the same VM where Eclipse workspace is running.

How?

Right click on 'build.xml' and from context menu Run As --> Ant Build...
Run As --> Ant Build...
Go to JRE tab. Select Run in the same JRE as the workspace radio button. Click Run button.
Run in the same JRE as the workspace
This time Ant build will be successful.
Console:

Buildfile: C:\projects\self_dev\workspaces\srccodes\examples-codebase\MyProject\build.xml
 
msg:
        [echo] Reproducing Problem: failed to create task or type eclipse.refreshLocal
 
refresh:
BUILD SUCCESSFUL
Total time: 437 milliseconds

We can configure Ant to Run in the same JRE as the workspace using External Tools Configurations... dialog as well.
Run As --> External Tools Configurations...

External Tools Configurations...
Run in the same JRE as the workspace

Download SrcCodes

MyProject: GitHub or zip

References