简体   繁体   中英

read property value in Ant

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

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

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

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

我使用Ant Contribpropertyregex任务来做类似的事情。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM