store ANT regex in an excel file -
i creating ant script using fileset , regex file , expression in file.
need store expression in spreadsheet, not sure how it.
have learned somewhere use replaceregexp, not sure how do that.
i have used target tasks search file , expression.
ant not programming language not trivial problem solve. following example uses:
- groovy task implement parsing logic
- apache poi write excel file
- apache ivy manage 3rd party jar dependencies.
example
this example counts number of occurrences of string "one" in resources sub directory:
├── build.xml ├── resources │ ├── file1.txt │ ├── file2.txt │ └── file3.txt └── results.xlsx results written excel file "results.xlsx"
parse-files: [groovy] file:file1.txt count:1 [groovy] file:file2.txt count:0 [groovy] file:file3.txt count:1 build.xml
<project name="demo" default="parse-files" xmlns:ivy="antlib:org.apache.ivy.ant"> <available classname="org.apache.ivy.main" property="ivy.installed"/> <target name="parse-files" depends="init" > <taskdef name="groovy" classname="org.codehaus.groovy.ant.groovy" classpathref="groovy.path"/> <fileset dir="resources" id="filestoparse" includes="*.txt"/> <groovy> import org.apache.poi.ss.usermodel.workbook import org.apache.poi.xssf.usermodel.xssfworkbook import org.apache.poi.ss.usermodel.creationhelper import org.apache.poi.ss.usermodel.sheet import org.apache.poi.ss.usermodel.row workbook wb = new xssfworkbook(); sheet sheet = wb.createsheet("regexp results"); creationhelper helper = wb.getcreationhelper(); rowpos = 0 project.references.filestoparse.each { def file = it.file def content = file.text // search file def findonestr = content =~ /one/ println "file:${file.name} count:${findonestr.count}" // save excel row row row = sheet.createrow(rowpos++) row.createcell(0).setcellvalue(helper.createrichtextstring(file.name)); row.createcell(1).setcellvalue(findonestr.count); } // save excel file new file("results.xlsx").withoutputstream { wb.write(it) } </groovy> </target> <target name="init" depends="install-ivy" description="resolve dependencies"> <ivy:cachepath pathid="groovy.path"> <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.3" conf="default"/> <dependency org="org.apache.poi" name="poi-ooxml" rev="3.10.1" conf="default"/> </ivy:cachepath> </target> <target name="install-ivy" description="install ivy" unless="ivy.installed"> <mkdir dir="${user.home}/.ant/lib"/> <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/> <fail message="ivy has been installed. run build again"/> </target> </project>
Comments
Post a Comment