Is there a way I can modify a file in a jar using ant script. Like, I have a x.properties in a y.jar. I want to edit this x.properties and put it back into the y.jar using ant script. Is this possible?
To extract a file from a jar:
<unjar src="y.jar" dest="build">
<patternset>
<include name="x.properties"/>
</patternset>
</unjar>
To add it back in:
<jar jarfile="y.jar" update="true">
<fileset dir="." includes="x.properties"/>
</jar>
jar -xvf <filename> extracts a file out of a jar.
jar -uvf <filename> can update a file in the jar.
You can use a script to do this and then use Ant to call the script.
I usually unpack the whole jar file, alter the file I want to alter, and then create a new jar file. I is probably not the fasted way, but my build scrips are not time critical.
Related
I have an existing ant build file that zips up some files, but I want to make some files read only in the zip on osx and windows. Is this possible?
I've found you can change the read only attribute on windows with <attrib>, but "Right now it has effect only under Windows". Any cross platform solutions?
chmod is the Unix-only way, but there is no cross-platform task.
But when you say you want to make the files read-only inside the ZIP then you don't need to modify the files on disk (in fact it wouldn't have any effect on the permissions stored inside the archive if you use Ant's zip task). In order to set permissions of archive entries, use a zipfileset and the filemode attribute. Something along the lines of
<zip ...>
<fileset dir="...">
<exclude name="files that should be read-only"/>
</fileset>
<zipfileset dir="..." filemode="444">
<include name="files that should be read-only"/>
</zipfileset>
</zip>
I'm trying to use Ant bndwrap task to wrap non-OSGi jars in a directory. My current Ant configuration for this is:
<target name="wrap-jars" description="Wrap non-OSGi jars">
<taskdef resource="aQute/bnd/ant/taskdef.properties" classpath="${biz.aQute:bnd:jar}"/>
<bndwrap output="${dist.dir}/app-modules">
<fileset dir="${dist.dir}/app-modules" includes="*.jar" />
</bndwrap>
<move overwrite="true" todir="${dist.dir}/app-modules" >
<fileset dir="${dist.dir}/app-modules" includes="*.bar" />
<mapper type="glob" from="*.bar" to="*.jar" />
</move>
</target>
This works fine, but the problem is that it also wraps existing OSGi jar, which causes problems. For instance, I noticed it changes Bundle-SymbolicName header to some default value. It might be changing something else, which I don't want. I only want it to operate on jars that have no OSGi info at all.
Is there some way to tell BND to ignore existing OSGi headers in manifest, or complete jars that are already OSGi-fied?
I would store non-OSGi jars in a separate folder and modify the fileset to process only that folder.
I've noticed that recent bnd versions (for example, 2.1.0) now honour the Bundle-SymbolicName when rewrapping OSGi jars.
just change your fileset to exclude that jar
I am trying to get files from FTP, it always says no files retrieved,
here goes my code
<target name="ftp">
<ftp action="get"
server="${myftpserver}"
userid="${username}"
password="${password}"
remotedir="a"
binary="no"
verbose="yes"
passive="yes">
<fileset dir="abc" includes="CatalogReferenceAttribute.java"/>
</ftp>
</target>
Here i am trying to retrieve a .java file from FTP folder
a
to my local folder
abc
below is my ouput
Where i was wrong?
Did you put the dependency libraries into ANT's lib directory?
common-net.jar may not be enough... check this page:
http://ant.apache.org/manual/install.html#librarydependencies
I am looking forward for an example which unjars a file referenced from the classpath inside the ANT. I couldn't find any example in the web. When I tried using the path inside the unjar target, it unjars all the jar files inside the classpath. Can anyone suggest me something giving some example ?
You have to specify the jar name which you what to unjar.
Probably you have wildcard(*) in your src
This will unjar all jars in ${ant.home}/lib/
<unzip src="${ant.home}/lib/*.jar" dest="..."/>
I think you should use:
<unzip src="${ant.home}/lib/ant.jar" dest="..."/>
If you want to unjar some files from your jar look at this example:
<unzip src="${ant.home}/lib/ant.jar" dest="...">
<patternset>
<include name="**/ant_logo_large.gif"/>
<include name="**/LICENSE.txt"/>
</patternset>
</unzip>
Look at unzip task documentation
I am a bit of an ant newbie, and I'm having trouble making a jar correctly. As an example, say I want to make a jar with my StringUtil class. Using the following ant directive, I can create the jar, but the problem is that the directory structure is lost. It simply puts StringUtil.class in the base directory of the jar. How can I correct this ant directive so that StringUtil.class is inside the com/test directory in the jar?
<jar destfile="myjar.jar" >
<fileset file="${build}/com/test/StringUtil.class"/>
</jar>
Thanks!
You need to tell Ant to build the jar from the base directory, then tell it to include only the desired file. Like so:
<jar destfile="myjar.jar" >
<fileset dir="${build}" includes="com/test/StringUtil.class"/>
</jar>
Here's the doc for <fileset> tags.