Preserve file permissions when unzipping and the zipping files using ant - ant

I'm writing an ant build.xml file which does the following:
Takes a zipped folder (.zip)
Unzips it
Adds a number of files
Zips up the resulting files
An extract of the code from build.xml:
<!-- Unzip SDK to a temporary directory -->
<unzip src="${zipFile}" dest="tmp"/>
<!-- pull in the files from another directory -->
<copy todir="tmp/someDirectory" >
<fileset dir="${addedFiles}" />
</copy>
<!-- Zip up modified SDK -->
<zip destfile="${destDir}" basedir="tmp"/>
This all works perfectly, except that the permissions set for the zipped files prior to running the ant build are lost in the zip file created by the ant build. For example, files which were previously executable no longer are.
So my question: is it possible to use ant to add files to a zip archive without destroying the permissions of the already present files?
I'm using Ant 1.7.1

I encountered same issue when using Ant unzip target:
<unzip src="${project.build.directory}/${project.build.finalName}.zip" dest="${user.home}/apps" overwrite="true" />
The permission of shell scripts inside the zip file was lost when using the unzip target above.
After some investigation, I use the follow 'exec' ant target with unzip command line parameters, it worked.
<!-- Use command line unzip to keep file permissions -->
<exec executable="unzip" spawn="true">
<arg line="-o ${project.build.directory}/${project.build.finalName}.zip -d ${user.home}/apps" />
</exec>
I hope this can help someone else when encountering this kind of issues.
Thanks,
J

You can't get the zip task to preserve file permissions, but you can set them explicitly:
<zip destfile="installer.zip" >
<zipfileset filemode="755" dir="../" includes="artisan/install.*" />
</zip>
(This worked for me on Windows and OSX)

Turns out that ant will destroy all information on permissions when unzipping due to a restriction in Java. However, what is possible is to add files to an existing zip file which preserves the permissions of the existing files:
<!-- Add to zip -->
<zip destfile="${existingZipFiledirectory}.zip"
basedir="${directoryOfFilesToAdd}"
update="true"
/>
The above script will update the zip file specified with the content in basedir, preserving file permissions in the original zip.

As far as I know, this feature (preserve0permissions) was introduced with Ant 1.8. Previous versions of Ant did not kept the permissions.
If you're stuck with Ant 1.7.1, you can use Tar that -if I'm not mistaken- stores the permissions.

Related

How do you make a file to read only in an ant build?

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>

Building project.war file Via ant missed the files in java->server folder

I am creating project.war file using ant and
worklight-ant-builder.jar file in the MFP.
While my project is build using mfp or via eclipse in the bin the .war file and classes folder have all the things including content in my server->java folder.
But when i build the ant file using step 1 i dont get the files in my server->java folder.
Note i have verified this by using 7-zip to see the contents in the .war file and also seen the folder classes created by the mobileFirst which contains server->java folder content when i use step 2.
Image Containing the peluk folder which is inside the Java->server->com ( Built using Step 2)
Image which dont have the peluk folder which is inside the Java->server->com (Built using Step 1)
I am using the following ant code.
<taskdef resource="com/worklight/ant/defaults.properties">
<classpath>
<pathelement location="${res.location}\Resources\worklight-ant-builder.jar"/>
</classpath>
</taskdef>
<war-builder projectfolder="bin"
warfile="bin\${proj.brcname}.war"
classesFolder="classes-folder"/>
As #dhineshsundar mentioned in the comments: If you have custom Java code and you're planning on building a project using the Ant task script then you must first compile the Java code into a .class file and specify its location in the Ant task script. Then when building the project using the Ant task script it will also pick up the .class file.
The above does not happen automatically like it happens when using MobileFirst Studio.
See this user documentation topic: http://www-01.ibm.com/support/knowledgecenter/SSHS8R_7.0.0/com.ibm.worklight.deploy.doc/devref/r_ant_tasks_deploy_projects.html
<?xml version="1.0" encoding="UTF-8"?>
<project name="myProject" default="all">
<taskdef resource="com/worklight/ant/defaults.properties">
<classpath>
<pathelement location="cli_install_dir/public/worklight-ant-builder.jar"/>
</classpath>
</taskdef>
<target name="all">
<war-builder projectfolder="."
destinationfolder="bin/war"
warfile="bin/project.war"
classesFolder="classes-folder"/>
</target>
</project>
The <war-builder> element has the following attributes:
The projectfolder attribute specifies the path to your project.
The destinationfolder attribute specifies a folder for holding temporary files.
The warfile attribute specifies the destination and file name of the generated .war file
The classesFolder attribute specifies a folder with compiled Java™ classes to - add to the .war file. .jar files in the projectfolder\server\lib directory are added automatically

How to list all zip files in a specified directory?

I am trying to retrieve the list of all zip files present in a directory.
I have a directory ${src} which contains some zip files and I am using the following code
<target name="test">
<dirset id="dist.contents" dir="${src}" includes="*.zip"/>
<property name="prop.dist.contents" refid="dist.contents"/>
<echo message="${prop.dist.contents}" />
</target>
but it only echoes null.
I also tried includes="*" but it didn't make any difference.
I am using Ant version 1.7.0.
As written in the documentation:
A DirSet is a group of directories.
So since you want zip files, you should use a FileSet.

Cannot Get Files From FTP using ANT

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

Ant read a property file from within a zip file

Is there a way in ant to load properties from inside a zip file?
I have a project ant file which needs to use some properties that are in a file that is inside a zip file. The zip file is stored in a known location on our CI server.
/known location/file.zip
|
+--- properties/details.properties
The following doesn't work
<project name="test" basedir="." >
<property file="/known location/file.zip/properties/details.properties"/>
....
</project>
Since zip files and jar files are basically the same, you can use the url form of the property task, with a jar url.
<property url="jar:file:/known location/file.zip!/properties/details.properties" />
Note the jar:file: at the front of the url, and the !/ separating the zip file location from the path of the properties file within the zip.
See the JarURLConnection docs for more info on the syntax of a jar: url.
you could unzip the file to a temp location and then load the unzipped properties file
<target name="load-zipped-props">
<unzip src="${propfile-name}.zip" dest="${unzip-destination}" />
<property file="${unzip-destination}/${propfile-name}.properties"/>
</target>

Resources