Any analog of rsync for Ant? - ant

I need some analog of rsync for ant. The issue is to copy files from source directory to the set of subdirectories that was previously accomplished with script
rsync -r --ignore-existing $BASE_DIR/common/config/* $BASE_DIR/config
Thanks for any help

You can use exec to call rsync from Ant, you could use the java task to call Jarsync or java-sync or you could create a custom ant task to call either of those libraries.

Zombie question but posting https://gist.github.com/garethr/878364 in case I ever search for it myself again. Pasting Gist content in case something something.
<project name="{{ name }}" default="help" basedir=".">
<property name="username" value="{{ username }}"/>
<property name="host" value="{{ host }}"/>
<property name="dir" value="/srv/{{ path }}/"/>
<tstamp>
<format property="TODAY_UK" pattern="yyyyMMddhhmmss" locale="en,UK"/>
</tstamp>
<target name="help" description="show available commands" >
<exec executable="ant" dir="." failonerror="true">
<arg value="-p"/>
</exec>
</target>
<target name="deploy-to" description="show where we are deploying to" >
<echo>${username}#${host}:${dir}</echo>
</target>
<target name="deploy" description="deploy usng rsync" >
<exec executable="rsync" dir="." failonerror="true">
<arg value="-r"/>
<arg value="."/>
<arg value="${username}#${host}:${dir}"/>
<arg value="--exclude-from=rsync.excludes"/>
<arg value="-v"/>
</exec>
</target>
<target name="deploy-test" description="test deploy usng rsync with the dry run flag set" >
<exec executable="rsync" dir="." failonerror="true">
<arg value="-r"/>
<arg value="."/>
<arg value="${username}#${host}:${dir}"/>
<arg value="--exclude-from=rsync.excludes"/>
<arg value="--dry-run"/>
<arg value="-v"/>
</exec>
</target>
<target name="backup" description="backup site" >
<exec executable="scp" dir="." failonerror="true">
<arg value="-r"/>
<arg value="${username}#${host}:${dir}"/>
<arg value="backups/${TODAY_UK}"/>
</exec>
</target>
</project>

Related

How to access values passed from ant script to shell script

We have a shell script which we invoke it from an ant script. We need to pass input to shell script from ant script. It can be done as follows
<target name="help">
<exec executable="cmd">
<arg value="Hello"/>
<arg value="Welcome"/>
<arg value="World"/>
</exec>
</target>
But We are not able to figure out how to access the values passed from ant script in shell script. Could anyone please suggest me with the right information.Thanks.
Use properties as input, something like :
<project>
<property name="foobar" location="C:/foobar" />
<property name="foo" value="bar" />
<exec executable="cmd">
<arg value="/c" />
<env key="PATH" path="${env.PATH};${foobar}/bin" />
<arg value="set" />
</exec>
<exec executable="cmd">
<arg value="/c" />
<arg value="echo" />
<arg value="${foo}" />
</exec>
</project>
You have to use /c as first arg value.
When calling a batfile which expects %1 ... %9 as input, first arg is <arg value=/c">,second arg <arg value="yourbatfile.bat/>.The following args <arg value=.../> would be %1 and so on, f.e. :
foobar.bat
#echo off
echo First argument %1
echo Second argument %2
build.xml
<project>
<exec dir="dir="path/to/batfile" executable="cmd">
<arg value="/c"/>
<arg value="foobar.bat"/>
<arg value="foo"/>
<arg value="bar"/>
</exec>
</project>
output
[exec] First argument foo
[exec] Second argument bar
Example for calling a shellscript, first arg has to be <arg value="/path/to/shellscript.sh"/>, the following args <arg value="..."/> will be $1 ...
foobar.sh
#!/bin/bash
echo "\$# = $#"
echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"
build.xml
<project>
<exec executable="/bin/bash">
<arg value="/path/to/foobar.sh"/>
<arg value="foo"/>
<arg value="bar"/>
</exec>
</project>

No output running yui-compressor from Ant

I'm going to minify my js files by yuicomressor via ant, I wrote this:
<property name="concat-js-file-name" value="main.concat.js"/>
<property name="concat-js-file-path" value="${temp-folder}/js/${concat-js-file-name}"/>
<property name="yui-jar-path" value="lib/yuicompressor-2.4.7.jar"/>
<target name="minification" depends="concatation">
<echo>---Minification is started</echo>
<java jar="${yui-jar-path}" fork="true">
<arg value="${concat-js-file-path}"/>
<arg value="-o minified.js"/>
</java>
<echo>---Minification is finished successfully...</echo>
</target>
The problem is the output file is not generated!
Any idea?
You should set <java ... failonerror="true"/> and increase the noiselevel to see what's going on, means start your ant build with ant -f yourbuild.xml -debug
Actually, after some tries I found a solution:
I used <arg line="-o outputfile inputfile"/> instead, and it worked.
I suggest using <arg value="..."> instead of <arg line="...">. <arg value="..."> ensures that each command line argument has quotes around it, if necessary.
In the case of yui-compressor, the "-o" and "<file>" arguments should each go in their own <arg value="..."> elements:
<java jar="${yui-jar-path}" fork="true">
<arg value="-o"/>
<arg value="minified.js"/>
<arg value="${concat-js-file-path}"/>
</java>

build.xml ignoring <echo> tags and if options

I have a sencha Ext.js project and I am trying to get user input and take action based on that user input in my build.xml file.
Here's my build.xml:
<?xml version="1.0" encoding="utf-8"?>
<project xmlns:if="ant:if" xmlns:unless="ant:unless" name="PSOGN" default=".help">
<import file="${basedir}/.sencha/app/build-impl.xml"/>
<target name="-after-build">
<echo>57 ...</echo>
<property name="realperl" value="/usr/bin/perl"/>
<echo>59 ...</echo>
<exec executable="/bin/hostname" outputproperty="myhost">
<arg value="-s"/>
</exec>
<echo>64 ...</echo>
<exec executable="${realperl}" outputproperty="env">
<arg value="../bin/getCf.pl"/>
<arg value="--config"/>
<arg value="../etc/config/currentCf"/>
<arg value="ENV"/>
</exec>
<echo>70 ...</echo>
<input message="Install ${env} version on ${myhost}?" addproperty="install" validargs="y,n" />
<echo>72 ...</echo>
<condition property="doinstall">
<equals arg1="${install}" arg2="y" />
</condition>
<echo>Hello ...</echo>
<exec executable="${realperl}" outputproperty="htdocspath">
<arg value="../bin/getCf.pl"/>
<arg value="--config"/>
<arg value="../etc/config/currentCf"/>
<arg value="HTDOCSPATH"/>
</exec>
<echo>do install is ${doinstall}</echo>
<echo if:true="${doinstall}">Please wait. Executing rsync to ${htdocspath}. Output logged to /tmp/rsync.log.</echo>
<echo if:false="${doinstall}">Skipping rsync to ${htdocspath}.</echo>
<exec executable="/usr/local/bin/rsync" if:true="${doinstall}">
<arg value="--exclude='*.log'" />
<arg value="--chmod=ugo=rwX" />
<arg value="-rltDq" />
<arg value="../info"/>
<arg value="../sbin"/>
<arg value="../bin"/>
<arg value="../cec"/>
<arg value="../cec.list.html"/>
<arg value="../cgi"/>
<arg value="../common"/>
<arg value="../dblib"/>
<arg value="../etc"/>
<arg value="../ext-2.3.0"/>
<arg value="../framework"/>
<arg value="../help"/>
<arg value="../icon-loading-animated.gif"/>
<arg value="../images"/>
<arg value="../img"/>
<arg value="../include"/>
<arg value="../index.shtml"/>
<arg value="../kicker"/>
<arg value="../lib"/>
<arg value="../msgs"/>
<arg value="../src"/>
<arg value="../thingold"/>
<arg value="../thing"/>
<arg value="${htdocspath}"/>
<redirector output="/tmp/rsync.log" alwayslog="true"/>
</exec>
</target>
</project>
When I run sencha app build which invokes ant I only get this output from by build.xml:
Install Dev version on tools-dev1? (y, n)
There is no output from my <echo... tags and if I answer 'n' I can that the rsync command executes. What am I doing wrong here?
I simplified your build.xml
<project xmlns:if="ant:if" xmlns:unless="ant:unless" name="PSOGN" >
<input message="Install ?" addproperty="install" validargs="y,n" />
<condition property="doinstall">
<equals arg1="${install}" arg2="y" />
</condition>
<echo>do install is ${doinstall}</echo>
<echo if:true="${doinstall}">Please wait. Executing rsync</echo>
<echo unless:true="${doinstall}">Skipping rsync </echo>
<exec executable="echo" if:true="${doinstall}">
<arg value="ECHO IS EXECUTED" />
<redirector output="./rsync.log" alwayslog="true"/>
</exec>
</project>
and I replaced if:false with unless:true
<echo unless:true="${doinstall}">Skipping rsync </echo>
Everything works fine:
oleg#oleg-ThinkPad-X201:~/temp/aa$ ant
Buildfile: /home/oleg/temp/aa/build.xml
[input] Install ? (y, n)
y
[echo] do install is true
[echo] Please wait. Executing rsync
[exec] ECHO IS EXECUTED
BUILD SUCCESSFUL
Total time: 2 seconds
oleg#oleg-ThinkPad-X201:~/temp/aa$ ant
Buildfile: /home/oleg/temp/aa/build.xml
[input] Install ? (y, n)
n
[echo] do install is ${doinstall}
[echo] Skipping rsync
BUILD SUCCESSFUL
Total time: 2 seconds

Zend Server: Application name already exists

we have a problem on our Zend Server during the deployment of an application.
We've set up a virtual host on our Zend Server called "dev.application-7.de".
We are trying to deploy our Zend Framework 2 project over Jenkins to this virtual host. Usually this works well.
But we got the following error in 3 out of 170 builds:
deploy:
[exec] ======================================================================
[exec] The application has thrown an exception!
[exec] ======================================================================
[exec] ZendServerWebApi\Model\Exception\ApiException
[exec] Invalid userAppName parameter: Application name 'application-7' already exists
[exec] 2015-03-13T11:35:12+01:00 ERR (3): Invalid userAppName parameter: Application name 'application-7' already exists<zendServerAPIResponse xmlns="http://www.zend.com/server/api/1.9">
[exec] <requestData>
[exec] <apiKeyName><![CDATA[ZendStudio]]></apiKeyName>
[exec] <method>applicationDeploy</method>
[exec] </requestData>
[exec] <errorData>
[exec] <errorCode>applicationConflict</errorCode>
[exec] <errorMessage><![CDATA[Invalid userAppName parameter: Application name 'application-7' already exists]]></errorMessage>
[exec] </errorData></zendServerAPIResponse>
To fix this problem, we deleted the virtual host and recreated it with the same config mentioned below.
Is there another way to fix the problem?
Further Information
Zend Server Version:
Version 8.0.2, Developer Standard Edition, Development profile
OS Version:
Ubuntu 14.04.1 LTS
Jenkins Version:
1.599
Information about the Virtual-Host:
Type: Zend Server defined
Created at: 27/Feb/2015 8:18:23
Document Root: /usr/local/zend/var/apps/https/dev.application-7.de/443/1.0.0_177/public/
Security: SSL enabled
Certificate File Path /etc/apache2/ssl/dev.application-7.de.crt
Key File Path /etc/apache2/ssl/dev.application-7.de.key
Chain File Path /etc/apache2/ssl/dev.application-7.de.csr
Virtual-Hosts Config:
<VirtualHost *:443>
SetEnv "APP_ENV" "dev"
DocumentRoot "/usr/local/zend/var/apps/https/dev.application-7.de/443/1.0.0_177/public/"
<Directory "/usr/local/zend/var/apps/https/dev.application-7.de/443/1.0.0_177/public/">
Options +Indexes +FollowSymLinks
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile "/etc/apache2/ssl/dev.application-7.de.crt"
SSLCertificateKeyFile "/etc/apache2/ssl/dev.application-7.de.key"
SSLCertificateChainFile "/etc/apache2/ssl/dev.application-7.de.csr"
ServerName dev.application-7.de:443
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
# include the folder containing the vhost aliases for zend server deployment
IncludeOptional "/usr/local/zend/etc/sites.d/https/dev.application-7.de/443/*.conf"
</VirtualHost>
Ant Build File:
<?xml version="1.0" encoding="UTF-8"?>
<project name="application-7-de-2" default="build">
<!-- By default, we assume all tools to be on the $PATH -->
<!-- <property name="toolsdir" value=""/> -->
<!-- Uncomment the following when the tools are in ${basedir}/vendor/bin -->
<!-- <property name="toolsdir" value="${basedir}/vendor/bin/"/> -->
<!-- Uncomment the following when the tools are in a custom path -->
<property name="baseuri" value="https://dev.application-7.de:443"/>
<property name="appname" value="application-7-de"/>
<property name="defaultserver" value="false"/>
<property name="toolsdir" value="/usr/local/zend/bin/"/>
<property name="params" value="APP_ENV=dev"/>
<property name="host" value="http://our-zend-server.de:10081"/>
<property name="key" value="ZendStudio"/>
<property name="secret" value="OUR_SECRET"/>
<target name="build" depends="prepare,composer,lint,phploc-ci,pdepend,phpmd-ci,phpcs-ci,phpcpd-ci,phpunit,phpdox,zpk,deploy" description=""/>
<target name="build-parallel" depends="prepare,lint,tools-parallel,phpunit,phpdox" description=""/>
<target name="tools-parallel" description="Run tools in parallel">
<parallel threadCount="2">
<sequential>
<antcall target="pdepend"/>
<antcall target="phpmd-ci"/>
</sequential>
<antcall target="phpcpd-ci"/>
<antcall target="phpcs-ci"/>
<antcall target="phploc-ci"/>
</parallel>
</target>
<target name="clean" unless="clean.done" description="Cleanup build artifacts">
<delete dir="${basedir}/build/api"/>
<delete dir="${basedir}/build/coverage"/>
<delete dir="${basedir}/build/logs"/>
<delete dir="${basedir}/build/pdepend"/>
<delete dir="${basedir}/build/phpdox"/>
<property name="clean.done" value="true"/>
</target>
<target name="prepare" unless="prepare.done" depends="clean" description="Prepare for build">
<mkdir dir="${basedir}/build/api"/>
<mkdir dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/logs"/>
<mkdir dir="${basedir}/build/pdepend"/>
<mkdir dir="${basedir}/build/phpdox"/>
<property name="prepare.done" value="true"/>
</target>
<target name="composer" depends="prepare" description="Update dependencies">
<exec executable="${toolsdir}composer" failonerror="true">
<arg value="update"/>
<arg value="--working-dir"/>
<arg path="${basedir}"/>
</exec>
</target>
<target name="lint" description="Perform syntax check of sourcecode files">
<apply executable="php" failonerror="true">
<arg value="-l" />
<fileset dir="${basedir}/module">
<include name="**/*.php" />
<modified />
</fileset>
<fileset dir="${basedir}/tests">
<include name="**/*.php" />
<modified />
</fileset>
</apply>
</target>
<target name="phploc" description="Measure project size using PHPLOC and print human readable output. Intended for usage on the command line.">
<exec executable="${toolsdir}phploc">
<arg value="--count-tests" />
<arg path="${basedir}/module" />
<arg path="${basedir}/tests" />
</exec>
</target>
<target name="phploc-ci" depends="prepare" description="Measure project size using PHPLOC and log result in CSV and XML format. Intended for usage within a continuous integration environment.">
<exec executable="${toolsdir}phploc">
<arg value="--count-tests" />
<arg value="--log-csv" />
<arg path="${basedir}/build/logs/phploc.csv" />
<arg value="--log-xml" />
<arg path="${basedir}/build/logs/phploc.xml" />
<arg path="${basedir}/module" />
<arg path="${basedir}/tests" />
</exec>
</target>
<target name="pdepend" depends="prepare" description="Calculate software metrics using PHP_Depend and log result in XML format. Intended for usage within a continuous integration environment.">
<exec executable="${toolsdir}pdepend">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" />
<arg path="${basedir}/module" />
</exec>
</target>
<target name="phpmd" description="Perform project mess detection using PHPMD and print human readable output. Intended for usage on the command line before committing.">
<exec executable="${toolsdir}phpmd">
<arg path="${basedir}/module" />
<arg value="text" />
<arg path="${basedir}/build/phpmd.xml" />
</exec>
</target>
<target name="phpmd-ci" depends="prepare" description="Perform project mess detection using PHPMD and log result in XML format. Intended for usage within a continuous integration environment.">
<exec executable="${toolsdir}phpmd">
<arg path="${basedir}/module" />
<arg value="xml" />
<arg path="${basedir}/build/phpmd.xml" />
<arg value="--reportfile" />
<arg path="${basedir}/build/logs/pmd.xml" />
</exec>
</target>
<target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer and print human readable output. Intended for usage on the command line before committing.">
<exec executable="${toolsdir}phpcs">
<arg value="--standard=PSR2" />
<arg value="--extensions=php" />
<arg value="--ignore=autoload.php" />
<arg path="${basedir}/module" />
<arg path="${basedir}/tests" />
</exec>
</target>
<target name="phpcs-ci" depends="prepare" description="Find coding standard violations using PHP_CodeSniffer and log result in XML format. Intended for usage within a continuous integration environment.">
<exec executable="${toolsdir}phpcs" output="/dev/null">
<arg value="--report=checkstyle" />
<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
<arg value="--standard=PSR2" />
<arg value="--extensions=php" />
<arg value="--ignore=autoload.php" />
<arg path="${basedir}/module" />
</exec>
</target>
<target name="phpcpd" description="Find duplicate code using PHPCPD and print human readable output. Intended for usage on the command line before committing.">
<exec executable="${toolsdir}phpcpd">
<arg path="${basedir}/module" />
</exec>
</target>
<target name="phpcpd-ci" depends="prepare" description="Find duplicate code using PHPCPD and log result in XML format. Intended for usage within a continuous integration environment.">
<exec executable="${toolsdir}phpcpd">
<arg value="--log-pmd" />
<arg path="${basedir}/build/logs/pmd-cpd.xml" />
<arg path="${basedir}/module" />
</exec>
</target>
<target name="phpunit" depends="prepare" description="Run unit tests with PHPUnit">
<exec executable="${toolsdir}phpunit" failonerror="true">
<arg value="--log-junit"/>
<arg value="build/logs/junit.xml"/>
<arg value="--configuration"/>
<arg path="${basedir}/tests/phpunit.xml"/>
</exec>
</target>
<target name="phpdox" depends="phploc-ci,phpcs-ci,phpmd-ci" description="Generate project documentation using phpDox">
<exec executable="${toolsdir}phpdox" dir="${basedir}/build"/>
</target>
<target name="zpk" depends="phpdox">
<exec executable="${toolsdir}zs-client" failonerror="true">
<arg value="packZpk"/>
<arg value="--folder=${basedir}"/>
<arg value="--destination=${basedir}"/>
<arg value="--name=application.zpk"/>
</exec>
</target>
<target name="deploy" depends="zpk">
<exec executable="${toolsdir}zs-client" failonerror="true">
<arg value="installApp"/>
<arg value="--baseUri=${baseuri}"/>
<arg value="--userAppName=${appname}"/>
<arg value="--defaultServer=${defaultserver}"/>
<arg value="--userParams=${params}"/>
<arg value="--zpk=${basedir}/application.zpk"/>
<arg value="--zsurl=${host}"/>
<arg value="--zskey=${key}"/>
<arg value="--zssecret=${secret}"/>
</exec>
</target>
</project>

