39

I created my own build.xml which has:

<target name="compile">
    <mkdir dir="build"/> 
    <javac destdir="build"> 
        <src path="src"/> 
    </javac>
</target>

<target name="build" depends="compile">
    <mkdir dir="dist"/>
    <jar destfile="dist/app.jar" basedir="build" />
</target>

<target name="run" depends="compile">
    <java classname="webserver.Loader" classpath="build" fork="true" />      
</target>

It works great. When I call ant run so it compiles and runs my application, but my application has a package with icons and it isn't moved to a folder "build" so my application ends with an exception that it couldn't locate my icons. When I move them by myself then it works.

I tried to use

<copy todir="build/app/icons">
    <fileset dir="src/app/icons"/>
</copy>

It works, but I would like to do it without the copy command. Is there any parameter to javac? Or something else?

Thank you for answer.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Tomáš Linhart
  • 13,509
  • 5
  • 51
  • 54

4 Answers4

49

There is no such parameter. You can copy all sorts of files between your directories with:

<copy todir="build">
    <fileset dir="src"
             includes="**/*.xml,**/*.properties,**/*.txt,**/*.ico" />
</copy>
Chris Winters
  • 1,001
  • 1
  • 6
  • 5
25

Sorry, you will need to copy non-java files manually. Resources are technically not "source". The command-line javac will not copy resource files from your source directory to the output directory, neither will ant's javac task.

Mike Miller
  • 2,149
  • 1
  • 18
  • 23
  • It seems that hooking into the eclipse java compiler through ant will auto-copy non java files to the output directory. When I switched over to using javac ant tasks instead, I had to do what Chris Winters suggested. – Justin Skiles Mar 27 '14 at 18:11
  • Since writing this answer I have changed over to almost exclusively using Maven for java projects. There is a decently steep learning curve vs. Ant, but very much worth it. – Mike Miller Sep 09 '14 at 18:40
  • This answer tells you why it cannot be done, my answer below tells you how to do it. – David Hopkins Mar 16 '18 at 01:03
15

You can do this using the fileset element of the jar task instead of manually copying the files. For example:

<jar destfile="dist/app.jar" basedir="build">
    <fileset dir="src" includes="app/icons/**" />
</jar>

This will copy everything in src/app/icons/ to the app/icons path in your .jar file.

David Hopkins
  • 352
  • 3
  • 5
  • 2
    The other answers tell you why it can't be done. This one tells you how to do it. And it works. This should be upvoted. – philburk Mar 05 '17 at 00:50
4

No, there isn't. The copy task is the correct way to copy resources into your build folders.

jsight
  • 27,819
  • 25
  • 107
  • 140