简体   繁体   English

读取Ant中的属性值

[英]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. 我需要从Ant脚本中的文件中读取属性的值,并删除前几个字符。 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 此属性存储在我可以在ant脚本中访问的文件中

<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? 如何从加载的属性文件中读取单个“path”属性?
  2. How do I remove the leading 'file:' from the property value? 如何从属性值中删除前导'file:'?

Ultimately, I'd like to have access to the following name-value pair within the Ant script: 最后,我想在Ant脚本中访问以下名称 - 值对:

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 在Ant 1.6或更高版本中,您可以将LoadProperties与嵌套的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. 这应该导致path属性加载字符串“file:”被剥离。

Not tested, caveat emptor... 未经测试,请注意......

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

You could probably use ant's exec task and a system command. 您可以使用ant的exec任务和系统命令。

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. 不幸的是,只有在可以使用/ bin / cut或可以使用某种可执行文件的系统上构建时,这才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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