Переопределение игнорируется для свойства

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,

Verbose Output :

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


ant
person user421551    schedule 16.08.2010    source источник


Ответы (3)


После установки свойства Ant его значение нельзя изменить.

Возможно, вы могли бы использовать атрибут prefix задачи <property> при первой загрузке файла свойств, чтобы свойство имело другое имя:

<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>

Примечание. Я не проверял это.

Затем во второй цели вы все еще можете использовать собственное имя, поскольку это свойство еще не установлено.

(это решение работает только с установленным сторонним плагином ANT-contrib-library: Скачать ANT contrib)

person Dan Dyer    schedule 16.08.2010
comment
спасибо @дан. вот ответ, как написано в руководстве муравья: свойства неизменяемы: тот, кто устанавливает свойство первым, замораживает его для остальной части сборки; они определенно не являются переменными. (ant.apache.org/manual/index.html) - person martin jakubik; 21.05.2012

Если вы уже используете банку Ant-Contrib, как видно из вашего примера, вы можете изменить определение своего свойства на var (см. http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html)

person Daniele    schedule 09.03.2012

Использовать

<property name="x" value="6"/>
<echo>${x}</echo>   <!-- will print 6 -->
<var name="x" unset="true"/>
<property name="x" value="12"/>
<echo>${x}</echo>   <!-- will print 12 -->

Это работа для моего штрафа!

person lemon55    schedule 17.06.2021
comment
Эй, добро пожаловать в SO, убедитесь, что всякий раз, когда вы добавляете фрагмент кода, вы также добавляете какое-то объяснение. - person ILoveLogCat; 19.06.2021