如何用ant脚本替换所有匹配项

时间:2013-01-31 16:37:10

标签: ant

如何更换所有匹配的线?我想替换* xml文件中的所有匹配行。 下面的脚本片段只替换一行 提前致谢。 brgds

<?xml version="1.0" encoding="windows-1252" ?>
<project default="init" basedir=".">
  <property file="build.properties"/>
  <target name="init">
    <tstamp/>
    <loadfile property="jndiurl"
              srcfile="${src.model}/META-INF/persistence.xml">
      <filterchain>
        <linecontains>
          <contains value="hibernate.jndi.url"></contains>
        </linecontains>
      </filterchain>
    </loadfile>
    <replace file="${src.model}/META-INF/persistence.xml" token="${jndiurl}"
             value="${hibernate.jndi.url.live}${line.separator}"/>
    <echo>${hibernate.jndi.url.live}</echo>
    <loadfile property="providerurl"
              srcfile="${src.structure}/com/arsivist/structure/connection.properties">
      <filterchain>
        <linecontains>
          <contains value="providerurl"></contains>
        </linecontains>
      </filterchain>
    </loadfile>
    <replace file="${src.structure}/com/arsivist/structure/connection.properties"
             token="${providerurl}"
             value="${providerurl.live}${line.separator}"/>
    <echo>${providerurl.live}</echo>
    <loadfile property="ucmusername"
              srcfile="${src.roketsanutil}/com/arsivist/util/ucmConnection.properties">
      <filterchain>
        <linecontains>
          <contains value="username"></contains>
        </linecontains>
      </filterchain>
  </target>
</project>

2 个答案:

答案 0 :(得分:1)

使用复制任务作为模板系统,而​​不是尝试编辑文件,它不是更简单,它支持filterset,可用于替换生产值。

实施例

.
├── build.xml
└── src
    └── resources
        └── persistence.xml

的build.xml

<project name="demo" default="template">

    <target name="template">
        <copy todir="build/META-INF">
            <fileset dir="src/resources" includes="*.xml"/>
            <filterset>
                <filter token="HIBERNATE.HBM2DDL.AUTO" value="create-drop"/>
            </filterset>
        </copy>
    </target>

    <target name="clean">
        <delete dir="build"/>
    </target>

</project>

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
   <persistence-unit name="sample">
      <jta-data-source>java:/DefaultDS</jta-data-source>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="@HIBERNATE.HBM2DDL.AUTO@"/>
      </properties>
   </persistence-unit>
</persistence>

答案 1 :(得分:0)

该片段对我有用:

<?xml version="1.0" encoding="windows-1252" ?>
<project default="init" basedir=".">
  <property file="build.properties"/>
  <target name="init">
    <tstamp/>
    <loadfile property="jndiurl"
              srcfile="${src.model}/META-INF/persistence.xml">
      <filterchain>
        <linecontains>
          <contains value="hibernate.jndi.url"></contains>
        </linecontains>
        <headfilter lines="1"/>
      </filterchain>
    </loadfile>
    <echo>${jndiurl}</echo>
    <replace file="${src.model}/META-INF/persistence.xml" token="${jndiurl}"
             value="${hibernate.jndi.url.live}${line.separator}"/>
    <echo>${hibernate.jndi.url.live}</echo>
</project>