How to pass a list of files to <exec>?

This is a part of my ant script:
<target>
<exec executable="find" outputproperty="found">
<arg value="src/main/java"/>
<arg line="-name '*.java'"/>
</exec>
<exec executable="xgettext">
<arg value="-k_"/>
<arg line="-o gettext.pot"/>
<arg line="${found}"/>
</exec>
</target>
Doesn't work because xgettext receives a quoted list of files and treats this list as a single file name. How to solve it?
You'd need to separate out each file to a separate arg for that to work.
You can supply a list-of-files file to process to 'xgettext' using the --files-from option.
How about something like this: write the 'find' output to a file, then reload into 'xgettext':
<target>
<exec executable="find" outputproperty="found">
<arg value="src/main/java"/>
<arg line="-name '*.java'"/>
</exec>
<echo file="xgettext.files" message="${found}" />
<exec executable="xgettext">
<arg value="-k_"/>
<arg value="-o" />
<arg value="gettext.pot"/>
<arg value="--files-from=xgettext.files"/>
</exec>
</target>
Alternatively, here's a variation that assumes you have the Bourne Shell sh - if you have something else you can probably adapt. This pipes the 'find' output directly to 'xgettext':
<exec executable="sh">
<arg value="-c"/>
<arg value="find src/main/java -name '*.java' | xgettext -k_ -o gettext.pot -f -"/>
</exec>

Resources