How to include an empty folder in a zip file - ant

I have created a zip file with many folders and files in it.
<!-- Create the distribution directory -->
<mkdir dir="${buildDirectory}/core/dist"/>
<zip destfile="${buildDirectory}/core/dist/core.zip">
<zipfileset dir="${buildDirectory}/core/" prefix="core/bin">
<include name="*.jar" />
</zipfileset>
<zipfileset dir="${buildDirectory}/core/src/resources" prefix="core/conf">
<include name="log4j.properties" />
<include name="core.properties" />
<include name="conf.xml" />
</zipfileset>
<zipfileset dir="${buildDirectory}/algorithm/testData" prefix="core/data">
<include name="defs.properties" />
<include name="proto.aza" />
</zipfileset>
So the zip now has
/bin
/conf
/data
I also need to include (add) an empty /logs folder in the zip structure.
How do I do that.
thanks

The least clumsy way I can find of doing this is to create an empty directory somewhere first, and then include it in your zipfile. Cutting down your example a bit, to get a zip with just an empty logs directory in it, you could write:
<mkdir dir="${buildDirectory}/core/dist"/>
<mkdir dir="${buildDirectory}/core/dist/logs"/>
<zip destfile="${buildDirectory}/core/dist/core.zip">
<zipfileset dir="${buildDirectory}/core/dist/logs" fullpath="logs"/>
</zip>

Related

I wan to unzip multiple files in a folder with the same name as the file name using Ant?

I have a folder with multiple .zip files. I want to unzip them in the same folder, with the name same as that of the file.
eg.
temp/product_file1.zip
temp/product_file2.zip
temp/product_file3.zip
Output expected:
temp/product_file1
temp/product_file2
temp/product_file2
How can I make a generic code using Ant?
The code which I am using now is able to only unzip the last file.
<path id="warFilePath">
<fileset dir="/temp">
<include name="product_*.zip"/>
</fileset>
</path>
<property name="warFile" refid="warFilePath" />
<basename property="warFilename" file="${warFile}" suffix=".zip" />
<unzip dest="temp/${warFilename}">
<fileset dir="/temp">
<include name="${warFilename}.zip"/>
</fileset>
</unzip>

Ant to delete subdirectories and files in subdirectories but not any file from the directory itself

My requirement is to delete all subdirectories from a specified directory, but NOT delete files in the specified directory.
I have fussed around with fileset and dirset and I could not get a single collection to do the job. What works is this:
<delete includeemptydirs="true" verbose="true" >
<fileset dir="release/reports" >
<exclude name="*.*" />
</fileset>
<dirset dir="release/reports" includes="**/*" />
</delete>
Isn't there a way to do this with one collection (either fileset or dirset)?
It should work like that :
<delete includeEmptyDirs="true">
<fileset dir="C:/yourdir" includes="**/*" excludes="*.*"/>
</delete>

How do I flatten the top level folder of a zip file with ant?

A lot of zip files have a root folder, how do I unpack the zip file and get rid of the root folder?
I know there is the globmapper:
<unzip dest="${dest.path}">
<fileset dir="${source.path}">
<include name="**/zipfile*.*.zip" />
</fileset>
<mapper>
<globmapper from="rootFolder/*" to="*" />
</mapper>
</unzip>
But what if I don't know the name of the root folder? Wildcards are not working e.g.
<globmapper from="root*Folder/*" to="*" />
Is there a way to use wildcards or a mapper/function that upacks without the root folder?
There's actually a separate mapper specifically made for this called cutdirsmapper. Give this a try:
<unzip dest="${dest.path}">
<fileset dir="${source.path}">
<include name="**/zipfile*.*.zip" />
</fileset>
<cutdirsmapper dirs="1" />
</unzip>

Copy files and folder to WEB-INF using Ant

I want to copy a .properties file from a certain location to my WEB-INF/classes/com/infiniti folder(in a WAR file).
I have gone through this link How to get Ant to copy properties file to classes directory
using which I can copy the .properties file to WEB-INF/classes/ but not to WEB-INF/classes/com/infiniti
Code I am using is:
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
<lib dir="${lib}">
.......
.......
.......
<classes dir="${configHome}/config/com/infiniti">
<include name="accredit.properties" />
</classes>
...
....
.......
</war>
Also I need to copy ${configHome}/resources/com/infiniti/errorcode folder to
WEB-INF/classes/com/infiniti.
Is this possible using Ant?
yes, you can use for instance ZipFileSet like this
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
...
<zipfileset dir="${configHome}/config/com/infiniti" includes="**/*.properties" prefix="WEB-INF/classes/com/infiniti"/>
Yes, it's possible using ant. Just use the copy or sync commands to move your file:
<copy todir="${distribution}/location" file="${local.path}/data/file.txt">
</copy>
You can also copy with rules:
<copy includeemptydirs="false" todir="${combined.bin}">
<fileset dir="${buildbin}"/>
<fileset dir="${output2buildbin}"/>
<fileset dir="${output3buildbin}"/>
</copy>
Using sync:
<sync includeemptydirs="false" todir="${distres}">
<fileset dir="${buildres}">
<include name="logging/**" />
</fileset>
</sync>
Tasks are described on their doc site:
http://ant.apache.org/manual/Tasks/copy.html
The same 'fileset' declarative applies to the war task:
Examples
Assume the following structure in the project's base directory:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
http://ant.apache.org/manual/Tasks/war.html

Ant Zip Extracted Parent Directory

I have several zip files that I need to unzip within an Ant target. All the zip files are in the same directory, and have the same internal directory and file structure.
So I am using the following snippet to unzip all the zip files in the directory, but each zip file does not contain a parent folder at the root, so each successive zip file is unzipped and overwrites the previous files.
<unzip dest="C:/Program Files/Samsung/Samsung TV Apps SDK/Apps">
<fileset dir=".">
<include name="**/*.zip"/>
</fileset>
</unzip>
Is there a better way to unzip a group of files, and create a directory to unzip them to that is based on the zip file name?
So, if the zip files are:
1.zip
2.zip
3.zip
then the content of each will be extracted to:
1/
2/
3/
Thanks
One solution might be to use the ant-contrib 'for' and 'propertyregex' tasks to do this:
<for param="my.zip">
<fileset dir="." includes="**/*.zip" />
<sequential>
<propertyregex property="my.zip.dir"
input="#{my.zip}"
regexp="(.*)\..*"
select="\1"
override="yes" />
<unzip src="#{my.zip}" dest="${my.zip.dir}" />
</sequential>
</for>
The 'propertyregex' strips the .zip extension from the zip file name to use as the target directory name.
Without ant-contrib:
https://stackoverflow.com/a/12169523/957081
<!-- Get the path of the war file. I know the file name pattern in this case -->
<path id="warFilePath">
<fileset dir="./tomcat/webapps/">
<include name="myApp-*.war"/>
</fileset>
</path>
<property name="warFile" refid="warFilePath" />
<!-- Get file name without extension -->
<basename property="warFilename" file="${warFile}" suffix=".war" />
<!-- Create directory with the same name as the war file name -->
<mkdir dir="./tomcat/webapps/${warFilename}" />
<!-- unzip war file -->
<unwar dest="./tomcat/webapps/${warFilename}">
<fileset dir="./tomcat/webapps/">
<include name="${warFilename}.war"/>
</fileset>
</unwar>

Resources