How to refer to a changing path in Ant? - ant

I have a dynamic path in my server inside which resides a file to be updated.
I am using Ant to make the file changes. But it does not seem to identify the path.
I have defined it in the following way:
<replaceregexp file="/tmp/automation/server/main*/main*/webserver/apache-tomcat.*/webapps/ROOT/WEB-INF/web.xml" match="https://api.test.com" replace="${test-Endpoint}" byline="true" />
Here 'main' is suffixed with the time-stamp and hence keep changing on every installation.

Related

Issue with the space in the path specified for build.xml

I have a build file, which has the following property in it.
<property name="schema.dir" value="src/main/resources/schema" />
This schema.dir is used to refer a wsdl file.The parent folder which contains the build.xml has a space in it like this folder name .
When I echoed the property it displayed only src/main/resources/schema.
But I can see from the ant logs that issue is with the space in the folder name.
Since the parent folder is having a space in it, I am not able to refer the wsdl.
Can somebody suggest a solution so that file can be accessed with out changing the folder name
Is it possible to provide directly full path to wsdl file?
Try to replace " " sign with "%20".
For test you can hardcode that and with ANT you can use the propertyregex task from Ant Contrib.
See Replacing characters in Ant property
Have you tried specifying your property as a location rather than a string so Ant knows it's dealing with a file path, you can also specify the path as relative to your base directory.
<property name="schema.dir" location="${basedir}/src/main/resources/schema"/>
Adding ${basedir} to the path may not be necessary after changing the property from a value to a location.

calling a target from a particular directory

I have script that call the below target but before calling this target i want that i should be at the following location which is stored in the below variable ..
todir="${release.deployment.tool}
the target that is called is shownb below..
<target name="deploy-ion" description="Used to deploy to a given aaa.">
so finally when my target deploy-ion is being called i should make sure that I should be in
directory stored in todir , please advise how to achieve this.
I meant you can use absolute path of your property reference while performing set of tasks inside your target, as by default it uses basedir default value as cur-dir or one which is specified at project level at beginning of your build.xml file.
Also alternatively you can use ant's ant task : https://ant.apache.org/manual/Tasks/ant.html with usage like
<ant dir="${release.deployment.tool}" target="deploy-ion" />

Why does ant mappedresources seem to flatten paths to file names no matter what mapper is used

