Ant # variable not resolved - ant

Here is my snippet of build.xml
<target name="compile">
<for param="rsync.destination.host" list="${file}" delimiter="${line.separator}">
<sequential>
<echo message="${rsync.ssh.user}##{rsync.destination.host}:${rsync.destination.base.dir}/"/>
</sequential>
</for>
</target>
I received following output,
compile:
[echo] root#{rsync.destination.host}:/tmp/
[echo] root#{rsync.destination.host}:/tmp/
BUILD SUCCESSFUL
Total time: 0 seconds
So here #{rsync.destination.host} variable is not interpreted because of double ## character. If I put space in between them
<echo message="${rsync.ssh.user}# #{rsync.destination.host}:${rsync.destination.base.dir}/"/>
then variable is resolved as expected.
compile:
[echo] root# server1:/tmp/
[echo] root# server2:/tmp/
BUILD SUCCESSFUL
Total time: 0 seconds
Since there is space in username and server it will through exception if we perform ssh here. Any idea how to solve this problem.

There are no variables in (core) ant, but properties and attributes.
#{foo} is the syntax for accessing the value of a macrodef attribute inside a macrodef. As antcontrib for task uses ant macrodef task under the hood it has the same syntax.
Try with :
...
<echo message="${rsync.ssh.user}###{rsync.destination.host}:${rsync.destination.base.dir}/"/>
...
means use 3x # instead of 2x #
The param #{rsync.destination.host} has to be masked with a second #, so in fact you need to use # 3 times.

You could put the literal at-sign in a property:
<property name="at" value="#"/>
<echo message="${rsync.ssh.user}${at}#{rsync.destination.host}:${rsync.destination.base.dir}/"/>

Related

Checksum task in Ant not working as expected

I am struggling with a small ant file/target that is as follows:
<project name="test" default="test" basedir=".">
<property name="out.dir" value="${basedir}/out/"/>
<property name="apidoc.path" value="${out.dir}test.zip"/>
<property name="apidoc.input" value="${basedir}/../source//apidocs"/>
<property name="apidoc.sha" value="TODO"/>
<target name="test">
<echo message="Starting target APIDOC"/>
<zip destfile="${apidoc.path}" basedir="${apidoc.input}" update="no"/>
<echo message="${apidoc.path}"/>
<checksum file="${apidoc.path}" algorithm="SHA-256" property="apidoc.sha"/>
<echo message="Hash wert ist ${apidoc.sha}"/>
</target>
</project>
The target should create a zip file from a doc folder (it does) and then store the hash value of the zip file into a property for further use. However, the hash value is not stored in the property. I get the output as follow:
test:
[echo] Starting target APIDOC
[echo] /Users/user1/git/project/out/test.zip
[echo] Hash wert ist TODO
BUILD SUCCESSFUL
Does anybody have and idea, what is going wrong here?
Properties in Ant are immutable. This line
<property name="apidoc.sha" value="TODO"/>
sets the value, and after that it can't be changed.
If you run ant with the -v command-line option you should see a message like
Override ignored for property "apidoc.sha"
indicating that the attempt to alter the property value in the <checksum> task is being ignored.

Force Ant to substitue a substitution variable

I use a software (Drops) based on ant script.
I try to dynamically generate the destination path of a file that I want to copy. To do this I execute a linux command line.
In my application, I have this properties :
environment.props.environment_name=recette
application.props.target.gmao=/opt/${environment.props.environment_name}/gmao-ws
I expected Ant to replace ${environment.props.environment_name} by its value at runtime. But it doesn't.
Here is the Ant script that I wrote :
<project xmlns:drops="antlib:com.arcadsoftware.mmk.anttasks" name="deployJar" basedir="." default="main">
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<taskdef resource="com/dropssoftware/drops/ant/antlib.xml"/>
<loadDropsContext/>
<target name="main">
<!-- get the value of the property "application.props.target.gmao" -->
<propertycopy name="target.dir" from="application.props.target.gmao"/>
<!-- I expect this to print target.dir=/opt/recette/gmao-ws but it print target.dir=/opt/${environment.props.environment_name}/gmao-ws -->
<echoproperties />
<!-- Supposed to copy from /opt/drops/storage/afile.jar to /opt/recette/gmao-ws but the property "target.dir" is wrong -->
<exec executable="sudo">
<arg value="/bin/cp"/>
<arg value="${param.artifacts.root}/${param.jar.root}"/>
<arg value="${target.dir}"/>
</exec>
</target>
</project>
With this input :
param.env=gmao
param.artifacts.root=/opt/drops/storage/
It is supposed to copy a file from the artifacts directory to the /opt/recette/gmao-ws directory. But Ant tried to copy it to /opt/${environment.props.environment_name}/gmao-ws.
I don't understand why Ant doesn't replace ${environment.props.environment_name} by its value.
Is it possible to force Ant to replace the substitution variable by its value ?
Not entirely clear what you're trying to do. The propertycopy task is not part of normal Ant, coming from a 3rd party extension called ant-contrib
I suspect what you're trying to do can be done with normal property substitution. I have provided an example.
Example
A simple example of how to pass in parameters to a build file by setting properties:
$ ant -Dparam.from=AAA -Dparam.to=BBB
build:
[echo]
[echo] sudo
[echo] /bin/cp
[echo] /opt/drops/storage/AAA
[echo] /opt/drops/storage/BBB
[echo]
build.xml
Note the 3 properties declared at the top? These are effectively the default values available for override.
<project name="demo" default="build">
<property name="param.artifacts.root" value="/opt/drops/storage"/>
<property name="param.from" value="fromDir"/>
<property name="param.to" value="toDir"/>
<target name="build">
<echo>
sudo
/bin/cp
${param.artifacts.root}/${param.from}
${param.artifacts.root}/${param.to}
</echo>
</target>
</project>
I think that I find the answer to my question in ant document :
https://ant.apache.org/manual/properties.html
Normally property values can not be changed, once a property is set, most tasks will not allow its value to be modified.
In the case of the software that I use : Drops, it loaded the application properties BEFORE the environment properties.
So application.props.target.gmao is set BEFORE environment.props.environment_name and the ${environment.props.environment_name} cannot be replace.
The answer to my question is seems to be NO, it's not possible to force Ant to replace the substitution variable by its value.
It's done automatically if the variables are loaded in the good order.

