Ant Javac compile subpackage class files to the parent src directory

Tags: , , ,



I would like Ant 1.9 to be able to compile servlet classes to the parent src directory, but the file system has the files in packages. No, the packages are not declared in the servlet files.

The code is deployed with all the servlets in the same directory. I supposed I can create an ant copy command to do this, but I would prefer there be an ant javac solution.

Manually this would be ‘javac -d .. *.java’ after changing to each subdirectory, not including the files already in the default package. This is highly irregular, but I cannot change the how the packages are defined nor can I change the urls from which the code is executed from.

<target name="compileServlet" description="Compiles Java source files.">
            <javac srcdir="${servlets.dir}" destdir="$(servlets.dir}" debug="true" encoding="ISO-8859-1" source="1.7" target="1.7" failonerror="true">
                    <classpath path="${env.CLASSPATH}" />
                    <include name="**/*.java"/>
            </javac>
    </target>

Right now when I run this ant build.xml, no class files are generated. Any thoughts how I can solve this issue?

Answer

If you have source files in multiple root directories and want to compile them all into a single root directory, use the <src> element instead of the srcdir attribute.

Here is the example shown in the documentation:

<javac destdir="${build}"
       classpath="xyz.jar"
       debug="on">
  <src path="${src}"/>
  <src path="${src2}"/>
  <include name="mypackage/p1/**"/>
  <include name="mypackage/p2/**"/>
  <exclude name="mypackage/p1/testpackage/**"/>
</javac>


Source: stackoverflow