【问题标题】:ant replaceregexp, matching with variable/propertyant replaceregexp,与变量/属性匹配
【发布时间】:2014-07-30 21:18:11
【问题描述】:

这应该是一个常见的问题,但我不知道该怎么做,希望在这里得到一些指导。

问题:

我需要使用 ant 替换一组 XML 配置文件(weblogic config.xml 文件)中的一些,但不是所有应用程序版本(也可以使用 ant-contrib)。

我有一个应该设置新版本的应用程序列表,它们应该有什么版本,我有我需要替换的配置文件。

到目前为止我走过的路:

我创建了一个带有三个参数的宏;应用程序名称、应用程序版本和配置文件。

这将从循环中调用。

这是我现在拥有的:

<macrodef name="update-template-config-file">
  <attribute name="application-name" />
  <attribute name="application-version" />
  <attribute name="config-file-name" />

  <sequential>

    <local name="match-string"/>
    <local name="replace-string"/>
    <property name="match-string" value="@{application-name}#[0-9](\.[0-9])*"/>
    <property name="replace-string" value="@{application-name}#@{application-version}"/>

    <echo>match ${match-string} replace: ${replace-string} in @{config-file-name}</echo>

    <replaceregexp file="@{config-file-name}"
      match="${match-string}"
      replace="${replace-string}"
      byline="true"/>
  </sequential>

</macrodef>

所以问题在于 replaceregexp 的匹配部分被解释为正则表达式(正确),我想知道是否有办法使用“匹配字符串”属性的值而不是名称它。

解决方法:

从 ant 脚本中,通过在每个循环中回显替换指令来创建一个新的 ant 脚本,然后调用创建的 ant 文件来执行替换。

为了完整起见,定位宏被调用的地方

<target name="update-template-config-files">
  <for param="config-file">
     <path>
       <fileset dir="${RP_CONTENT_ROOT_DIR}/domain_templates" includes="**/config.xml"/>
     </path>
    <sequential>
      <for param="application-file">
        <path>
          <fileset dir="${STAGE_ROOT}/applications" includes="*.*"/>
        </path>
        <sequential>
          <local name="filename"/>
          <basename property="filename" file="@{application-file}"/>
          <update-template-config-file application-name="${filename}" application-version="5.1.0.3.0" config-file-name="@{config-file}" />
        </sequential>
      </for>
    </sequential>
  </for>
</target>

【问题讨论】:

  • 回显是否正常工作?
  • 嗨,确实如此,这是替换真实应用程序名称和路径后的示例: [echo] match some_application.ear#[0-9](\.[0-9])* replace : some_application.ear#5.1.0.3.0 在 /some/path/config/config.xml
  • 我正在写一个蚂蚁任务。
  • 其实我发现了一个错误,忘记删除后缀(例如.ear)。会试试的。

标签: xml regex ant


【解决方案1】:

我的脚本中有错误,忘记从应用程序名称中删除后缀。

感谢 vio1331 让我再看一次回声线。

在下面找到有效的更新 sn-p(但仅适用于 ear 文件)

<target name="update-template-config-files">
  <for param="config-file">
     <path>
       <fileset dir="${RP_CONTENT_ROOT_DIR}/domain_templates" includes="**/config.xml"/>
     </path>
    <sequential>
      <for param="application-file">
        <path>
          <fileset dir="${STAGE_ROOT}/wlng/applications" includes="*.ear"/>
        </path>
        <sequential>
        <local name="filename"/>
        <basename property="filename" file="@{application-file}" suffix=".ear"/>
        <update-template-config-file application-name="${filename}" application-version="5.1.0.3.0" config-file-name="@{config-file}" />
        </sequential>
      </for>
    </sequential>
  </for>
</target>

【讨论】:

    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多