我有以下要求。在我的项目中,有一个目录托管着一组谷歌protobuf模式文件(.proto(。由于.proto文件应该与我的应用程序通信的其他项目同步,我希望有一个ANT构建任务来验证本地和"远程".proto文件是否相同。
以下是一个示例项目目录结构:
${project.root}
etc
refData
refdata.proto
marketData
md.proto
ANT任务会将文件路径映射到属性名称。属性将依次用于查找远程路径。对于上面的示例目录,我应该能够将etc目录下的每个.proto文件映射到以下属性:
refdata.refdata.proto=[configure remote path here]
marketdata.md.proto=[configure remote path here]
我对ANT不是很熟悉,我应该用什么任务来实现以上目标?
<?xml version="1.0" encoding="UTF-8" ?>
<project default="all" basedir=".">
<target name="all">
<property name="project.root" value="c:" />
<property name="refdata.refdata.proto" value="${project.root}etcrefDatarefdata.proto" />
<echo message="refdata.refdata.proto = ${refdata.refdata.proto}" />
<property name="marketdata.md.proto" value="${project.root}etcmarketDatamd.proto" />
<echo message="marketdata.md.proto = ${refdata.refdata.proto}" />
</target>
</project>
文件结构:
.
├── build.xml
├── etc
│ ├── a
│ │ └── aa.proto
│ └── b
│ └── bb.proto
└── some.properties
some.properties:
a.aa.proto=propa
b.bb.proto=propb
build.xml:
<project name="a" default="props" basedir=".">
<!-- define file containing *.proto propterties -->
<property name="prop.file" value="some.properties" />
<target name="props">
<echo>props file: ${prop.file}</echo>
<!-- create an OR regex based on file names
while stripping the path prefix -->
<pathconvert property="proto.files" pathsep="|">
<map from="${basedir}/etc/" to="" />
<fileset dir="${basedir}">
<include name="**/*.proto" />
</fileset>
</pathconvert>
<echo>proto files: ${proto.files}</echo>
<!-- load converted regex into new property -->
<loadresource property="proto.props">
<!-- use property as a resource, avoids temp files -->
<propertyresource name="proto.files" />
<!-- replace / with . -->
<filterchain>
<tokenfilter>
<filetokenizer />
<replacestring from="/" to="." />
</tokenfilter>
</filterchain>
</loadresource>
<echo>proto props: ${proto.props}</echo>
<!-- load properties matching the regex -->
<loadproperties srcfile="${prop.file}">
<filterchain>
<containsregex pattern="${proto.props}" />
</filterchain>
</loadproperties>
<!-- confirm -->
<echo>a:${a.aa.proto}</echo>
<echo>b:${b.bb.proto}</echo>
</target>
</project>
生成输出:
Buildfile: /tmp/a/build.xml
props:
[echo] props file: some.properties
[echo] proto files: a/aa.proto|b/bb.proto
[echo] proto props: a.aa.proto|b.bb.proto
[echo] a:propa
[echo] b:propb
BUILD SUCCESSFUL
Total time: 1 second