Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ant include non-java resource files with compiled classes

Tags:

java

ant

I have a Java project with all source files in the src folder. This src folder contains several packages. Some of these packages/folders do not include any Java code. They just contain some images that I use in the application as icons.

The directory structure is similar to the following.

src/
com/
   example/
       pk1/     # This contains only some Java files.
       pk2/     # This will contain some XML and Java code.
       im1/     # This subfolder will contain just the images

Now, when I use the javac task, it compiles all the Java files into class files and stores them in the destination directory that I specify. However, an non-java files remain as is in the source folder.

I referred this Question on SO. However, I think in my case I want to include all non-Java resource files (without the need to explicitly specifying what files to be moved) automatically into the build directory (one that contains all the class files) while following the same directory structure as in source.

Pardon me if I am missing something very trivial, but how do I ask Ant to do this?

like image 613
Ankit Avatar asked May 05 '12 02:05

Ankit


People also ask

What does * stand for in * Test Java in ant?

**\*.sql means "in the given directory and inside all of its subdirectories, all the files that end with .sql"

How do I compile an ant project?

In the Project tool window, right-click the generated build file and select Add as Ant Build File to open it in the Ant tool window. In the Select Path dialog, navigate to the desired build. xml file, and click OK. A build file should have at least a root element to be added to the Ant Build tool window.

What is Ant build file?

Ant is a Java-based build tool created as part of the Apache open-source project. You can think of it as a Java version of make. Ant scripts have a structure and are written in XML. Similar to make, Ant targets can depend on other targets. For example, Ant is used in the context of plug-in development in the build.


2 Answers

Use the ant copy task to copy your resources into the classes directory (or wherever your .class files are stored). By default, ant keeps the directory structure.

For example:

<copy todir="classes">
  <fileset dir="com/example">
    <exclude name="**/*.java"/>
  </fileset>
</copy>
like image 90
Tim Pote Avatar answered Oct 21 '22 11:10

Tim Pote


Just to complete the previous answer, the block

<copy todir="classes">
  <fileset dir="com/example">
    <exclude name="**/*.java"/>
  </fileset>
</copy>

goes before the task javac

<javac [...]

and inside the target compile

<target name="compile" [...]

then

<target name="compile" [...]>
   <copy todir="&&&" [...]>
       [...]
   </copy>

   <javac srcdir="[...]" destdir="&&&" >
   </javac>
</target>

in my case the attribute "todir" of tag "copy" it's the same of the attribute "destdir" of tag "javac"; I wanted to detailing the answer in order to give you a resolution as immediate as possible.

like image 34
HouseFragance Avatar answered Oct 21 '22 12:10

HouseFragance