You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Luca Ferrari <fl...@infinito.it> on 2008/02/20 14:22:25 UTC
problems creating a jar
Hi guys,
I've got a problem creating a buildfile for a project of mine, where I'd like
also to build a jar as final step. This is source tree:
projectRoot
+src // java sources
+srcTest // junit sources
+img // icons
+lib // jar required by the project
+conf // configuration files
I've created a build file that creates a build directory that compiles the
sources and copies the resource (images and configuration), producing a tree
like the following:
build
+classes
+conf
+img
+lib
+deploy
where deploy should contain the jar created. The problem is that the jar is
created but without the directory I'd like to include (classes, conf, img),
but with the resources in a flat space. For example, instead of having img/
I've got all the images in the root of the jar, the same happens for the conf
directory. I didn't find a solution, can anyone please explain me how to
include the resource in the jar?
My jar target is the following:
<target name="dist" depends="compile" >
<jar destfile="${build.jar.name}" update="false" >
<!-- include the compiled files (classes directory)-->
<fileset dir="${build.dir}">
</fileset>
<!-- include the images (img directory)-->
<fileset dir="${build.img.dir}">
</fileset>
<!-- the manifest of this jar -->
<manifest>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Main-Class" value="${runtime.mainclass}" />
<!-- a section with a few notes -->
<section name="additional-info">
<attribute name="Project-Name" value="${ant.project.name}"/>
<attribute name="Project-Version" value="${build.version}" />
<attribute name="Project-Build" value="${build.number}" />
</section>
</manifest>
</jar>
</target>
Thanks,
Luca
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: problems creating a jar
Posted by Steve Loughran <st...@apache.org>.
Dimitris Mouchritsas wrote:
> Steve Loughran wrote:
>> <zipfileset dir="build/classes" prefix="WEB-INF/classes"/>
> Steve, creating a tmp dir to create a complicated structure inside a jar
> file is the recommended approach? Is there a better way to do it? If the
> tmp dir is the way to go I suppose it's best not to delete this dir,
> except on a "clean" build right? This would allow for time stamp
> checking so in case nothing changes the jar wouldn't have to be re-jared
> every time?
There's no right answer here. Zip files only have 2s worth of
resolution, so can get a bit confused dependency wise. I think also for
war files, sticking everything out to place makes it easier to deploy
stuff in exploded, skipping the <war> step, which gets pretty slow on a
big project.
for doing redistributable, well, whatever suits. I tend to do
copy->tar->zip, because tar retains the accuracy. I also try to set
permissions up in both the tar and the zip, which needs complex
<zipfile> work
Here's a snippet. Patterns are shared across zip and tar, each one I set
up the prefix of the redistributed app (including version) and then pull
in the various patterns with the right executable flags:
<target name="zipfilesets" depends="ready-to-release">
<zipfileset id="doc.files.zf" dir="docs" prefix="${prefix}/docs">
<patternset refid="docs.pattern" />
</zipfileset>
<!--docs in the dist/docs subdir-->
<zipfileset id="dist.doc.files.zf" dir="${dist}/docs/"
prefix="${prefix}/dist/docs">
<patternset refid="docs.pattern" />
</zipfileset>
<!-- distributables. Leaves out but docs but includes binaries
without setting permissions-->
<zipfileset id="dist.main.zf" dir="${dist}" prefix="${prefix}/dist">
<include name="**"/>
<exclude name="lib/**" />
<exclude name="bin/**"/>
<exclude name="docs/**"/>
<exclude name="classes/**"/>
<patternset refid="excluded.cruft"/>
</zipfileset>
<!--binaries. Does not include bin/security -->
<zipfileset id="dist.unix.bin.zf" dir="${dist}/bin/"
filemode="${exec}" prefix="${prefix}/dist/bin">
<patternset refid="unix.bin" />
</zipfileset>
<zipfileset id="dist.dos.bin.zf"
dir="${dist}/bin/" prefix="${prefix}/dist/bin">
<patternset refid="dos.bin" />
</zipfileset>
<zipfileset id="dist.other.bin.zf"
dir="${dist}/bin/" prefix="${prefix}/dist/bin">
<patternset refid="other.bin" />
</zipfileset>
<zipfileset id="security.unix.bin.zf" dir="${dist}/bin/security"
filemode="${exec}"
prefix="${prefix}/dist/bin/security">
<patternset refid="unix.bin" />
</zipfileset>
<zipfileset id="security.dos.bin.zf" dir="${dist}/bin/security"
prefix="${prefix}/dist/bin/security">
<patternset refid="dos.bin" />
</zipfileset>
<zipfileset id="security.other.bin.zf" dir="${dist}/bin/security"
prefix="${prefix}/dist/bin/security">
<patternset refid="other.bin" />
</zipfileset>
<zipfileset id="unix.bin.zf" dir="bin" filemode="${exec}"
prefix="${prefix}/bin">
<patternset refid="unix.bin" />
</zipfileset>
<zipfileset id="dos.bin.zf" dir="bin" filemode="${exec}"
prefix="${prefix}/bin">
<patternset refid="dos.bin" />
</zipfileset>
<zipfileset id="other.bin.zf" dir="bin"
prefix="${prefix}/bin">
<patternset refid="other.bin" />
</zipfileset>
<!-- source -->
<zipfileset id="main.zf" dir="."
prefix="${prefix}/">
<patternset refid="source.pattern" />
</zipfileset>
<zipfileset id="dist.lib.zf" dir="${dist}/lib"
prefix="${prefix}/dist/lib">
<patternset refid="lib.pattern" />
</zipfileset>
<zipfileset id="lib.zf" dir="lib"
prefix="${prefix}/lib">
<patternset refid="lib.pattern" />
</zipfileset>
<presetdef name="make-dist-zip">
<zip duplicate="fail">
<zipfileset refid="dist.main.zf" />
<zipfileset refid="dist.doc.files.zf" />
<zipfileset refid="dist.unix.bin.zf" />
<zipfileset refid="dist.dos.bin.zf" />
<zipfileset refid="dist.other.bin.zf" />
<zipfileset refid="dist.lib.zf" />
<zipfileset refid="security.unix.bin.zf" />
<zipfileset refid="security.dos.bin.zf" />
<zipfileset refid="security.other.bin.zf" />
</zip>
</presetdef>
</target>
I can then run the preset, or extend it
<target name="dist-zip" depends="zipfilesets"
description="distribution zip">
<make-dist-zip destfile="${dist.zip}" />
</target>
<target name="dist-unzip" depends="dist-zip">
<property name="dist.unzip.dir"
location="${release.unzip.dir}/zip/dist" />
<delete dir="${dist.unzip.dir}" />
<mkdir dir="${dist.unzip.dir}" />
<unzip dest="${dist.unzip.dir}" src="${dist.zip}"/>
</target>
now, dont even get me started on how we build and test the RPMs, as it
is scary. VMware machine images, bulk scp upload, ssh in as root, rpm
commands then we run command on the remote machine to walk the new
init.d round its lifecycle. Its crossed the line between redistributable
and deployment (especially as the next step is to generate custom RPMs
with private certificates in)
--
Steve Loughran http://www.1060.org/blogxter/publish/5
Author: Ant in Action http://antbook.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: problems creating a jar
Posted by Dimitris Mouchritsas <di...@eurodyn.com>.
Steve Loughran wrote:
> Luca Ferrari wrote:
>> On Wednesday 20 February 2008 Dimitris Mouchritsas's cat, walking on
>> the keyboard, wrote:
>>> So for example you create:
>>> /dist
>>> tmp/
>>> classes/
>>> img/
>>> lib/
>>>
>>> and then you <jar destfile="${dist.dir}/${build.jar.name}"
>>> basedir="${dist.dir}/tmp/"> etc.
>>
>> But in this case into the jar I will have also the classes directory,
>> while it should be present only the package tree, isn't it?
>> Thanks for you suggestion, but is there any way to get the jar task
>> working with the fileset, since I've got already a tree structure
>> that contains everything I have to place in the jar?
>
> Use <zipfileset> and its prefix attribute
>
> <zipfileset dir="build/classes" prefix="WEB-INF/classes"/>
Steve, creating a tmp dir to create a complicated structure inside a jar
file is the recommended approach? Is there a better way to do it? If the
tmp dir is the way to go I suppose it's best not to delete this dir,
except on a "clean" build right? This would allow for time stamp
checking so in case nothing changes the jar wouldn't have to be re-jared
every time?
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: problems creating a jar
Posted by Steve Loughran <st...@apache.org>.
Luca Ferrari wrote:
> On Wednesday 20 February 2008 Dimitris Mouchritsas's cat, walking on the
> keyboard, wrote:
>> So for example you create:
>> /dist
>> tmp/
>> classes/
>> img/
>> lib/
>>
>> and then you <jar destfile="${dist.dir}/${build.jar.name}"
>> basedir="${dist.dir}/tmp/"> etc.
>
> But in this case into the jar I will have also the classes directory, while it
> should be present only the package tree, isn't it?
> Thanks for you suggestion, but is there any way to get the jar task working
> with the fileset, since I've got already a tree structure that contains
> everything I have to place in the jar?
Use <zipfileset> and its prefix attribute
<zipfileset dir="build/classes" prefix="WEB-INF/classes"/>
--
Steve Loughran http://www.1060.org/blogxter/publish/5
Author: Ant in Action http://antbook.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: problems creating a jar
Posted by Luca Ferrari <fl...@gmail.com>.
On Wednesday 20 February 2008 Dimitris Mouchritsas's cat, walking on the
keyboard, wrote:
> So for example you create:
> /dist
> tmp/
> classes/
> img/
> lib/
>
> and then you <jar destfile="${dist.dir}/${build.jar.name}"
> basedir="${dist.dir}/tmp/"> etc.
But in this case into the jar I will have also the classes directory, while it
should be present only the package tree, isn't it?
Thanks for you suggestion, but is there any way to get the jar task working
with the fileset, since I've got already a tree structure that contains
everything I have to place in the jar?
Luca
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: problems creating a jar
Posted by Dimitris Mouchritsas <di...@eurodyn.com>.
Luca Ferrari wrote:
> Hi guys,
> I've got a problem creating a buildfile for a project of mine, where I'd like
> also to build a jar as final step. This is source tree:
>
> projectRoot
> +src // java sources
> +srcTest // junit sources
> +img // icons
> +lib // jar required by the project
> +conf // configuration files
>
> I've created a build file that creates a build directory that compiles the
> sources and copies the resource (images and configuration), producing a tree
> like the following:
>
> build
> +classes
> +conf
> +img
> +lib
> +deploy
>
> where deploy should contain the jar created. The problem is that the jar is
> created but without the directory I'd like to include (classes, conf, img),
> but with the resources in a flat space. For example, instead of having img/
> I've got all the images in the root of the jar, the same happens for the conf
> directory. I didn't find a solution, can anyone please explain me how to
> include the resource in the jar?
> My jar target is the following:
>
> <target name="dist" depends="compile" >
> <jar destfile="${build.jar.name}" update="false" >
>
> <!-- include the compiled files (classes directory)-->
> <fileset dir="${build.dir}">
> </fileset>
>
>
> <!-- include the images (img directory)-->
> <fileset dir="${build.img.dir}">
> </fileset>
>
>
>
> <!-- the manifest of this jar -->
> <manifest>
> <attribute name="Built-By" value="${user.name}" />
> <attribute name="Main-Class" value="${runtime.mainclass}" />
>
> <!-- a section with a few notes -->
> <section name="additional-info">
> <attribute name="Project-Name" value="${ant.project.name}"/>
> <attribute name="Project-Version" value="${build.version}" />
> <attribute name="Project-Build" value="${build.number}" />
> </section>
>
> </manifest>
>
> </jar>
>
> </target>
>
>
> Thanks,
> Luca
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>
A quick and dirty solution is to create a directory with the exact
structure you want in the jar file (use the copy task for this). Then
use jar with the basedir attribute. Maybe someone else could come up
with a better answer?
Also, I think it's better (and seems to be commonplace) to use /dist as
the directory to place your files for distribution (jar's, war's, ear's etc)
So for example you create:
/dist
tmp/
classes/
img/
lib/
and then you <jar destfile="${dist.dir}/${build.jar.name}"
basedir="${dist.dir}/tmp/"> etc.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org