2
votes

I need to read the value of a property from a file in an Ant script and strip off the first few characters. The property in question is

path=file:C:/tmp/templates

This property is store in a file that I can access within the ant script via

<property file="${web.inf.dir}/config.properties"/>

I have two questions:

  1. How do I read the single 'path' property from the loaded property file?
  2. How do I remove the leading 'file:' from the property value?

Ultimately, I'd like to have access to the following name-value pair within the Ant script:

path=C:/tmp/templates

Cheers, Don

4

4 Answers

3
votes

In Ant 1.6 or later you can use LoadProperties with a nested FilterChain

<loadproperties srcFile="${property.file.name}">
  <filterchain>
    <tokenfilter>
      <containsstring contains="path=file:"/>
      <replaceregex pattern="path=file:" replace="path=" flags=""/>
    </tokenfilter>
  </filterchain>
</loadproperties>

This should result in a path property being loaded with the string "file:" stripped.

Not tested, caveat emptor...

3
votes

How about just changing the properties file so you can access both the full and simple path?

path=C:/tmp/templates
fullpath=file:${path}
2
votes

I used the propertyregex task from Ant Contrib to do something similar.

0
votes

You could probably use ant's exec task and a system command.

I wrote this up quickly to test the concept:

<target name="foo">
  <property name="my.property" value="file:C:/foo/bar"/>
  <exec executable="/bin/cut" inputstring="${my.property}" outputproperty="new.property">
    <arg line="-d':' -f2-"/>
  </exec>
  <echo message="FOO: ${new.property}"/>
</target>

Unfortunately this only works if you can build on a system with /bin/cut or some sort of executable you can use.