tags:

views:

981

answers:

2

I am trying to get ant4eclipse to work and I have used ant a bit, but not much above a simple scripting language. We have multiple source folders in our Eclipse projects so the example in the ant4eclipse documentation needs adapting:

Currently I have the following:

<target name="build">

  <!-- resolve the eclipse output location -->
  <getOutputpath property="classes.dir" workspace="${workspace}" projectName="${project.name}" />

  <!-- init output location -->
  <delete dir="${classes.dir}" />
  <mkdir dir="${classes.dir}" />

  <!-- resolve the eclipse source location -->
  <getSourcepath pathId="source.path" project="." allowMultipleFolders='true'/>

  <!-- read the eclipse classpath -->
  <getEclipseClasspath pathId="build.classpath" 
                          workspace="${workspace}" projectName="${project.name}" />

  <!-- compile -->
  <javac destdir="${classes.dir}" classpathref="build.classpath" verbose="false" encoding="iso-8859-1">
   <src refid="source.path" />
   </javac>

  <!-- copy resources from src to bin -->
  <copy todir="${classes.dir}" preservelastmodified="true">
    <fileset refid="source.path">
     <include name="**/*"/>
     <!--
     patternset refid="not.java.files"/>
     -->
    </fileset>
  </copy>
</target>

The task runs successfully, but I cannot get the to work - it is supposed to copy all non-java files over too to emulate the behaviour of eclipse.

So, I have a pathId named source.path which contains multiple directories, which I somehow needs to massage into something the copy-task like. I have tried nesting which is not valid, and some other wild guesses.

How can I do this - thanks in advance.

A: 

You could use the foreach task from the ant-contrib library:

<target name="build">
    ...

    <!-- copy resources from src to bin -->
    <foreach target="copy.resources" param="resource.dir">
        <path refid="source.path"/>
    </foreach>
</target>

<target name="copy.resources">
    <copy todir="${classes.dir}" preservelastmodified="true">
        <fileset dir="${resource.dir}" exclude="**/*.java">
    </copy>
</target>

If your source.path contains file paths as well then you could the if task (also from ant-contrib) to prevent attempting to copy files for a file path, e.g.

<target name="copy.resources">
    <if>
        <available file="${classes.dir}" type="dir"/>
        <then>
            <copy todir="${classes.dir}" preservelastmodified="true">
                <fileset dir="${resource.dir}" exclude="**/*.java">
            </copy>
        </then>
    </if>
</target>
Simon Lieschke
A: 

You might consider using pathconvert to build a pattern that fileset includes can work with.

<pathconvert pathsep="/**/*," refid="source.path" property="my_fileset_pattern">
    <filtermapper>
        <replacestring from="${basedir}/" to="" />
    </filtermapper>
</pathconvert>

That will populate ${my_fileset_pattern} with a string like:

1/**/*,2/**/*,3

if source.path consisted of the three directories 1, 2, and 3 under the basedir. We're using the pathsep to insert wildcards that will expand to the full set of files later.

The property can now be used to generate a fileset of all the files. Note that an extra trailing /**/* is needed to expand out the last directory in the set. Exclusion can be applied at this point.

<fileset dir="." id="my_fileset" includes="${my_fileset_pattern}/**/*">
    <exclude name="**/*.java" />
</fileset>

The copy of all the non-java files then becomes:

<copy todir="${classes.dir}" preservelastmodified="true">
    <fileset refid="my_fileset" />
</copy>

That will copy the source files over retaining the source directory structure under todir. If needed, the flatten attribute of the copy task can be set to instead make all the source files copy directly to todir.

Note that the pathconvert example here is for a unix fileseystem, rather than windows. If something portable is needed, then the file.separator property should be used to build up the pattern:

<property name="wildcard" value="${file.separator}**${file.separator}*" />
<pathconvert pathsep="${wildcard}," refid="source.path" property="my_fileset">
...
martin clayton