Ant : sshexec outputproperty not working as expected - ant

sshexec outputproperty not working as expected in Ant.
Ant Version : 1.9.4
Command :
<sshexec host="${Server}"
username="${User}"
Password="${Password}"
trust="true"
command="${cmd}"
failonerror="true"
outputproperty="sshexecOutput" />
<echo message="Value of outputproperty ${sshexecOutput}" />`
Logs :
[sshexec] Connecting to <Server_Name>:22
[sshexec] cmd : <SSHEXEC_COMMAND>
[sshexec] 1
[echo] value of outputproperty 0
Can you please help?

Related

What does code number 3 returned by exec Ant mean? [duplicate]

Im trying to invoke a bash scrip from an ant target. This is my target :
<target name="report" depends="test">
<!-- Step 3: Create coverage report -->
<exec executable="./checkStyle.sh"
failonerror="true"
osfamily="unix"/>
<jacoco:report>
<!-- This task needs the collected execution data and ... -->
<executiondata>
<file file="${result.exec.file}" />
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>
And my bash script is :
#!/bin/bash
DEST_FOLDER='./target/checkstyle'
CLASSES_FILE='classes.txt'
REPORT_FILE='report.xml'
mkdir -p "$DEST_FOLDER"
rm -f "$DEST_FOLDER/$CLASSES_FILE"
rm -f "$DEST_FOLDER/$REPORT_FILE"
find ./project -name "*.java" >> "$DEST_FOLDER/$CLASSES_FILE"
while read p; do
java -jar ./utilJars/checkstyle-6.5-all.jar -c ./sun_checks.xml -f xml $p >> "$DEST_FOLDER/$REPORT_FILE"
done < $DEST_FOLDER/$CLASSES_FILE
When typing ./checkStyle everything works fine, but when I try "ant report" the following error is raised:
BUILD FAILED
/home/luci/workspace/operations/build.xml:60: exec returned: 3
Total time: 4 seconds
I`ve searched on google and that code seems to be "permision denied", but i dont know how i could solve this problem.
Generally with Ant (and Java), you cannot directly execute shell scripts. You need to execute the interpreter/shell and give the script as an argument.
For example:
<exec executable="/bin/bash" failonerror="true" osfamily="unix">
<arg value="-c"/>
<arg value="./checkStyle.sh"/>
</exec>

Write "java -version" to a file using ant

I want to have a target in my script that records the user's java version in a file format. But for some reason the output to the command "java -version" cannot be recorded.
I tried running java -version > java.txt in terminal, which doesn't work either so I am guessing the result from java -version cannot be captured?
You can use the following exec task in your Ant build.xml:
<exec executable="java">
<arg value="-version"/>
<redirector output="java-version.txt" alwayslog="true"/>
</exec>
Simply use Ant builtin properties, see Ant Manual / Built-in Properties, f.e. :
<echoproperties prefix="java"/>
will print all Java properties.
There are 3 version related properties :
java.version
java.vm.specification.version
java.vm.version
So simply take the appropriate property:
<project>
<echo>
${java.version}
${java.vm.specification.version}
${java.vm.version}
</echo>
<!-- assuming you want ${java.version} -->
<echo file="foobar.txt">Java Version = ${java.version}</echo>
</project>
output :
[echo] 1.7.0_72
[echo] 1.7
[echo] 24.72-b04

Executing who -m from ant

