开发者

editing the xml using ant task

开发者 https://www.devze.com 2023-01-26 09:42 出处:网络
I was trying to edit the my config.xml file using ant task b开发者_C百科ut 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

I was trying to edit the my config.xml file using ant task b开发者_C百科ut 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="&lt;myVariable&gt;(.*)&lt;/myVariable&gt;" />
    <substitution expression="&lt;myVariable&gt;${myVariable.value}&lt;/myVariable&gt;" />
        <fileset dir="${user.dir}">
            <include name="config.xml" />
        </fileset>
</replaceregexp>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号