tags:

views:

166

answers:

2

Is there a way to update a jar file in Ant?

EDIT: For example, if I wanted to add some additional files to an already existing JAR file?

+5  A: 

You should be able to do this with the Jar Task if you set update to true.

<jar update="true" jarfile="${jarfile}" >
    <!-- ... -->
</jar>
C. Ross
+7  A: 

Sure, that's totally possible. The ant Jar task can do anything the jar command line can do. You do it with the update flag set to true instead of false.

  <jar destfile="/x/y/z/file.jar"
       basedir="/a/b/c/"
       update="true"
  />

Where the destination jar is already in existence at that path.

EDIT: To set a path

  <jar destfile="/x/y/z/file.jar"
       update="true">
      <zipfileset dir="/a/b/c"/ prefix="x/y/z" />
  </jar>
justkt
How does the update operation work? Specifically, if I have a file set in a/b/c/* that I want to add into the jar file x/y/z/* is that possible?
predhme
Yes, it's definitely possible. Remember - jar is just a specialized zip (for real). If you look at the Ant zip task, the zip even has an update example. So, if you imagine that "filesToAdd" in my example is /a/b/c* and the destfile is x/y/z/file.jar, you should be able to get it to work. Don't believe me? Copy x/y/z/file.jar somewhere and try it with a test ant script and check my work :).
justkt
To clarify. Assuming I have a file structure of x/y/z in my original jar file. I have some additional files in a/b/c that i want to add into z. Will the update simply add a/b/c to the original jar file. Or can I specify the file path that is used when adding?
predhme
To specify a path, look at the Jar task docs. It shows you how to do that using filesets. In general the ant task documentation is very good.
justkt
Many thanks found the zipfileset as you updated your answer. Thanks very much.
predhme