Ant Substring using regex - ant

I have a string like below
C:\deployment\dep_Scripts\OSB\configuration\sbconfig1.jar
From this string I want to extract only the file name sbconfig1
How can I do that using regex

Ant's basename task does this : https://ant.apache.org/manual/Tasks/basename.html
<basename property="jar.name.without.extention" file="C:\deployment\dep_Scripts\OSB\configuration\sbconfig1.jar" suffix="jar"/>
<echo message="${jar.name.without.extention}"/>

Related

Ant Script for converting multiple file formats using mappers- creating double extension files

I want to convert multiple file formats to a single file format.
Example: D:\myrepo\rough has 3 files
1. abc.sql
2. def.xml
3. ghi.dmp
I want them all to be converted to .txt using glob mappers.
<?xml version ="1.0"?>
<project name = "roughone" default="taget1">
<target name= "target1">
<move todir="D:\myrepo\rough">
<fileset dir="D:\myrepo\rough">
</fileset>
<mapper type ="glob" from="*" to="*.txt"/>
</move>
</target>
</project>
This is giving
1. abc.sql.txt
2. def.xml.txt
3. ghi.dmp.txt where as i need only abc.txt,def.txt and ghi.txt.
Plz let me know how this can be fixed(from= "." is not helping too).
Replace your globmapper with the following <regexpmapper>:
<regexpmapper from="^(.*)[.][^.]+$$" to="\1.txt"/>
The above regular expression captures the part of each file name before the final period. The regular expression also discards whatever extension the file previously had.
The double "$$" is needed because Ant would interpret a single "$" as the beginning of a property reference.

Override one value in properties file

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.

ant copy from absolute path read from xml

My problem is, I have to read the source path for a copy job from an xml file and then copy all files in that dir read from the xml file to another dir.
Since code is more than words:
<xmltask source="${projectfile}">
<copy path="Project/RecentResultsInfo/ResultsDirectoryOfRecentLoadTest/text()" property="recentdir" attrValue="true"/>
</xmltask>
<copy todir="${targetdirectory}">
<fileset dir="${recentdir}"/>
</copy>
The output when running this target is:
C:\develop\build.xml:44: Warning: Could not find resource file "C:\develop\C:\Programme\tool\test_90\" to copy.
It seems in fileset it does not recognize, that recentdir holds a full path inside. The written xml from the application has a newline before and after the path in the xml file that is read with the path. So ant does not recognize the path since theres a newline in front of it.
Is there anything like trim for ant?
Can anybody help me getting ant to accept that path?
Done it now by using Ant-Contrib, but that is used in this project anyway.
<xmltask source="${projectfile}">
<copy path="Project/RecentResultsInfo/ResultsDirectoryOfRecentLoadTest/text()" property="recentdirraw" attrValue="true"/>
</xmltask>
<!-- replace newlines and whitespace from read path -->
<propertyregex property="recentdir" input="${recentdirraw}" regexp="^[ \t\n]+|[ \t\n]+$" replace="" casesensitive="false" />
<copy todir="${targetdirectory}">
<fileset dir="${recentdir}"/>
</copy>
Simply modifying the property with a regex trimming the text by striping of whitespace and newlines.
As far as I can see, the copy element in xmltask provides a trim attribute.
trims leading/trailing spaces when writing to properties
Does that work?

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>

Text manipulation in ant

Given an ant fileset, I need to perform some sed-like manipulations on it, condense it to a multi-line string (with effectively one line per file), and output the result to a text file.
What ant task am I looking for?
The Ant script task allows you to implement a task in a scripting language. If you have JDK 1.6 installed, Ant can execute JavaScript without needing any additional dependent libraries. The JavaScript code can read a fileset, transform the file names, and write them to a file.
<fileset id="jars" dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<target name="init">
<script language="javascript"><![CDATA[
var out = new java.io.PrintWriter(new java.io.FileWriter('jars.txt'));
var iJar = project.getReference('jars').iterator();
while (iJar.hasNext()) {
var jar = new String(iJar.next());
out.println(jar);
}
out.close();
]]></script>
</target>
Try the ReplaceRegExp optional task.
ReplaceRegExp is a directory based task for replacing the occurrence of a given regular expression with a substitution pattern in a selected file or set of files.
There are a few examples near the bottom of the page to get you started.
Looks like you need a conbination of tasks:
This strips the '\r' and '\n' characters of a file and load it to a propertie:
<loadfile srcfile="${src.file}" property="${src.file.contents}">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.StripLineBreaks"/>
</filterchain>
</loadfile>
After loading the files concatenate them to another one:
<concat destfile="final.txt">
...
</concat>
Inside concat use a propertyset to reference the files content:
<propertyset id="properties-starting-with-bar">
<propertyref prefix="src.file"/>
</propertyset>
rodrigoap's answer is enough to build a pure ant solution, but it's not clean enough for me and would be some very complicated ant code, so I used a different method: I subclassed ant's echo task to make an echofileset task, which takes a fileset and a mapper. Subclassing echo buys me the ability to output to a file. A regexmapper performs the transformation on filenames that I need. I hardcoded it to print out each file on a separate line, but if I needed more flexibility I could add an optional separator attribute. I also thought about providing the ability to output to a property, too, but it turned out I didn't need it since I echo'ed straight to a file.

Resources