简体   繁体   中英

How can I load variables from an XML properties file?

I am currently loading properties from an XML file in Ant. However, I'd like to perform my current ant task inside a for loop while loading a new XML properties file for the same set of properties each time.

I'm aware of the existence of ant-contrib's var task which allows me to override properties. However, I'm unsure of how to combine this with loading properties from an XML file. I thought at first about iterating through all of the properties and then setting them to new values using the propertyregex task. Unfortunately, as I started writing code to do that I realized I didn't know how to actually load the property values still, since they can't overwrite previously set properties. (Well, they can using the var task, but this can't be used to load from an XML file as far as I can tell.)

Any ideas? What I'm ideally looking for is a task that would be called something like

<xmlvars file="myxmlpropertyfile.xml"/>

which would function just like

<xmlproperty file="myxmlpropertyfile.xml"/>

with the exception that it overwrites the variables.

One of the things you can do with the <xmlproperty> task is to prefix each property with a specific value. Why not simply use the name of your parameter as a prefix?

Otherwise, you could use the <echoproperties> task to unset all of the properties, and then loop through the next iteration of your <for> task.

Something like this, but this isn't tested:

 <for param="my.directory">
     <fileset dir="${some.directory}"/>
     <sequential>
         <xmlproperty file="@{my.directory}/myxmlpropertyfile.xml"
            prefix="foo-fighters"/>
          <blah, blah, blah/>
          <for param="reset.var">
              <echoproperty prefix=foo-fighters"/>
              <sequential>
                  <var name="@{reset.var}"
                     unset="true"/>
              </sequential>
          </for>
     </sequential>
 </for>

Basically, you use the outer <for> loop to loop through your directories, and use <xmlproperty> to set the property names. The <blah blah blah/> means do what you want. Then, before you go to the next iteration of your outer <for> loop, you do an inner <for> loop that will unset all the variables you previously set in the <xmlproperty> task.

The trick is using a variable prefix that will guarantee that the variable names are easily found. Thus foo-fighters .

It looks like I should be able to accomplish this by using the var task's unset attribute. Will report back...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM