I have a properties file in which the keys represent strings in source code files that I would like to search for, and the values represent the replacement string that I would like to replace the strings with (see below for an example).
I would like to perform these replacements over a set of files during my Ant build, however I cant seem to figure out how to perform this operation. A simple replace is easy using the Ant replacement task, but I can't determine if there is a way using Ant tasks to perform this bulk search and replace using a properties file to indicate what to search and replace. I think I may need to write a script to perform this.
Anyone have any ideas if this is possible using Ant tasks?
Example.props
gameStatusCode=statusCode
gameHomeName=homeName
gameAwayName=awayName
Original Source
if(dataitem.gameStatusCode === 'pre'){
var tmp = dataitem.gameHomeName;
...
}
Replacement Source
if(dataitem.statusCode === 'pre'){
var tmp = dataitem.homeName;
...
}
Use the replace task with replacefilterfile attribute:
<replace dir="${src}" replacefilterfile="example.props">
<include name="**/*.java"/>
</replace>
Related
I have an Ant buildfile (build.xml) which is called by some application. I would like to know exactly what kind of properties are used to invoke Ant. Therefore I would like to modify the build.xml file to display all properties specified in the call, e.g.:
ant aTarget -Dxslt.parser=SAXON -Dbasedir=aFolder
would display list as below
- target: aTarget
- xslt.parser = SAXON
- basedir=aFolder
Please note that I do not know exactly what is being using to invoke Ant. Therefore, I need to use some sort of a loop get all properties, options.
The simplest thing that comes to mind is to place a line like:
<echo message="Ant invocation is '${sun.java.command}'" />
In the buildfile outside of any target. It'll look something like:
% ant aTarget -Dx=y
[echo] ant invocation is: 'org.apache.tools.ant.launch.Launcher -cp . aTarget -Dx=y'
It shows you what was passed to the Ant Launcher, which might will likely be a little more than what was passed to the ant wrapper script, but should do.
I would avoid trying to parse the line, as you say, you don't know what might be there, and it could quickly get complicated.
Take a look at the <echoproperties> task:
<property name="in.file.prop" value="value2"/>
<echoproperties/>
in.file.prop and its value will be printed. However, over 60 other properties will be printed as well including properties built into Ant.
You can save the results of <echoproperties> to a file and then filter that file with something like a <linecontains> filter.
there are different folders or strings like 4.5, 4.10.1, 4.10.2, 4.10.5, 4.11.1, 4.12 , 4.13.2...etc
My input will be 4.10.1 to 4.11.1 and it should fetch only the respective strings.
I should be able to get an output like 4.10.1, 4.10.2, 4.10.5, 4.11.1.
Now, am able to split the strings, but not possible to increment to get the next strings.
You didn't provide enough details (what means respective ..), but basicly you'll need some kind of looping.
Either use ant script task with a scripting language, like javascript (already contained in JDK >= 6) or groovy or use some Ant addon like f.e. Flaka or Antcontrib.
Some basic snippet with Flaka :
Use for task combined with split function to iterate over your list
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="whatever" value="4.5,4.10.1,4.10.2,4.10.5,4.11.1,4.12,4.13.2"/>
<fl:for var="substring" in="split('${whatever}', ',')">
<!-- do something with substring ...-->
<fl:echo>#{substring}</fl:echo>
</fl:for>
</project>
For further snippets see FlakaExamples and Flaka Manual.
I am reading a file in ant and loading the properties through loadproperties. I am interested in using the value of a specific property, whose name is not known. I know that it follows a pattern because that is how I load the property.
I can echoproperties and see that it is being loaded.
But I dont know how to access its value, given that its name is actually a pattern rather that hardcoded.
How can I access this property's value to do some processing.
I hope this is clear. Please ask if I need to clarify some more.
Take a look at ant-contrib package. Its propertycopy task will do what you need. If you need to resolve an arbitrary number of properties following an established pattern, you would use ant-contrib's propertycopy in conjunction with ant-contribs "for" task.
http://ant-contrib.sourceforge.net/tasks/tasks/index.html
You should use Ant's script task.
I suggest using the beanshell script since it is pure java.
For example, to print all properties for your project, use the following:
<target name="echoprops">
<script language="beanshell">
System.out.println("All Properties: " + project.getProperties().keySet());
</script>
</target>
It should be easy to modify the above script to get the property you want.
To use this task, you will need to run the following in $ANT_HOME first:
ant -f fetch.xml script -Ddest=user
That will download all required optional jars to ~/.ant/lib .
In our Ant build environment, I have to do the same task for a number of items. The AntContrib foreach task is useful for that. However, the list is in a parameter, where I actually have the list in a file. How can I iterate over items in a file in an foreach-like way in Ant? Something like (pseudo-code):
<foreach target="compile-module" listFromFile="$fileWithModules"/>
I'm happy to write a custom Task, and welcome any suggestion on possible solutions.
I tried to load the file into a property and iterate over it, worked fine for me:
<loadfile property="file-content" srcFile="${fileWithModules}"/>
<foreach
target="compile-module"
list="${file-content}"
delimiter="${line.separator}"
param="your-param-used-in-target"/>
I use the Ant cvs and sql tasks to check out and deploy a full code set of database objects. I also use the Ant cvschangelog task to generate a list of what has changed between two tags. It seems that there should be some way of defining a target process that would iterate over the list of elements from the generated changelog xml file and only execute files that have changed between two tags (all of the files use "CREATE or REPLACE" syntax and only replaceable objects are of interest here).
Is there any native Ant solution for this or will a custom task to parse the xml file be necessary?
You might investigate the ant-contrib tasks for looping constructs. The for or foreach task may be just what you need.
Here is an example :
http://franckbehaghel.free.fr/antTask/
Last time I checked Ant didn't support any sort of looping constructs, so you'll have to write your own custom task> However, there is an xslt task that lets you apply stylesheets to xml files. It will depends on what you want to do with the xml itself.