4
votes

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>
1
you should consider loading the files as ressources from the classpath: stackoverflow.com/questions/3294196/…oers
I prefer not to modify the code if possible -- this is an external project that I'm evaluating.espeed
@espeed: Are you trying to add the properties files to a config directory in the jar?Christopher Peisert
Yes, so it's accessible within the jar.espeed
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 codeoers

1 Answers

5
votes

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?