问题描述
我想从 Ant 脚本之外生成的文件名中提取版本号.
I want to extract the version number from a file name generated outside of my Ant script.
一个外部构建工具(PDE build)在一个众所周知的目录中创建了一个artifactid-1.2.3.201101010101.jar
形式的文件,但我不能事先告诉版本信息.我需要将该文件名中的版本部分 (1.2.3.201101010101
) 提取到 Ant 属性中以进行进一步处理,例如变量替换.
An external build tool (PDE build) creates a file of the form artifactid-1.2.3.201101010101.jar
in a well-known directory, but I can not tell the versioning information beforehand. I need to extract the version part (1.2.3.201101010101
) from that file name into an Ant property for further processing, e.g. variable substitution.
使用 ant-contrib 是可以接受的,但是我还没有找到提取这些信息的方法.
Using ant-contrib is acceptable, however I have not found a way to extract this information.
推荐答案
这是使用 ant-contrib 的解决方案 PropertyRegex 任务.
Here's a solution using the ant-contrib PropertyRegex task.
- 获取文件名到路径中.
- 将路径转换为属性.
- PropertyRegex 属性(ant-contrib).
您可以通过将属性值写入临时文件,然后使用带有过滤器链的 loadfile 从中提取工件 ID 来避免 ant-contrib.请参阅此答案 为例.
You could avoid ant-contrib by writing the property value to a temporary file and then using loadfile with a filterchain to extract the artifact id from it. See this answer for an example.
<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>
输出
$ 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
这篇关于如何使用 Apache Ant 提取文件名的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!