简体   繁体   English

使用 Ant 或 Phing 在属性中存储值

[英]Store a Value in a Property with Ant or Phing

With Ant or Phing, I need to load a file's contents into a property, run a regular expression on the value of that property, and then store the result of that regular expression in another property.使用 Ant 或 Phing,我需要将文件的内容加载到属性中,对该属性的值运行正则表达式,然后将该正则表达式的结果存储在另一个属性中。 What's the best way to do this?最好的方法是什么?

I can load the file into a property easily (with Phing) like so:我可以轻松地将文件加载到属性中(使用 Phing),如下所示:

<loadfile file="myfile.txt" property="my.file" />

And I know how to update the file, but I can't seem to figure out how to run a regex on that property, and store the result in a new property for future use.而且我知道如何更新文件,但我似乎无法弄清楚如何在该属性上运行正则表达式,并将结果存储在新属性中以备将来使用。

Any help would be greatly appreciated!任何帮助将不胜感激!

Update更新

I've been tinkering with it, and this will work.我一直在修补它,这将起作用。 Let me know if there's a streamlined way though, The code below loads a file into a property, then reduces it to only the line that contains the title tag.让我知道是否有简化的方法,下面的代码将文件加载到属性中,然后将其减少到仅包含title标签的行。 And then, it runs a regular expression on that line, and stores the contents of that tag in my.prop.然后,它在该行上运行一个正则表达式,并将该标记的内容存储在 my.prop 中。

<loadfile file="../index.html" property="my.prop">
  <filterchain>
     <linecontainsregexp>
        <regexp pattern="&lt;title>" />
    </linecontainsregexp>

    <replaceregexp>
        <regexp pattern="[\s\S]+<title>(.+?)</title>" replace="$1" />
    </replaceregexp>
  </filterchain>
</loadfile>

Update 2更新 2

Actually, I ended up using an adhoc task to create my own.实际上,我最终使用了一个adhoc任务来创建自己的任务。 Worked perfectly!工作完美!

You can run an arbitrary command from an ant target like this:您可以从 ant 目标运行任意命令,如下所示:

<exec executable="bash">
   <arg line="script.sh"/>
</exec>

You can for example store the result of the regexp in a tmp file and then load it into another property the same way as the initial one.例如,您可以将正则表达式的结果存储在 tmp 文件中,然后以与初始属性相同的方式将其加载到另一个属性中。

In Phing you could process the content of the property where you loaded the file using the "php" task.在 Phing 中,您可以使用“php”任务处理加载文件的属性的内容。 See: http://www.phing.info/docs/guide/stable/chapters/appendixes/AppendixB-CoreTasks.html#PhpEvalTask请参阅: http://www.phing.info/docs/guide/stable/chapters/appendixes/AppendixB-CoreTasks.html#PhpEvalTask

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

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