将 ANT 正则表达式存储在 excel 文件中 [英] store ANT regex in an excel file
问题描述
我正在创建一个 ANT 脚本,为此我使用文件集和正则表达式来获取我的文件并获取该文件中的表达式.
我还需要将表达式存储在电子表格中,但我不知道该怎么做.
我在某处了解到可以使用 replaceregexp,但我不知道该怎么做.
I am creating an ANT script for which I am using a fileset and a regex to get my file and to get an expression in that file.
I also need to store the expression in a spreadsheet, which I am not sure how to do it.
I have learned somewhere that I could use replaceregexp, but I am not sure how do I do that.
我使用目标任务来搜索文件和表达式.
I have used target tasks to do the search for the file and expression.
推荐答案
ANT 不是一种编程语言,因此这不是一个需要解决的小问题.以下示例使用:
ANT is not a programming language so this is not a trivial problem to solve. The following example uses:
- groovy task 实现解析逻辑
- Apache POI 编写excel文件
- Apache ivy 用于管理 3rd 方 jar 依赖项.
- groovy task implement the parsing logic
- Apache POI to write the excel file
- Apache ivy to manage the 3rd party jar dependencies.
这是计算资源子目录中字符串one"出现次数的示例:
This is example counts the number of occurrences of the string "one" in the resources sub directory:
├── build.xml
├── resources
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
└── results.xlsx
结果也写入一个excel文件results.xlsx"
Results are also written to an 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 to 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 the build again"/>
</target>
</project>
这篇关于将 ANT 正则表达式存储在 excel 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!