Override one value in properties file - ant

I have a properties file:
custom.properties
the content of this properties file is:
id=sf2j2345kkklljhlaasfsdfafsf543
name=SOME_NAME
The value of id is a long random string.
I want to make an Ant script to replace/over-write the value of id to another one, I tried with Ant <replace> syntax:
<target name="change-id">
<replace file="custom.properties" token="id" value="aaa" />
</target>
I run ant change-id , the content of the properties file becomes:
aaa=sf2j2345kkklljhlaasfsdfafsf543
name=SOME_NAME
That's the key "id" get replaced instead of its value. But I need to replace the value to "aaa" , how to achieve this in Ant?
Please do not recommend me to set token to id's random value, because that value is random generated and put there. I only want to over-write the random value of "id" by Ant script, how to achieve this?.

You can do it using replaceregexp task. Try to do it like in this example
conf.ini (utf-8)
aaa=sf2j2345kkklljhlaasfsdfafsf543
name=SOME_NAME
build.xml
<project name="regexp.replace.test" default="test">
<target name="test">
<replaceregexp file="conf.ini" match="^aaa=.*" replace="aaa=newId" encoding="UTF-8" />
</target>
</project>
I don't know exactly if this regular expression is correct but this is the way you can do it.

Related

how to pass parameters to ant task's depends field

I have a build.xml that should receive dynamically parameters to the depends field.
I define this parameter in some other app.xml such as:
ops=op1, op2, op3,op4,op5,.... opn
then I import this app.xml into build.xml and want to use the parameter ops there.
<project name="Project" basedir="." default="help">
<target name="test" depends="{$ops}" description="executea series of commands in ant">
<echo message="batch operation job done. tasks = {$ops}"/>
</target>
</project>
How can I pass a parameter from one ant file to another?
The depends parameter does not take properties.
Ant uses a dependency matrix to determine what should be built and in what order. This matrix is calculated before any part of the build file itself is executed, so properties aren't even set when this is done.
What are you trying to accomplish? Maybe if we have a better idea what you want, we can help you with it. Ant isn't a scripting language like BASH or Python.
As already mentioned, you can't put properties into the Depends field. However, if you are setting a property, you can use it in the If field. Example
<project name="appProject">
<target name="test" depends="target1,target2,target3" description="execute series of commands"/>
<target name="target1" if="do.target1">
<echo message="Target1 executed." />
</target>
<target name="target2" if="do.target2">
<echo message="Target2 executed." />
</target>
<target name="target3" if="do.target3">
<echo message="Target3 executed." />
</target>
</project>
Then you set in your build.xml the given target flag do.target1, do.target2 or do.target3 and it gets executed. Basically what you wanted to have. In the If field properties are only checked for value. Also, you don't have to use the ${ } construction for the properties.

Ant get the value of a parameter obtained at runtime

I have an Ant build task where in based I need to lookup the property file based on a value which I get at runtime. For example, I have the following information in property file
COMPLETE_LIST=TEST1,TEST2,TEST3
TEST1=val1
TEST2=val2
TEST3=val3
In my Ant target, I have the following task.
<target name="target_main">
<foreach param="profile_name" list="${COMPLETE_LIST}" target="target_child">
</foreach>
</target>
<target name="target_child">
<echo>Printing the value of the param passed ${${profile_name}}</echo>
</target>
But this is not working. Is there any way to get the value of TEST1 which is passed as a parameter?
Since you already are using ant-contrib, the propertycopy task will help you do what you want. Here's the body of target_child modified to suit your purpose:
<target name="target_child">
<propertycopy name="value" from="${profile_name}"/>
<echo>Printing the value of the param passed ${${profile_name}}</echo>
</target>
The output:
target_main:
target_child:
[echo] Printing the value of the param passed val1
target_child:
[echo] Printing the value of the param passed val2
target_child:
[echo] Printing the value of the param passed val3

The prefix "sonar" for element "sonar:sonar" is not bound

I have a build.xml-file that looks something like this:
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpath="/path/sonar-ant-task.jar"/>
<target name="sonar">
<sonar:sonar/>
</target>
And when I run the file I get:
The prefix "sonar" for element "sonar:sonar" is not bound.
Any obvious things I'm missing?
You're missing the namespace declaration in the top project element of your Ant script.
xmlns:sonar="antlib:org.sonar.ant" ought to do it.
In ant you can not use .
try below and if you are setting any properties use key value pare in xml tag.
To allocate value use attributes of xml tags.
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
</sonar:sonar>

How to acess property within a property in ant

