开发者

strip till delimeter in ant

I have a target which will run a executable and get a version. But I need to remove stuff till the delimeter. Help me please.

<target name="tomcatVersion">
  <exec executable="${WT_HOME}/tomcat/bin/catalina.bat" outputproperty="tomcat.version">
    <arg value="version" />
    <redirector>
      <outputfilterchain>
        <tokenfilter>开发者_C百科
          <containsstring contains="Server number:"/>
        </tokenfilter>
      </outputfilterchain>
    </redirector>
  </exec>
  <echo message="${tomcat.version}"/>
</target>


[Update: single step loadresource method with thanks to Matt]

You could do this by reading the output of the executable into a property and then filtering the property through a replaceregexp token filter to extract the string you require. For example:

<project default="get-version">

  <target name="get-version">
    <exec executable="bash" outputproperty="version.output">
      <arg value="ant"/>
      <arg value="-version"/>
    </exec>
    <loadresource property="version">
      <string value="${version.output}"/>
      <filterchain>
        <tokenfilter>
          <replaceregex pattern="[^\d]*(\d.\d.\d).*" replace="\1"/>
        </tokenfilter>
        <striplinebreaks/>
      </filterchain>
    </loadresource>
    <echo level="info" message="version is: '${version}'"/>
  </target>
</project>

Sample output:

$ ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010

$ ant
Buildfile: build.xml

get-version:
     [echo] version is: '1.8.2'

BUILD SUCCESSFUL
Total time: 2 seconds

(I am using ant -version as a handy stand in for whatever executable you are running. I am aware that Ant version can be got from Ant properties.)

With older versions of Ant (<1.7) you could do this in two steps:

  1. Write the output of the executable to file
  2. Read the file through a replaceregexp token filter

For example:

<project default="get-version">
  <target name="get-version">
    <exec executable="bash" output="version.out">
      <arg value="ant"/>
      <arg value="-version"/>
    </exec>
    <loadfile property="version" srcfile="version.out">
      <filterchain>
        <tokenfilter>
          <replaceregex pattern="[^\d]*(\d.\d.\d).*" replace="\1"/>
        </tokenfilter>
        <striplinebreaks/>
      </filterchain>
    </loadfile>
    <echo level="info" message="version is: '${version}'"/>
  </target>
</project>

Sample output

$ ant -version
Apache Ant version 1.6.5 compiled on June 2 2005

$ ant
Buildfile: build.xml

get-version:
     [echo] version is: '1.6.5'

BUILD SUCCESSFUL
Total time: 2 seconds


The exec task has 3 attributes to catch the output from an executable :

  • outputproperty => catches stdout
  • errorproperty => catches stderr
  • resultproperty => catches returncode

see Ant Manual for exec task

So for your purpose :

  1. use outputproperty to catch the version written to stdout
  2. grep the versionstring from outputproperty via String replace function from Ant Plugin Flaka

    <project xmlns:fl="antlib:it.haefelinger.flaka">
      <exec executable="bash" outputproperty="bashversion">
        <arg value="--version"/>
      </exec>
      <fl:let>bashversion ::= '#{replace('${bashversion}','$2','(?s)(.+)(\d\.\d\.\d\(.\)?)(.+)')}'</fl:let>
      <fl:echo>
        Bashversion => ${bashversion}
      </fl:echo>
    </project>
    

output :

[fl:echo] Bashversion => 4.1.7(1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