问题描述
我想从我的Ant脚本之外生成的文件名中提取的版本号。
I want to extract the version number from a file name generated outside of my Ant script.
这是外部构建工具(PDE构建)创建一个众所周知的目录形式的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.
推荐答案
下面是用蚂蚁的contrib的任务。
Here's a solution using the ant-contrib PropertyRegex task.
- 获取文件名进入的路径。
- 转换路径的属性。
- PropertyRegex属性(蚂蚁的contrib)。
您可以通过属性值写入到一个临时文件,然后使用的loadFile用filterchain从中提取神器ID避免蚂蚁的contrib。见this回答一个例子。
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文件名的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!