如何使用Apache Ant提取部分文件名?

Phi*_*das 3 ant

我想从我的Ant脚本之外生成的文件名中提取版本号.

外部构建工具(PDE构建)artifactid-1.2.3.201101010101.jar在一个众所周知的目录中创建表单文件,但我无法预先告知版本信息.我需要1.2.3.201101010101将该文件名中的version part()提取到Ant属性中以进行进一步处理,例如变量替换.

使用ant-contrib是可以接受的,但是我还没有找到提取这些信息的方法.

sud*_*ode 7

这是使用ant-contrib PropertyRegex任务的解决方案.

  • 将文件名放入路径中.
  • 将路径转换为属性.
  • PropertyRegex属性(ant-contrib).

您可以通过将属性值写入临时文件然后使用带有过滤器链的loadfile从中提取工件ID来避免ant-contrib.请参阅此答案以获取示例.

  <project default="get-revision-number">
        <taskdef resource="net/sf/antcontrib/antcontrib.properties">
                <classpath>
                        <pathelement location="c:/lib/ant-contrib/ant-contrib-1.0b3.jar"/>
                </classpath>
        </taskdef>

        <target name="get-revision-number">
                <path id="artifact.id.path">
                        <fileset dir=".">
                                <include name="artifactid-*.jar"/>
                        </fileset>
                </path>
                <property name="artifact.id.file" refid="artifact.id.path"/>
                <echo message="artifact.id.file: ${artifact.id.file}"/>
                <propertyregex property="artifact.id" input="${artifact.id.file}" regexp=".*artifactid-(.*).jar" select="\1" />
                <echo message="artifact.id: ${artifact.id}"/>
        </target>
  </project>
Run Code Online (Sandbox Code Playgroud)

产量

$ ant
Buildfile: C:\tmp\build.xml

get-revision-number:
     [echo] artifact.id.file: C:\tmp\artifactid-1.2.3.201101010101.jar
     [echo] artifact.id: 1.2.3.201101010101

BUILD SUCCESSFUL
Total time: 0 seconds
Run Code Online (Sandbox Code Playgroud)