I am trying to execute the "who -m" command from Apache ant without success.
Here is my ant script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="default" default="who.am.i">
<target name="who.am.i">
<exec executable="who" outputproperty="myOutput">
<arg value="-m"/>
</exec>
<echo message="I am = ${myOutput}"/>
</target>
</project>
The result is blank.
[echo] I am =
If I run exec without the argument, it displays the correct result:
<exec executable="who" outputproperty="myOutput">
</exec>
[echo] host.name = gary tty8 2014-02-03 12:04 (:0)
[echo] gary pts/0 2014-02-03 12:09 (:0)
[echo] gary pts/1 2014-02-03 12:23 (:0)
[echo] gary pts/2 2014-02-04 11:36 (:0)
[echo] gary pts/4 2014-02-05 13:27 (:0)
[echo] gary pts/7 2014-02-04 12:23 (:0)
[echo] gary pts/8 2014-02-06 12:44 (:0)
If I run the who -m command from a terminal it displays what I am looking for:
who -m
gary pts/8 2014-02-06 12:44 (:0)
Any ideas why ant is not accepting the -m argument?
Try executing as shell executable to see it that helps. It helps to invoke shell with exact unix command you want to run.
<exec executable="sh" outputproperty="myOutput">
<arg value="who -m"/>
</exec>
You don't mention which flavour of Unix you have, but on Solaris I get this error message when I try your task:
[echo] $ Must be attached to terminal for 'am I' option
In contrast, on OSX it appears to work, but says:
[echo] I am = mjc tty?? Feb 7 02:35
note the ?? - it's also not finding the terminal for the session.
I suspect that in your case it is silently failing for the same reason as the Solaris test - namely that the shell forked by Ant (i.e. by java) isn't associated with your terminal session.
(There may well be a workaround, but I'm not aware of it, and if there is one, it is unlikely to be portable.)
At the end of the day I decided to go another route.
In ~/.bashrc I added the following line:
who -m | awk '{print $5}' > ~/.whoami.out
And to make it global I just added it to /etc/bashrc
This will write to the ~/.whoami.out file every time I log into the remote system.
In my ant script I read the content of this file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="default" default="default">
<target name="test.who.key">
<loadfile property="who.key" srcFile="${user.home}/.whoami.out" failonerror="false"/>
<condition property="who.cond">
<isset property="who.key"/>
</condition>
<condition property="who.cond2">
<not>
<isset property="who.key"/>
</not>
</condition>
</target>
<target name="init.who.key" depends="test.who.key" if="who.cond">
<echo message="WHO EXIST"/>
<property name="whoAmI" value="${who.key}"/>
</target>
<target name="init.not.who.key" depends="test.who.key" if="who.cond2">
<echo message="WHO DOES NOT EXIST"/>
<property name="whoAmI" value=""/>
</target>
<target name="default" depends="init.who.key, init.not.who.key">
<echo message="whoAmI = ${whoAmI}"/>
</target>
</project>

Ant: Why are my CLI args not persisting when I invoke targets that use them?

I have a property and target in my build.xml:
<property name="somedir" value="path/to/dir/${prop}"/>
<echo message="${prop}"/>
<target name="foo">
<echo message="Property: ${somedir}"/>
</target>
In the directory where build.xml is I run:
ant -Dprop="someVal" foo
This gets echoed:
[echo] someVal
[echo] Property: path/to/dir/
What happened to ${prop} when foo is called? How do I get the value to persist when foo is invoked?
Thanks in advance!
I can't reproduce your issue. What version of ANT are you using?
build.xml
<project name="demo" default="foo">
<property name="somedir" value="path/to/dir/${prop}"/>
<echo message="${prop}"/>
<target name="foo">
<echo message="Property: ${somedir}"/>
</target>
</project>
Run as follows:
$ ant -Dprop=hello
Buildfile: /home/me/tmp/build.xml
[echo] hello
foo:
[echo] Property: path/to/dir/hello
ANT version:
$ ant -version
Apache Ant(TM) version 1.9.0 compiled on March 5 2013
I'm using Ant version 1.8.4
I have this as my build.xml
<project>
<property name="somedir" value="path/to/dir/${prop}"/>
<echo message="${prop}"/>
<target name="foo">
<echo message="Property: ${somedir}"/>
</target>
</project>
I get this:
$ ant -Dprop=foo foo
Buildfile: /Users/david/build.xml
[echo] foo
foo:
[echo] Property: path/to/dir/foo
BUILD SUCCESSFUL
Total time: 0 seconds
I had to put <project> and </project> in the build.xml or else it wouldn't execute.
Is there something I'm missing? It seems to work fine.
I was trying to overwrite a pre-defined property that is immutable from the build.xml ...this is more along the lines of what I'm trying to do:
<project>
<property name="basedir" value="/usr/me/${prop}"/>
<echo message="${basedir}"/>
</project>
echos:
Buildfile: build.xml
[echo] /usr/me
BUILD SUCCESSFUL
Total time: 0 seconds
facepalm

Exiting ant target without failing build

I'm looking for a way to exit a target and continue running the ant code without exiting the overall process. Something like the below but I get errors.
<target name="foo">
<some-task/>
<another-task/>
<condition property="should.exit">
(condition check here....)
</condition>
<exit if="should.exit" message="some message"/>
(skipped tasks if "should.exit" is true)
</target>
Errors I get:
Problem: failed to create task or type exit
[sshexec] [echo] Cause: The name is undefined.
[sshexec]
[sshexec] [echo] Action: Check the spelling.
[sshexec]
[sshexec] [echo] Action: Check that any custom tasks/types have been declared.
[sshexec] [echo] Action: Check that any <presetdef>/<macrodef> declarations have taken place.
[sshexec]
[sshexec] [echo] [sshexec]
[sshexec] [echo] ERROR Error error
<target name="foo">
<some-task/>
<another-task/>
<condition property="should.exit">
(condition check here....)
</condition>
<fail message="failing">
<condition>
<equals arg1="${should.exit}" arg2="true"/>
</condition>
</fail>
(skipped tasks if "should.exit" is true)
</target>

Resources