ant sshexec terminating without giving any message - ant

I am trying to unzip a zip file which is available in the remote server.
After completing there is a log entry and this is not coming at all.
output file also don't have much details.
1.) how can I disable sshexec console print?
2.) Why this is not printing final logger statement?
3.) Is this not extracting full zip file?
<target name="unzip">
<echo level="info">==== Extracting zip file ====</echo>
<sshexec host="${host}" username="${user}" password="${password}" output="${log.file}" command="unzip -o ${path}/${zipdata}.zip -d ${path2}/${zipdata}/" trust="true" />
<echo level="info">==== Extracting zip file completed====</echo>
</target>

Related

Exception when using sshexec in ant 1.10

I'm trying deploy a PHP application using Ant script. I've put all the necessary jars in the library: ie ant-jsch-1.8.1.jar, jsch-0.1.55.jar
In my script i declare the sshexec and scp as followed:
<path id="jsch.class.path">
<pathelement location="lib/ant-jsch-1.8.1.jar" />
<pathelement location="lib/jsch-0.1.55.jar" />
</path>
<taskdef name="scp" classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp" classpathref="jsch.class.path" />
<taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec" classpathref="jsch.class.path" />
When the build is executed in Jenkins i get these errors event though the location of the public key is correct:
[sshexec] Caught exception: java.io.FileNotFoundException: /home/jenkins/.ssh/id_dsa (No such file or directory)
[sshexec] Caught exception: java.io.FileNotFoundException: /home/jenkins/.ssh/id_dsa (No such file or directory)
[scp] Caught exception: java.io.FileNotFoundException: /home/jenkins/.ssh/id_dsa (No such file or directory)
The weird thing is when i run the build script locally on my machine it works fine.
Any help would be much appreciated.
The issue was that I had a variable that holds the path to the private key, the string probably had an extra space or character and ANT got confused. I replaced variable with the direct path of the private key and it worked. To reuse the variable I copied the path and assigned it to the same variable in run command.

Ant: How to call a directory with php files and save the result as overwritten files

How can I use Ant to execute each php file in a directory? The output for the php execution should overwrite the original file.
The directory is on a localhost webserver with php installed. I assume the solution would involve GET and have src point to the http equivalent of each file.
Some dynamic version of
c:/webroot/php/file1.php
c:/webroot/php/file2.php
translate into
get src="localhost/php/file1.php"
get src="localhost/php/file2.php"
and then have the output overwrite itself.
Why not use the php command line combined with the ANT apply task?
Something like:
<apply executable="C:\PHP5\php.exe">
<arg value="-f"/>
<fileset dir="c:/webroot/php" includes="*.php"/>
</apply>
It's not clear to me why you want to overwrite your source files....
Update
Alternatively use an embedded groovy script
<fileset id="phpfiles" dir="c:/webroot/php" includes="*.php"/>
<groovy>
project.references.phpfiles.each {
def file = new File(it.toString())
ant.get(src:"http://localhost/php/${file.name}", dest:"output/${file.name}")
}
</groovy>

sshexec command setup

