How can I allow an Ant property file to override the value set in another?

后端 未结 3 1738
不思量自难忘°
不思量自难忘° 2021-01-30 10:56

I have an ant file that does the following:




        
相关标签:
3条回答
  • 2021-01-30 11:28

    Actually ant properties may be overriden. See the documentation of the property task:

    Normally property values can not be changed, once a property is set, most tasks will not allow its value to be modified.

    One of the tasks that are able to override the property value is script. Also any custom task may use this backdoor. Other proposals are in question Ant loadfile override property. This is against the spirit of ant and usually unnecessary. But it's good to know that, because I just had an opposite problem: why the property value changed although it is immutable.

    Here is a sample target that uses script task to change the value of a property. It shows the basic methods to work with properties. All methods are described in Ant Api which is not available online. You need to download the Ant Manual. In its api directory there is the api documentation.

      <target name="t1">
        <property name="a" value="one" />
        <script language="javascript">
          sProp = project.getProperty("a");
          sProp = sProp.replace("e", "ly");
          project.setProperty("a", sProp);
          project.setNewProperty("a", "new value");
        </script>
        <property name="a" value="two" />
        <echo>a=${a}</echo>
      </target>
    

    How to easily setup the script task? Making the script task running with beanshell language is a bit tricky and non-trivial, but it's explained in this answer. However as Rebse noted, using javascript language is supported out of the box in jdk 6.

    0 讨论(0)
  • 2021-01-30 11:32

    Ant property can't be overwritten unless using macro and javascript plug-in to do:

    Step 1: define a macro function to overwrite property

     <!--overwrite property's value-->
        <macrodef name="set" >
            <attribute name="name"/>
            <attribute name="value"/>
            <sequential>
                <script language="javascript">
                    <![CDATA[
                    project.setProperty("@{name}", "@{value}");
                    ]]>
                </script>
            </sequential>
        </macrodef>
    

    Step 2: use the macro in the ant xml

    <set
        name="your_target_property"
        value="your_value" or "${another_property}"     
    </set>
    
    0 讨论(0)
  • 2021-01-30 11:34

    The initial problem with your set up is that you've got build.properties and build-defaults.properties reversed.

    Ant Properties are set once and then can never be overridden. That's why setting any property on the command line via a -Dproperty=value will always override anything you've set in the file; the property is set and then nothing can override it.

    So the way you want this set up is:

    <property file="build.properties" description="local build configuration overrides"/>
    <property file="project.properties" description="Project configuration properties"/>
    <property file="build-defaults.properties" description="default build configuration."/>
    

    This way:

    1. Anything set at the command line takes precedence over build.properties
    2. Anything set in build.properties overrides other values
    3. etc. on down the line.
    0 讨论(0)
提交回复
热议问题