tags:

views:

2044

answers:

1

Hello,

I am in the process of packaging my java application into a jar file. I am using ant and eclipse. I need to actually include in the jar a couple of separate, non-code files (xml and txt files) directly under the root folder, not in the same place as the code.

I am trying to use includesfile, but that doesn't seem to work, here is my ant target:

<target name="distribute" depends="compile" includesfile="readthis.txt">
 <jar destfile="${distributionDir}/myjar.jar" >
     <fileset dir="${outputDir}"/>
 <fileset dir="${sourceDir}"/>     
 </jar>
</target>

The above works fine when I omit the includesfile argument. Plus, I need to add multiple non-code files. These files are located directly under the root project folder in Eclipse, and not within any java packages.

Also, as you can see above, I am basically including to the jar my source code as well. Is there a way to tell the jar task to put the source code in a separate folder from the compiled code?

As a compile or build task, Ant easily can separate source code from the compiled classes. But is there a way to do it within a jar file as well?

Many thanks for your time!

+4  A: 

You shouldn't have an includesfile attribute in a target to start with, and I suspect you've misunderstood the point of it anyway - the idea is that the file you specify with includesfile contains patterns for which files to include, if you don't want to put them into your Ant file.

It strikes me that all you need is an extra fileset containing the appropriate files you need. For example, for readthis.txt:

<target name="distribute" depends="compile">
  <jar destfile="${distributionDir}/myjar.jar" >
    <fileset dir="${outputDir}"/>
    <fileset dir="${sourceDir}"/>
    <fileset file="readthis.txt" />
  </jar>
</target>

I would also suggest that you create a new directory for the resources, so you can do:

<target name="distribute" depends="compile">
  <jar destfile="${distributionDir}/myjar.jar" >
    <fileset dir="${outputDir}"/>
    <fileset dir="${sourceDir}"/>
    <fileset dir="${resourcesDir}" />
  </jar>
</target>

with no hassle. This will also keep your directory structure cleaner, making it easier to find things from the top downwards.

Jon Skeet
Thanks! That certainly helped. I was wondering if I can also tell the jar task to separate source from compiled classes? At this time, both the source and the classes are located in the same package structure, Shouldn't the jar task preserve the output and src folders?Meaning that when I expand the jar, I see in myjar/code/my/package/file.java and file.classI thought that the jar task could preserve the folders like:myjar/code/my/package/source/file.java -- and separatelymyjar/code/my/package/build/file.classAm I missing something in my usage of the jar task?
denchr
No you're incorrect. They should be side-by-side. If the jar contains a folder structure like code/my/package/build/file.class, then that would imply the package name should be code.my.package.build. You could also just put the sources into a second JAR file.
matt b
I see what you mean. Soemthing like what I am suggesting would have implications to the package naming... So, in general, If I want to include the source in my jar file, it *has* to be in he same folder as the compiled build.
denchr