Hi all please give a look to this code
in my properties file i have
win-x86.pc-shared-location=E:\Ant_Scripts
Now below i am trying to call PrintInstallerName_build from my build.xml,while as PrintInstallerName_build is in test.xml. In build.xml file,${platform.id} has value=win-x86 in the calling target and in called target param1 also has value=win-x86
<target name="PrintInstallerName" >
<echo>PlatForm.Id====>${platform.id}</echo>
<ant antfile="test.xml" target="PrintInstallerName_build">
<property name="param1" value="${platform.id}"/>
</ant>
<target name="PrintInstallerName_build" >
<echo>${param1.pc-shared-location}</echo><!--${param1.pc-shared-location}-->
<echo>${param1}.pc-shared-location}</echo><!--win-x86.pc-shared-location-->
<echo>${win-x86.pc-shared-location}</echo><!--E:\\Ant_Scripts-->
</target>
as you can see only the last statement gives correct output but it is hardcoded,i want to use param1 and the output should be E:\\Ant_Scripts i tried to use $ and # but none works,may be i am doing wrong somewhere can someone help please,i am struck and tomorrow is its DOD.
See Nesting of Braces in the Properties page of the Ant Manual.
In its default configuration Ant will not try to balance braces in
property expansions, it will only consume the text up to the first
closing brace when creating a property name. I.e. when expanding
something like ${a${b}} it will be translated into two parts:
the expansion of property a${b - likely nothing useful.
the literal text } resulting from the second closing brace
This means you can't use easily expand properties whose names are
given by properties, but there are some workarounds for older versions
of Ant. With Ant 1.8.0 and the the props Antlib you can configure Ant
to use the NestedPropertyExpander defined there if you need such a
feature.
You can use <propertycopy> to make it happen.
Consider that you need to have the property value of ${propA${propB}}
Use ant tag of propertycopy as follows:
<propertycopy property="myproperty" from="PropA.${PropB}"/>
<echo >${myproperty}</echo>
This will echo the value of ${propA${propB}}
<target name="PrintInstallerName_process" >
<echo>${param1}</echo><!--win-x86-->
<macrodef name="testing">
<attribute name="v" default="NOT SET"/>
<element name="some-tasks" optional="yes"/>
<sequential>
<echo>Source Dir of ${param1}: ${#{v}}</echo><!-- Dir of Win-x86:E:\Ant_Scripts-->
<some-tasks/>
</sequential>
</macrodef>
<testing v="${param1}.pc-shared-location">
<some-tasks>
</some-tasks>
</testing>
</target>
this is the way it works and for me it works fine anyways #sudocode your tip took me there so thank you very much

editing the xml using ant task

I was trying to edit the my config.xml file using ant task but I could not do that can anybody tell me How can I edit the xml using ant task automatically so that i dont need to change it manually for every new branch?
The first option to check would be the Ant xslt task. For an introduction to its use see the Ant/XSLT Wikibook.
I've used groovy to do this. groovy is very Java like, so you can create your groovy classes very similarly to a static java method, and have ant call out to your groovy script using the <groovy> task (you will of course need to include the groovy task def).
Because groovy can use Java syntax you can include the org.w3c.com.* libraries to have access to DOM classes.
For example, snippet of code showing the adding a resource ref element to a specifed web.xml file :-
import org.w3c.dom.*;
String web_xml_filename=args[0];
String res_ref_name=args[1];
Document doc = DomHelper.getDoc(web_xml_filename);
Element rootNode=doc.getDocumentElement();
newNode = doc.createElement("resource-ref");
DomHelper.createElement(doc, newNode, "res-ref-name", res_ref_name);
DomHelper.createElement(doc, newNode, "res-type", "javax.sql.DataSource");
DomHelper.createElement(doc, newNode, "description", description);
DomHelper.createElement(doc, newNode, "res-auth", "Container");
rootNode.insertBefore(newNode, nodes.item(0));
DomHelper.writeDoc(doc, web_xml_filename, false);
To call from ant, use the groovy task :-
<groovy src="${e5ahr-groovy.dir}/addResoureRefToJBossWebXML.groovy" classpath="${groovy.dir}">
<arg value="${jboss-web.xml}"/>
<arg value="jdbc/somesource/>
<arg value="java:jdbc/somesource"/>
</groovy>
You can use ReplaceRegExp. Pattern and Expression options won't let you use less than or more than, but it can be replaced with HTML entities. For example:
<replaceregexp byline="true">
<!-- In config.xml this looks like <myVariable></myVariable> -->
<regexp pattern="<myVariable>(.*)</myVariable>" />
<substitution expression="<myVariable>${myVariable.value}</myVariable>" />
<fileset dir="${user.dir}">
<include name="config.xml" />
</fileset>
</replaceregexp>

Resources