4

I'm trying to build an "uberjar" with ant, and the project has a config directory with several properties files.

How do I add the config dir to build.xml?

Here's an example of how it's referenced:

static private String CONFIG_DIR = "config/";
static public String CONFIG_FILE = "proj.properties";

File configFile = new File(CONFIG_DIR, CONFIG_FILE);

There are multiple properties files in the config directory. Normally I would just add it to the classpath:

java -classpath bin:lib/*:config some.project.Example

Here's my build.xml target, which isn't quite right:

<target name="uberjar" depends="compile">
  <jar jarfile="${babelnet.jar}">
    <fileset dir="${bin.dir}">
      <include name="**/*.class"/>
    </fileset>
    <zipgroupfileset dir="lib" includes="*.jar"/>
  </jar>
  <copy todir="config">
    <fileset dir="config">
      <include name="**/*.properties"/>
    </fileset>
      </copy>
</target>
espeed
  • 4,754
  • 2
  • 39
  • 51
  • you should consider loading the files as ressources from the classpath: http://stackoverflow.com/questions/3294196/load-resource-from-anywhere-in-classpath – oers Aug 17 '12 at 14:47
  • 1
    I prefer not to modify the code if possible -- this is an external project that I'm evaluating. – espeed Aug 17 '12 at 14:51
  • @espeed: Are you trying to add the properties files to a config directory in the jar? – Christopher Peisert Aug 17 '12 at 15:21
  • Yes, so it's accessible within the jar. – espeed Aug 17 '12 at 15:26
  • it is not possible to access anything inside a jar file with new File() ..., you need to have those file separate from your jar if you don't want to change the code – oers Aug 17 '12 at 17:25

1 Answers1

5

To include the properties files within the new jar file, you could add another nested <fileset> to the <jar> task.

<target name="uberjar" depends="compile">
  <jar jarfile="${babelnet.jar}">
    <fileset dir="${bin.dir}">
      <include name="**/*.class"/>
    </fileset>
    <zipgroupfileset dir="lib" includes="*.jar"/>
    <fileset dir="${basedir}">
      <include name="config/*.properties"/>
    </fileset>
  </jar>
</target>

Also, see stackoverflow question: How to read a file from a jar file?

Community
  • 1
  • 1
Christopher Peisert
  • 21,862
  • 3
  • 86
  • 117
  • I tried that, but `new File(CONFIG_DIR, CONFIG_FILE);` is not finding the file. – espeed Aug 17 '12 at 16:30
  • @espeed: See the link to ***[How to read a file from a jar file?](http://stackoverflow.com/q/2271926/1164465)*** – Christopher Peisert Aug 17 '12 at 16:31
  • Thanks. I'm trying to make this work without modifying the project's code -- this is an external project. – espeed Aug 17 '12 at 16:36
  • 2
    If you must use a Java [`File`](http://docs.oracle.com/javase/6/docs/api/java/io/File.html) object, then you will not be able to read the properties files from the jar file. – Christopher Peisert Aug 17 '12 at 16:43