how to replace all matches with ant script - ant

How can i replace all matching lines? I wanna replace all matching lines in *xml file.
Script snippet which is below just only replace one line
Thanks in advance.
brgds
<?xml version="1.0" encoding="windows-1252" ?>
<project default="init" basedir=".">
<property file="build.properties"/>
<target name="init">
<tstamp/>
<loadfile property="jndiurl"
srcfile="${src.model}/META-INF/persistence.xml">
<filterchain>
<linecontains>
<contains value="hibernate.jndi.url"></contains>
</linecontains>
</filterchain>
</loadfile>
<replace file="${src.model}/META-INF/persistence.xml" token="${jndiurl}"
value="${hibernate.jndi.url.live}${line.separator}"/>
<echo>${hibernate.jndi.url.live}</echo>
<loadfile property="providerurl"
srcfile="${src.structure}/com/arsivist/structure/connection.properties">
<filterchain>
<linecontains>
<contains value="providerurl"></contains>
</linecontains>
</filterchain>
</loadfile>
<replace file="${src.structure}/com/arsivist/structure/connection.properties"
token="${providerurl}"
value="${providerurl.live}${line.separator}"/>
<echo>${providerurl.live}</echo>
<loadfile property="ucmusername"
srcfile="${src.roketsanutil}/com/arsivist/util/ucmConnection.properties">
<filterchain>
<linecontains>
<contains value="username"></contains>
</linecontains>
</filterchain>
</target>
</project>

Instead of trying to edit files in place, wouldn't it be simpler to use the copy task as a templating system, it supports a filterset which can be used to substitute production values.
Example
.
├── build.xml
└── src
└── resources
└── persistence.xml
build.xml
<project name="demo" default="template">
<target name="template">
<copy todir="build/META-INF">
<fileset dir="src/resources" includes="*.xml"/>
<filterset>
<filter token="HIBERNATE.HBM2DDL.AUTO" value="create-drop"/>
</filterset>
</copy>
</target>
<target name="clean">
<delete dir="build"/>
</target>
</project>
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="sample">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="#HIBERNATE.HBM2DDL.AUTO#"/>
</properties>
</persistence-unit>
</persistence>

That snippet works for me:
<?xml version="1.0" encoding="windows-1252" ?>
<project default="init" basedir=".">
<property file="build.properties"/>
<target name="init">
<tstamp/>
<loadfile property="jndiurl"
srcfile="${src.model}/META-INF/persistence.xml">
<filterchain>
<linecontains>
<contains value="hibernate.jndi.url"></contains>
</linecontains>
<headfilter lines="1"/>
</filterchain>
</loadfile>
<echo>${jndiurl}</echo>
<replace file="${src.model}/META-INF/persistence.xml" token="${jndiurl}"
value="${hibernate.jndi.url.live}${line.separator}"/>
<echo>${hibernate.jndi.url.live}</echo>
</project>

Related

my property file seems not to be implemented by build.xml

I have a problem with properties.
here is content of my build.xml file:
<?xml version="1.0" ?>
<project name="default" default="package">
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<property file="${basedir}/localproperties"/>
<property name="javac.debug" value="off"/>
<target name="compile" depends="init" description="Compiles JAVA files">
<echo message="Debug: ${javac.debug}"/>
<javac srcdir="src"
destdir="build/classes"
classpathref="compile.classpath"
debug="${javac.debug}"/>
</target>
<path id="compile.classpath">
<fileset dir="lib" includes="*.jar"/>
</path>
</project>
and here is content of my localproperties file:
javac.debug = on
please note that I have saved localproperties as .xml file and put it into the same directory as build.xml
the problem is that it does not work as the output I get is:
Debug: off
clearly it should be:
Debug: on
please advise.
It should be:
<property file="${basedir}/localproperties.xml"/>
There is no extension assumed by the property task. As long as the content follows the key-value convention, it can be any file extension. But it would be confusing to save as .xml. Just save it as localproperties.properties, or simply local.properties.

PMD ANT script gives taskdef class net.sourceforge.pmd.ant.PMDTask cannot be found using the classloader AntClassLoader[]

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="info" name="MyProject">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="lib.dir" value="lib"/>
<property name="jar.name" value="${ant.project.name}"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" debug="true" nowarn="true" debuglevel="lines,vars,source"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${jar.name}.jar" basedir="${classes.dir}">
<exclude name="**/Main.class" />
<fileset dir="${src.dir}" includes="**/*.java">
<exclude name="**/Main.java" />
</fileset>
<zipgroupfileset dir="${lib.dir}" includes="*.jar">
<exclude name="**/Utils.jar" />
</zipgroupfileset>
</jar>
</target>
<target name="build" depends="jar"/>
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" />
<target name="pmd">
<pmd shortFilenames="true" failuresPropertyName="failures.count" rulesetfiles="\path\pmd\ruleSet.xml">
<formatter type="html" toFile="pmd_report.html" toConsole="true"/>
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
<target name="info">
<echo message="Available Targets:"/>
<echo message=" clean"/>
<echo message=" compile"/>
<echo message=" jar"/>
<echo message=" build"/>
<echo message=" pmd"/>
</target>
</project>
This script gives me this " taskdef class net.sourceforge.pmd.ant.PMDTask cannot be found using the classloader AntClassLoader[]"
I have added PMD library jar file in lib folder of the project,where other libraries are present as jar files.
But if i change add path ref to the library not as a jarfile , it works well.
<path id="pmd.classpath">
<fileset dir="C:\Users\PMD\pmd-bin-5.5.2">
<include name="**/*.jar"/>
</fileset>
</path>
May I know what is the problem? I am pretty new to ANT and PMD , any help will be appreciated.
Thanks
First thing to check is the file pmd-core-*.jar in your lib directory?
Secondly add a reference to the path in the taskdef task:
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="classpath"/>

