tags:

views:

1839

answers:

4

Hi,

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

+2  A: 

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

Dan Dyer
A: 

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.

Rob Hruska
Unfortunately, this needs to work on windows and Linux
Don
Yeah, that'd be a bit of a hassle if you didn't have something like Cygwin installed on your Windows system.
Rob Hruska
+2  A: 

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...

Ken Gentle
<loadproperties> doesn't support a nested <tokenfilter>
Don
It does, however, support nested <filterchain> -- see http://ant.apache.org/manual/CoreTasks/loadproperties.html
Ken Gentle
+2  A: 

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

path=C:/tmp/templates
fullpath=file:${path}
spilth
Cleaner and simple.
Gastoni