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>
Related
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.
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.
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
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>
I'm trying to debug a macrodef in Ant. I cannot seem to find a way to display the contents of a parameter sent as an element.
<project name='debug.macrodef'>
<macrodef name='def.to.debug'>
<attribute name='attr' />
<element name='elem' />
<sequential>
<echo>Sure, the attribute is easy to debug: #{attr}</echo>
<echo>The element works only in restricted cases: <elem /> </echo>
<!-- This works only if <elem /> doesn't contain anything but a
textnode, if there were any elements in there echo would
complain it doesn't understand them. -->
</sequential>
</macrodef>
<target name='works'>
<def.to.debug attr='contents of attribute'>
<elem>contents of elem</elem>
</def.to.debug>
</target>
<target name='does.not.work'>
<def.to.debug attr='contents of attribute'>
<elem><sub.elem>contents of sub.elem</sub.elem></elem>
</def.to.debug>
</target>
</project>
Example run:
$ ant works
...
works:
[echo] Sure, the attribute is easy to debug: contents of attribute
[echo] The element works only in restricted cases: contents of elem
...
$ ant does.not.work
...
does.not.work:
[echo] Sure, the attribute is easy to debug: contents of attribute
BUILD FAILED
.../build.xml:21: The following error occurred while executing this line:
.../build.xml:7: echo doesn't support the nested "sub.elem" element.
...
So I guess I need either a way to get the contents of the <elem /> into a property somehow (some extended macrodef implementation might have that), or I need a sort of <element-echo><elem /></element-echo> that could print out whatever XML tree you put inside. Does anyone know of an implementation of either of these? Any third, unanticipated way of getting the data out is of course also welcome.
How about the echoxml task?
In your example build file replacing the line
<echo>The element works only in restricted cases: <elem /> </echo>
with
<echoxml><elem /></echoxml>
results in
$ ant does.not.work
...
does.not.work:
[echo] Sure, the attribute is easy to debug: contents of attribute
<?xml version="1.0" encoding="UTF-8"?>
<sub.elem>contents of sub.elem</sub.elem>
Perhaps the XML declaration is not wanted though. You might use the echoxml file attribute to put the output to a temporary file, then read that file and remove the declaration, or reformat the information as you see fit.
edit
On reflection, you can probably get close to what you describe, for example this sequential body of your macrodef
<sequential>
<echo>Sure, the attribute is easy to debug: #{attr}</echo>
<echoxml file="macro_elem.xml"><elem /></echoxml>
<loadfile property="elem" srcFile="macro_elem.xml">
<filterchain>
<LineContainsRegexp negate="yes">
<regexp pattern=".xml version=.1.0. encoding=.UTF-8..." />
</LineContainsRegexp>
</filterchain>
</loadfile>
<echo message="${elem}" />
</sequential>
gives
$ ant does.not.work
...
does.not.work:
[echo] Sure, the attribute is easy to debug: contents of attribute
[echo] <sub.elem>contents of sub.elem</sub.elem>