How to write a variable in to file in ant? - ant

i have a variable abc and had the value this is ant script. This abc variable will keep on changing.
Using ANT script, how can i write the value out to the file?

The echo task in ANT is able to write to files
<echo file="output.txt" append="true">
abc=${abc}
</echo>

Use propertyfile task. An example taken from ant manual:
<propertyfile file="my.properties">
<entry key="abc" value="${abc}"/>
</propertyfile>
This may be better than echo as it updates the properties file with a given value, while echo appends to or overwrites the whole file.

Related

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.

Replacing property dynamically based on environments in Ant scripts

I want to develop ant script which replaces application properties with environment specific properties. My requirement is that I will have all environment properties in single env.properties file. During building the application I need to replace with whatever in env.properties file. Ant replace works well when I have property files for each environment.
Sample : env.properties
dev.AddNETWORK_USER=devUser
dev.ADDPASS=devPass
sit.AddNETWORK_USER=situser
sit.ADDPASS=sitPass
This needs be replaced in mule.properties as
for DEV environment:
dev.AddNETWORK_USER=devUser
dev.ADDPASS=devPass
for SIT environment:
AddNETWORK_USER=sitUser
ADDPASS=sitPass
You can use property ant task on your env.properties file. This allows to access key=value pair based on your need. You can redirect your environment specific properties to file you want or write to one specific file.
<property file="${base.dir}/env.properties"/>
<for list="dev,sit" param="value">
<sequential>
<echo message="#{value}.AddNETWORK_USER=${#{value}.AddNETWORK_USER}" append="true" file="${base.dir}/#{value}Any-File.prop"/>
<echo message="${line.separator}" append="true" file="${para.home}/#{value}Any-File.prop"/>
<echo message="#{value}.ADDPASS=${#{value}.ADDPASS}" append="true" file="${para.home}/#{value}Any-File.prop"/>
</sequential>
</for>

Tags between <echo> and </echo> in ant

I have an Ant script like this:
<target name="create_report_file">
<echo file="${testResultsDir}/test_report.xml">
<testsuite name="${platformTask}">
</testsuite>
</echo>
</target>
What do the tags between <echo> and </echo> mean? Will Ant run them or output? Or both?
file is an echo parameter. It's the file to write the message to.
testsuite is possibly a JUnit task.
The Ant target runs the test suite and outputs the results to the test_report.xml file.
Does your Ant script work? What about if it attempts to execute the create_report_file target?
Usually, echo tasks simply echo the contents to either the console or to a file if the file parameter is specified.
However, as written, it makes <testsuite> is a sub-entity of the <echo/> task, and it's not. In fact, there's no documented sub-entities in the <echo/> task. In fact, <echo> doesn't even take <condition/> sub-entity tasks like <fail/> would.
This is why I'm asking whether or not your build file is even working.
It appears they might want to log the testsuite being executed. There are two ways to do this to make this work:
Change all < and > to character entities:
<target name="create_report_file">
<echo file="${testResultsDir}/test_report.xml">
<testsuite name="${platformTask}">
</testsuite>
</echo>
</target>
Use <echoxml> instead of <echo>:
<target name="create_report_file">
<echoxml file="${testResultsDir}/test_report.xml">
<testsuite name="${platformTask}">
</testsuite>
</echoxml>
</target>
Another possibility
It is possible that you're using some Ant plugin that has a <testsuite> task. I don't know what this would be. The <testsuite> task isn't part of JUnit or TestNG. However, if there is an Ant plugin being used that defines a <testsuite> task, it might redefine the <echo> task which it's at it. Does your build script have a <taskdef> in it? If so, what's the class reference?
It could be that the user defines their own <testsuite> macro in your build script. However, that wouldn't redefine the <echo> task and it still wouldn't work.

Ant modifying property file but escaping characters

I have an ant build script that is modifying a properties file. When it amends the properties with the new paths, it seems to escape the back slashes and colons. I understand this is probably working as intended but batch files use this property file further down the process and it is causing errors.
Is there an ant solution to this, or should I start looking at a shell script workaround?
Thanks,
BON
Ant target:
<target name="modify_workstation_properties" depends="loadWinEnvVars, loadUnixEnvVars">
<propertyfile file="${basedir}/Deliverables/config/framework_setup/workstation.properties">
<entry key="toplevel.project.dir" value="${basedir}"/>
<entry key="root.project.dir" value="${basedir}/Construction"/>
<entry key="root.dir" value="${basedir}/Framework/Construction/netc_os"/>
<entry key="jdk.home" value="${JDKHome}"/>
<entry key="wls.home" value="${WLSHome}"/>
<entry key="domain.dir" value="${DomainDir}"/>
<entry key="stage.dir" value="${DomainDir}"/>
</propertyfile>
</target>
Output:
# Top Level Root directory of the new working project
toplevel.project.dir=C\:\\forImage\\r16_dev_deploy
# Root directory of the new working project
root.project.dir=C\:\\forImage\\r16_dev_deploy/Construction
# Root directory of the framework project
root.dir=C\:\\forImage\\r16_dev_deploy/Framework/Construction/netc_os
...
No you can't modify the way that the properties are written. However after the file is written you could use the ReplaceRegExp task and correct the escaped characters.

Can Ant expand environment variables from a properties file?

I have a question regarding Ant and its treatment of environment variables.
To illustrate I have a small sample.
Given the Ant build file test.xml:
<project name="myproj" default="testProps">
<property environment="env"/>
<target name="testProps">
<echo message="${env.MyEnvVar}"/>
<echo message="${MY_PROPERTY}"/>
</target>
</project>
And the properties file test.props:
MY_PROPERTY=${env.MyEnvVar}
Now set the environment variable MyEnvVar to some value (foo in my case) and run Ant using this command line:
ant -f test.xml -propertyfile test.props testProps
The output I get is:
[echo] foo
[echo] ${env.MyEnvVar}
What I would like to know is whether there is any way to structure the input properties file such that I get
[echo] foo
[echo] foo
That is, I would like to name an environment variable in the properties file which is replaced within the Ant script. Note - I know how to access environment variables directly (as is done here). What I need to do is make use of a set of Ant scripts that expect one collection of properties in an environment that defines the same properties using different names. Thus the thought of "bridging" them in a properties file.
I am using Ant version 1.6.5.
You need to read the test.props property file after the environment - you could do so using another property task, i.e. add
<property file="test.props" />
after your existing property environment task.
In full:
<property environment="env" />
<property file="test.props" />
<target name="testProps">
<echo message="${env.MyEnvVar}"/>
<echo message="${MY_PROPERTY}"/>
</target>
When you supply the properties file on the command line this gets processed before the content of the build, but at that time ${env.MyEnvVar} is not yet set.

Resources