I wan to unzip multiple files in a folder with the same name as the file name using Ant? - 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>

Related

How do I copy files which have same name but different suffix in ant copy task?

I have written the below script which is copying files which were changed after the specified time. What i also want is to get the same files which has different suffix with these files.
<project name="copy latest files test" basedir="." xmlns:sf="antlib:com.salesforce">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="C:/apache-ant-1.9.6/lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<property name="src.dir" value="retrieveUnpackaged"/>
<property name="dest.dir" value="tempFolder"/>
<!-- Copy contents of classes folder from source folder -->
<target name="moveFolders" >
<copy toDir="${dest.dir}">
<fileset dir="${src.dir}">
<scriptselector language="javascript">
import(java.io.file);
self.setselected(true);
</scriptselector>
<include name="*/**"/>
<date datetime="04/20/2016 10:35 AM" when="after"/>
</fileset>
</copy>
</target>
I was trying to use , but couldnt figure it out.
The files that are getting copied will have another file in the same folder ending with "-meta.xml" as suffix. What I want is to get those files together as well.

How to copy a files to a particular dir using ant?

i need to copy test.bat into the bin folder
i worte a code in build.xml such that it should copy a file from bin/test.bat to dist/intall/windows/
but it not copying
project structure
project
|-dist-install-windows-bin
|-etc-bin-test.bat
|-src-build.xml
<target name="copy">
<copy todir="./dist/install/windows/bin">
<fileset dir=".">
<include name="etc/bin/test.bat"/>
</fileset>
</copy>
</target>
Give parent directory as "dir" attribute
<fileset dir="..">
<include name="etc/bin/test.bat"/>
</fileset>

How to include an empty folder in a zip file

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>

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