I'm trying to do something fairly simple: take a path or dirset and extend each of its elements to get a new path.
For example, say I have a path that contains
c:\foo\plugins\com.foo.bar
c:\foo\plugins\com.baz.quux
I want to get a new path with:
c:\foo\plugins\com.foo.bar\src
c:\foo\plugins\com.baz.quux\src
I'm actually using a path based on a dirset for the first path, then using a <mappedresources> to get the second path, but the second path always ends up truncated like this:
com.foo.bar\src
com.baz.quux\src
To simplify for debugging, I removed the path and just stuck the dirset in a mapped resources, then I use a pathconvert macrodef for debug output.
I also replaced the globmapper I was using (for mapping * to */src) with an <identitymapper> just to see if it was the globmapper. It wasn't.
In a nutshell I have this:
<mappedresources id="src.resources">
<dirset dir="c:\foo">
<include name="com.*"/>
</dirset>
<identitymapper/>
</mappedresources>
Now when I dump this with path convert I get
com.foo.bar
com.baz.quux
(Again, this is debugging, in my original I use a globmapper and I get that same list but with "/src" appended - but I'm replacing the globmapper with an identitymapper)
Now, if replace the mappedresources with regular <resources>, I get the full path
<resources id="src.resources">
<dirset dir="c:\foo">
<include name="com.*"/>
</dirset>
</resources>
Then I get the full paths when dumping:
c:\foo\plugins\com.foo.bar
c:\foo\plugins\com.baz.quux
Why?
The ant mapper documentation clearly shows examples of full paths being mapped to full paths with both the globmapper and the identity mapper, and only shows that the <flatten> mapper does something like this. So why is it happening here?
And failing this, is there some better way to get a path that contains all the elements of another path but extended with an extra directory like this?
One last note: I'm using these paths in a custom task, not in a <copy> or other task that takes mappers directly - I'm assuming this wouldn't occur directly in the task, but I haven't tested yet.

Changing properties file inside a jar with Ant

I have a settings in a properties file located within a jar that I wish to alter at build-time using ant. Ideally if I am able to search for a specific text in the properties file and replace it easily, I would like to do that but isn't sure how.
So I was thinking I can overwrite it with another properties file that has the new settings already predefined. The jar already exists in my directory and the hierarchy of my jar is as follows:
food.jar
/com/food/donut.properties
some file...
some file...
If I had another donut.properties file with a different setting located in a different directory. How can I overwrite it with ant?
Thanks for the help, much appreciated!
EDIT:
With the following code I was able to copy the properties file into the jar. But whenever I attempt to copy the new properties file into the same directory of the old properties file, it does not get replaced. (i.e. If i change the prefix to 'com' i can see the new properties file being inserted into the jar. If the prefix is changed to com/food, nothing is replaced. What am i doing incorrectly?
<jar destfile="${dist.dir}/food.jar" update="true">
<zipfileset file="donut.xml" prefix="com/food/" />
</jar>
needs Ant 1.8.x
Step 1)
edit your propertyfile, multiple nested entry elements possible :
<propertyfile file="/path/to/propertyfile/foo.properties">
<!-- will change an existing key named 'somekey' with the value 'foo' inplace -->
<entry key="somekey" value="foo"/>
</propertyfile>
see Ant Manual propertyfile
Step 2)
update your jar with the altered propertyfile :
<jar destfile="/path/to/your/foo.jar" update="true">
<fileset dir="/path/to/propertyfile" includes="*.properties"/>
</jar>
for renaming use nested mapper like that :
<jar destfile="/path/to/your/foo.jar" update="true">
<mappedresources>
<fileset dir="." includes="*.properties"/>
<globmapper from="*.properties" to="/com/xml/*.properties"/>
</mappedresources>
</jar
The ant documentation of the jar task says:
The update parameter controls what happens if the JAR file already
exists. When set to yes, the JAR file is updated with the files
specified. When set to no (the default) the JAR file is overwritten.
An example use of this is provided in the Zip task documentation.
Please note that ZIP files store file modification times with a
granularity of two seconds. If a file is less than two seconds newer
than the entry in the archive, Ant will not consider it newer.
You might need to make sure the properties file is newer than the one in the jar file. Using the touch task could solve the problem.
Or you might just unzip the jar in the temp directory, copy the properties file with the copy task and its overwrite attribute set to true, and re-jar the contents of the temp directory.

ANT: How to call target for all build.xml in subdirectories?

How do you call a specific target in all build.xml located in all subdirectories using wildcards (ie not hard coding the subdirectory names)? The below answer is hardcoded. Is there a way to do it without hardcode?
Similar to this question: Pass ant target to multiple build.xml files in subdirectories
Use the Ant subant task like this:
<subant target="sometarget">
<fileset dir="." includes="*/build.xml" />
</subant>
If you include an "inheritall" attribute (same as how it's used in but defaults the opposite), you can share all your current project's properties and everything too. This also makes it very easy to overwrite tasks defined in your main build.xml file if you need to.
Read more about it here.
I'll setup different properties within my build.properties file. I use these to dynamically build paths in my targets.
Define the location of your build.properties file:
<!-- all properties are in build.properties -->
<property file="build.properties" />
Use those properties in your targets:
Properties in the build properties are similar to setting up an .ini file:
project.rootdir=c:/Deploy
project.tempbuilddir = c:/Deploy/Temp/Inetpub
project.builddir=c:/Deploy/Inetpub
# Build prefix will be added to that tags urls (.../tags/${project.buildprefix}Build_${today.date})
project.buildprefix=ACA_
I guess you could use a dynamic file as your properties file, if necessary, as long as you define the proper path to the file. You could point it to a server-side file to dynamically write your properties file (ColdFusion, PHP, JSP, whatever).
I've used ant-contrib's foreach task to do something like this.
http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html
Sounds like a perfect candidate for the <subant> task.

Resources