Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant multiple source directories with copied resources

Tags:

ant

Consider minimal build.xml fragment which builds jar from sources and includes all non-java resources:

<property name="src.dir" value="src" />

<target name="build">
    <javac destdir="bin">
        <src path="${src.dir}" />
    </javac>
    <copy includeemptydirs="false" todir="bin">
        <fileset dir="${src.dir}">
            <exclude name="**/*.java" />
        </fileset>
    </copy>
    <jar destfile="dist/foo.jar" basedir="bin"/>
</target>

Now imagine that I need to support a list of source directories:

<property name="src.dirs" value="src;src-gen" />

How can i modify above script to make it happen ? javac will happily take list of directories but for copy I need to transform string into list of filesets with exclusions or find some other way.

like image 986
Marcin Wisnicki Avatar asked Jul 12 '13 15:07

Marcin Wisnicki


2 Answers

Normally, you simply list them all together:

<javac destdir="bin">
    <src path="${src.dir}"/>
    <src path="${src2.dir}"/>
    <src path="${src3.dir}"/>
</javac>

You can try the <sourcepath/> attribute. I've never used it, but I believe you can use it to define a path of various source files, and use that:

<path id="source.path">
    <pathelement path="${src.dir}"/>
    <pathelement path="${src2.dir}"/>
    <pathelement path="${src4.dir}"/>
</path>

<javac destdir="bin">
    srcpathref="source.path"/>

The first will work, but not 100% sure about the second.

like image 112
David W. Avatar answered Nov 02 '22 10:11

David W.


I'm not sure of a way to do it with built-in Ant tasks but you could use an ant-contrib <for> task

<path id="src.path">
  <pathelement location="src" />
  <pathelement location="src-gen" />
</path>

<target name="build">
    <javac destdir="bin">
        <src refid="src.path" />
    </javac>
    <for param="dir">
        <path refid="src.path" />
        <sequential>
            <copy includeemptydirs="false" todir="bin">
                <fileset dir="@{dir}">
                    <exclude name="**/*.java" />
                </fileset>
            </copy>
        </sequential>
    </for>
    <jar destfile="dist/foo.jar" basedir="bin"/>
</target>
like image 4
Ian Roberts Avatar answered Nov 02 '22 09:11

Ian Roberts