Ant creates jar files in wrong destination

Below is my build.xml file for ant.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Struts2ProductsDemo" default="jar">
<property name="src.dir" location="src" />
<property name="build.dir" location="L:\build" />
<property name="project.name" value="Struts2ProductsDemo" />
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/classes" />
</target>
<target name="compile" depends="clean,makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" />
</target>
<target name="jar" depends="compile">
<jar destfile="${build.jar}/jars/${project.name}.jar" basedir="${build.dir}/classes" />
</target>
The jar files are created in: C:\Users\San\EclipseWS\Struts2ProductsDemo\${build.jar}\jars\Struts2ProductsDemo.jar
I am expecting the jar to be here: ${build.jar}/jars/${project.name}.jar
Property build.jar is never set. You have to define it. For example, you may add this to your build:
<property name="build.jar" location="target"/>

Illegal value at 'filematch': patternSet{ includes: [Index_*.xml] excludes: [] }

I have to search a given string "OK" from a file Index_*.xml. This * is a random generated Id. This file is generated after every 15seconds
This is what I have done so far, but I am getting the exception " Illegal value at 'filematch': patternSet{ includes: [Index_*.xml] excludes: [] }"
<?xml version="1.0" encoding="UTF-8"?>
<project name="StringSearch" default="wait-for-some-time" basedir=".">
<patternset id="filematch">
<include name="Index_*.xml"/>
</patternset>
<target name="wait-for-some-time">
<waitfor maxwait="15" maxwaitunit="second" timeoutproperty="notfound">
<resourcecontains refid="filematch" substring="OK" />
</waitfor>
<antcall target="success" />
</target>
<target name="success" depends="fail" unless="notfound">
<echo message="String OK found" />
</target>
<target name="fail" if="notfound">
<echo message="String not found" />
</target>
</project>
Any suggestion of what is wrong here or may be another approach if this is not feasible.
Thanks
New code:
Using this
<path id="pathtoIndexfile">
<fileset dir="${destination.dir}">
<include name="Index_*.xml"/>
</fileset>
</path>
<target name="wait-for-some-time">
<echo>${destination.dir}</echo>
<waitfor maxwait="1" maxwaitunit="minute" timeoutproperty="notfound">
<resourcecontains refid="pathtoIndexfile" substring="OK" />
</waitfor>
<antcall target="success" />
</target>
1) The exception is "java.lang.ClassCastException: org.apache.tools.ant.types.Path cannot be cast to org.apache.tools.ant.types.Resource".
2) With fileSet the exception is "java.lang.ClassCastException: org.apache.tools.ant.types.FileSet cannot be cast to org.apache.tools.ant.types.Resource"
Am I missing some Jars?
I have achieved the solution by two steps which are as follows:
Step 1: Copying the index_*.xml file to output.xml file
Step 2: Search OK in the output.xml file
My improvised code which does the search of a string "OK" is
<target name="StringSearch">
<sleep seconds="10"/>
<copy tofile="${destination.dir}/output.xml">
<fileset dir="${destination.dir}">
<include name="Index_*.xml"/>
</fileset>
</copy>
<sleep seconds="10"/>
<echo>destination.dir is: ${destination.dir}</echo>
<loadfile property="OK.exists" srcfile="${destination.dir}/output.xml">
<filterchain>
<linecontainsregexp>
<regexp pattern="OK"/>
</linecontainsregexp>
</filterchain>
</loadfile>
<echo>OK exists: ${OK.exists}</echo>
</target>

Replace property in a xml file using ANT

I'm trying to replace a version number in a build.xml file using an ANT script.
I've tried various approaches, searched and re-searched StackOverflow for answers but could not get the exact query.
so here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<project name="feature" default="main" basedir=".">
<target name="init">
<property name="Version" value="1.0.0.20120327"/>
</target>
<target name="main" depends="init">
<description>Main target</description>
</target>
</project>
Now as u can see the Version has yesterday's date. I need to replace it with the current date.
Here is what I've tried:
<target name="replace">
<tstamp >
<format property="touch.time" pattern="yyyyMMdd"/>
</tstamp>
<property name="Feature.dir" location="../feature" />
<!--Didnt Work-->
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"
filtering="yes" overwrite="yes">
<filterset>
<filter token="Version" value="1.0.0.${touch.time}"/>
</filterset>
</copy>
<!--Didnt work
<replacetoken><![CDATA[<property name="Version" value=""/>]]>
</replacetoken>
<replacevalue><![CDATA[<property name="Version"value="1.0.0.${touchtime}" />]]>
</replacevalue>
-->
<!-- Didnt work
<copy file="${Feature.dir}/build.xml" tofile="${Feature.dir}/build1.xml" >
<filterchain>
<tokenfilter>
<replaceregex pattern="^[ \t]*Version[ \t]*=.*$"
replace="Version=1.0.0.${touch.time}"/>
</tokenfilter>
</filterchain>
</copy>
-->
</target>
I would use replaceregex inside a filterchain.
For example:
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"
filtering="yes" overwrite="yes">
<filterchain>
<tokenfilter>
<replaceregex pattern="1.0.0.[0-9.]*" replace="1.0.0.${touch.time}"/>
</tokenfilter>
</filterchain>
</copy>
If you want to replace the file, feel free to copy to a temp file and move it back.
<tempfile property="build.temp.file.name"/>
<copy file="${Feature.dir}\build.xml" tofile="${build.temp.file.name}" ... />
<move file="${build.temp.file.name}" tofile="${Feature.dir}\build.xml" />

Resources