2010-08-16 57 views
4
Content of Build.xml File 


<?xml version="1.0"?> 
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="ant-contrib-0.6.jar"/> 
<taskdef classpath="orangevolt-ant-tasks-1.3.2.jar" resource="com/orangevolt/tools/ant/taskdefs.properties"/>    
<project name="initinstaller" default="all" basedir="." >  
<target name="configure-server-types-module">  
    <property file="./installation.conf"/> 
    <echo message="${client.server.types}"/> 
    <if> 
     <not>    
      <contains string="${client.server.types}" substring="tomcat" /> 
     </not> 
    <then> 
     <replaceregexp file="./installation.conf" 
       match="client.server.types=(.*)" 
       replace="client.server.types=\1tomcat," 
       byline="true"> 
     </replaceregexp> 
    </then> 
    </if>  
</target> 

<target name="all" depends="configure-server-types-module">  
    <property file="./installation.conf"/> 
    <echo message="${client.server.types}"/> 
    <if> 
     <not>    
      <contains string="${client.server.types}" substring="tomcat" /> 
     </not> 
    <then> 
     <replaceregexp file="./installation.conf" 
       match="client.server.types=(.*)" 
       replace="client.server.types=\1tomcat," 
       byline="true"> 
     </replaceregexp> 
    </then> 
    </if>  
</target> 

覆盖忽视了物业

Content of installation.conf : client.server.types=jboss, 

详细输出:

Apache Ant version 1.8.1 compiled on April 30 2010 
Trying the default build file: build.xml 
Buildfile: D:\testing\build.xml 
Detected Java version: 1.6 in: C:\Program Files\Java\jdk1.6.0_21\jre 
Detected OS: Windows XP 
parsing buildfile D:\testing\build.xml with URI = file:/D:/testing/build.xml 
Project base dir set to: D:\testing 
parsing buildfile jar:file:/C:/apache-ant-1.8.1/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/C:/apache-ant-1.8.1/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file 
dropping D:\testing\ant-contrib-0.6.jar from path as it doesn't exist 
dropping D:\testing\orangevolt-ant-tasks-1.3.2.jar from path as it doesn't exist 
Build sequence for target(s) `all' is [configure-server-types-module, all] 
Complete build sequence is [configure-server-types-module, all, ] 

configure-server-types-module: 

[property] Loading D:\testing\installation.conf 

[echo] jboss, 

[replaceregexp] Replacing pattern 'client.server.types=(.*)' with 
'client.server.types=\1tomcat,' in 'D:\testing\installation.conf' by line. 

[replaceregexp] File has changed; saving the updated file 


all: 

[property] Loading D:\testing\installation.conf 

**Override ignored for property "client.server.types"** 

[echo] jboss, 

[replaceregexp] Replacing pattern 'client.server.types=(.*)' with 
'client.server.types=\1tomcat,' in 'D:\testing\installation.conf' by line. 

[replaceregexp] File has changed; saving the updated file 

BUILD SUCCESSFUL 

Total time: 0 seconds 

============================================================== 

我的问题是,如何我可以覆盖Ant属性

回答

6

一旦一个Ant属性已设置了值不能改变。

也许你可以使用<property>任务的prefix属性,当你第一次加载属性文件,这样的酒店有不同的名称:

<target name="configure-server-types-module">  
    <property file="./installation.conf" prefix="temp."/> 
    <echo message="${temp.client.server.types}"/> 
    <if> 
    <not>    
     <contains string="${temp.client.server.types}" substring="tomcat" /> 
    </not> 
    <then> 
     <replaceregexp file="./installation.conf" 
        match="client.server.types=(.*)" 
        replace="client.server.types=\1tomcat," 
        byline="true"> 
     </replaceregexp> 
    </then> 
    </if>  
</target> 

注:我没有测试过这一点。

然后在第二个目标中,您仍然可以使用正确的名称,因为该属性尚未设置。

+0

感谢@丹。这里是蚂蚁手册中写的答案:“属性是不可变的:任何设置属性的人都会先冻结其余部分;它们绝对不是变量。” (http://ant.apache.org/manual/index.html) – 2012-05-21 14:21:04