How do I prevent a dependency from executing in Ant?

When debugging a build.xml file, or an Ant task, I often want to execute one task without executing its dependencies. Is there a way to do this from the command line?
For example, with this build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="A" />
<target name="B" depends="A" />
</project>
is there a command that will execute task B but not task A?
You can make execution of any target conditional on a property using if or unless.
<project default="B">
<target name="A" unless="no.a">
<echo>in A</echo>
</target>
<target name="B" depends="A" >
<echo>in B</echo>
</target>
</project>
Output with no condition specified:
$ ant
Buildfile: C:\Users\sudocode\tmp\ant\build.xml
A:
[echo] in A
B:
[echo] in B
BUILD SUCCESSFUL
Total time: 0 seconds
Output with condition specified on command line:
$ ant -Dno.a=any
Buildfile: C:\Users\sudocode\tmp\ant\build.xml
A:
B:
[echo] in B
BUILD SUCCESSFUL
Total time: 0 seconds
Notes:
Ant console output will show that the target was "hit" even if entry was blocked by the condition.
The if and unless conditions do not do boolean check. They just check whether the property is defined or not.
You will have to restructure your Ant script to achieve this:
<target name="B">
<if>
<isset property="some.property"/>
<then>
<antcall target="A">
</then>
</if>
<!-- execute task B here -->
</target>
If some.property is set, then it will first execute A followed by B. Otherwise, it will skip task A and execute B by itself.

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

'make -n' equivalent for ant

According to the man page of make, -n option does the following job:
Print the commands that would be executed, but do not execute them.
I am looking for an option which acts the same in Apache Ant.
Horrific, but here it is. We can hack the targets at runtime using some code inside a <script> tag*. The code in do-dry-run below sets an unless attribute on each of your targets, and then sets that property so that none of them executes. Ant still prints out the names of targets that are not executed because of an unless attribute.
*(JavaScript script tags seem to be supported in Ant 1.8+ using the Oracle, OpenJDK and IBM versions of Java.)
<?xml version="1.0" encoding="UTF-8"?>
<project default="build">
<target name="targetA"/>
<target name="targetB" depends="targetA">
<echo message="DON'T RUN ME"/>
</target>
<target name="targetC" depends="targetB"/>
<target name="build" depends="targetB"/>
<target name="dry-run">
<do-dry-run target="build"/>
</target>
<macrodef name="do-dry-run">
<attribute name="target"/>
<sequential>
<script language="javascript"><![CDATA[
var targs = project.getTargets().elements();
while( targs.hasMoreElements() ) {
var targ = targs.nextElement();
targ.setUnless( "DRY.RUN" );
}
project.setProperty( "DRY.RUN", "1" );
project.executeTarget( "#{target}" );
]]></script>
</sequential>
</macrodef>
</project>
When I run this normally, the echo happens:
$ ant
Buildfile: build.xml
targetA:
targetB:
[echo] DON'T RUN ME
build:
BUILD SUCCESSFUL
Total time: 0 seconds
But when I run dry-run, it doesn't:
$ ant dry-run
Buildfile: build.xml
dry-run:
targetA:
targetB:
build:
BUILD SUCCESSFUL
Total time: 0 seconds
Ant has no dry-run option as make or maven have. But you could run the ant file step by step it in debugging mode under eclipse.
No I belive. There is no such way by default in Ant. And many unstisfying attempts you would find on google. But I have searched once and was unsuccessful.
It would be a useful feature, but not easily implemented.
Make and ANT are architecturally quite different. ANT doesn't run external OS commands, instead, most ANT "tasks" execute within the same Java thread.
It would be possible to emulate a "dry run" as follows:
<project name="Dry run" default="step3">
<target name="step1" unless="dry.run">
<echo>1) hello world</echo>
</target>
<target name="step2" depends="step1" unless="dry.run">
<echo>2) hello world</echo>
</target>
<target name="step3" depends="step2" unless="dry.run">
<echo>3) hello world</echo>
</target>
</project>
Running ANT as follows will print the target name but won't execute the enclosed tasks:
$ ant -Ddry.run=1
Buildfile: build.xml
step1:
step2:
step3:
BUILD SUCCESSFUL
Total time: 0 seconds
Create a special target in your buildscript that does some echoing only i.e. to check whether properties, path .. are resolved correctly.
see https://stackoverflow.com/a/6724412/130683 for a similar question answered.
For checking the details of your ant installation use ant -diagnostics

Resources