I need to run ant script from a local machine which will invoke ant executions on remote machine.
So on the local ant file I have :
<target name="test">
<sshexec host="${host}" username="${user}"
password="${pwd}" trust="yes"
commandResource="(cd F:\execution; ant -f build.xml run)"/>
</target>
On remote machine I have build.xml` which contains
<target name="run">
<mkdir dir ="F:\Testfolder"/>
</target>
When I execute the loca ant script I get the following error :
java.io.FileNotFoundException: (cd F:\execution; ant -f build.xml run)
(The filename, directory name, or volume label syntax is incorrect)
Why am I getting this error?
We deploy a build file named "remote-build.xml" to the path /root/project/remote-build.xml on the remote machine, and then we use
<sshexec host="${host}"
username="${user}"
password="${pwd}"
trust="yes"
command="ant -f /root/project/remote-build.xml the-targets-to-execute" />
to execute the target.
You could create a file in remote machine ( F:\execution\runmanycommands.sh and execute that file . From the site
Run a set of commands from a command resource (file) on a remote
machine using key authentication with no passphrase
The commandResource expects single resource file execute. '(cd F:\execution; ant -f build.xml run)' is not a resource.
(I am not sure your over all goal. Looking at the number questions, I guess you need Continous Integration solution- should check jenkins with many of its plugins )
(BTW, you can all ant directly with ant -f , avoiding the need for cd)

How to create temporary directory in ant?

I'd like to create a temporary directory in ant (version 1.6.5) and assign it to a property.
The command "mktemp -d" would be ideal for this, but I cannot find similar functionality from inside ant
I can't find any official function in the docs apart from the tempfile task which apparently only creates files, not directories.
I'm considering using exec to call tempfile and get the result, however this will make my build.xml dependent on UNIX/linux, which I'd like to avoid.
Background: I'm trying to speed up an existing build process which builds inside networked filesystem. The build already copies all the source to a temporary directory, however this is on the same filesystem. I've tested changing this to /tmp/foo and it gives a worthwhile speed increase: 3mins vs 4mins.
You could combine the tempfile task with the java.io.tmpdir system property to get a file path to use to create a temporary dir:
<project default="test">
<target name="test">
<echo>${java.io.tmpdir}</echo>
<tempfile property="temp.file" destDir="${java.io.tmpdir}" prefix="build"/>
<echo>${temp.file}</echo>
</target>
</project>
Note that the tempfile task does not create the file (unless you ask it to). It just sets a property which you can use to create a file or dir.
This task sets a property to the name of a temporary file. Unlike
java.io.File.createTempFile, this task does not actually create the
temporary file, but it does guarantee that the file did not exist when
the task was executed.
Output in my environment:
test:
[echo] C:\Users\sudocode\AppData\Local\Temp\
[echo] C:\Users\sudocode\AppData\Local\Temp\build1749402932
The answer above only hints at how to create a temporary directory. The point is that merely returns a string. A more complete answer is
<target name="temptest" description="test making tempdir">
<tempfile property="mytempdir" destdir="${java.io.tmpdir}"/>
<tempfile property="mytempfile" destdir="${mytempdir}"/>
<tstamp>
<format property="now" pattern="MMMM dd yyyy"/>
</tstamp>
<copy tofile="${mytempfile}">
<string value="today=${now}"/>
</copy>
<property file="${mytempfile}"/>
<echo message="It it now ${today}"/>
</target>

Passing filepath to ANT from BAT script

I am trying to invoke an ANT target from Windows (right-click) file context menu.
I have setup the registry entries to invoke a batch script which invokes my ANT EXEC target.
I need to pass the path of the file (on which user right-clicked) to my ANT target. So I am using %~dp1 to set an ANT properties in my bat script:
Set tobeusedfilepath=%~dp1
Set tobeusedfile=%~n1
resulting in:
tobeusedfilepath=D:\Project\Rel L\
tobeusedfile=file
The problem is %~dp1 returns a string with "\" as file separator. But ANT EXEC task wants "/"
[exec] '-source'
[exec] 'D:ProjectRel L/file'
[exec] ......
[exec] The file, 'D:ProjectRel L/file', does not exist.
Any suggestions how to get around this path separators?
set AntPath="D:\Project\Rel L\"
set AntPath=%AntPath:\=/%
set AntPath=%AntPath::/=:%
gives
set AntPath="D:\Project\Rel L\"
set AntPath="D:/Project/Rel L/"
set AntPath="D:Project/Rel L/"
If you are running on Windows Ant will happily accept OS directory separator which is \.
Upon examination of the output of your program I see that the path separators are missing: you have D:ProjectRel not D:\Project\Rel. I may only guess that you are trying to exec a Cygwin program. Cygwin programs will use \ as an escape character. Therefore you need to use a <pathconvert> property to adjust the directory separators.
Code snippet below illustrates how to do this
<property name="tobeusedfilepath" location="D:\Project\Rel L\"/>
<property name="tobeusedfile" value="file"/>
<property name="system-path-filename"
location="${tobeusedfilepath}/${tobeusedfile}"
/>
<pathconvert property="unixized-filename" targetos="unix">
<path location="${system-path-filename}"/>
</pathconvert>
<echo message="system-path-filename=${system-path-filename}"/>
<echo message="unixized-filename=${unixized-filename}"/>
And here is the output of this run:
[echo] system-path-filename=D:\Project\Rel L\file
[echo] unixized-filename=D:/Project/Rel L/file

Resources