views:

355

answers:

3

I'm working with ant on linux using the command line. I've got a simple build step with javac. This compiles correctly but it creates a directory structure under build like my namespace.

ie: ./build/com/crosse/samplespace/SampleProgram.class

How can I get this output into one directory (where it's easier to call java on it).

I tried

 <target name="output" depends="compile">
            <copy todir="${output}">
                    <fileset dir="${build}" includes="**/*.class"/>
            </copy>
 </target>

but that repeats the same thing in my output directory. How can I use ant to get everything into a single directory?

Alternatively how could I use an ant step to copy another file into the root of that path (an apache commons configuration file)?

Edit: This is mainly a convenience factor, I get tired of navigating through the same 3 directories to run my program.

+1  A: 

What about creating a jar file containing all your files (classes and other ressources)? A jar also has a 'default' class which's main method gets executed when the user double clicks it or calls it using java -jar . You can use ant's jar task.

Beside that, it is important that you keep your directory structure (outside a jar), otherwise the java class loader won't be happy when it has to load these classes. Or did I get your question wrong?

reto
A: 

You can accomplish what you want to do by using the flattenmapper but I'm at a loss to understand what possible valid reason you'd have for doing it.

A: 

To answer your question, you would use a flatten mapper, as carej points out. The copy task even has a shortcut for it:

<copy todir="${output}" flatten="yes">
    <fileset dir="${build}" includes="**/*.class"/>
</copy>

However, I also don't understand what you hope to accomplish with this. The java runtime expects class files to be in a directory structure that mirrors the package structure. Copying the class files to the top-level directory won't work, unless the classes are in the default package (there is no package statement in the .java files).

If you want to avoid having to manually change directories, you can always add a target to your ant build file that calls the java task with the appropriate classpath and other parameters. For example:

<target name="run" depends="compile">
    <java classname="com.crosse.samplespace.SampleProgram"
          classpath=".:build"/>
</target>
Jason Day