Change values of list in ANT
Asked Answered
C

3

1

I need to change the values of an ANT-script list in real time. This is the situation;

I have these properties:

x.y.6.1=something1
x.y.6.2=something2
x.y.6.3=something3

list=6.1,6.2

I want the list to become list=something1;something2

This is the gist of the code;

<target name="target1">
    <foreach list="${list}" target="target2" param="var" delimiter="," />
</target>

<target name="target2">
    <propertycopy name="var" from="x.y.${var}" silent="true"/>
</target>

Now, the propertycopy part works, however, it will not keep the new value. I tried many variations, none which worked. I am using ant-contrib.

Help would be much appreciated! Adam

Cristobal answered 23/6, 2013 at 9:33 Comment(0)
C
1

I have solved the problem, in an icky way, but it works great!

<project name="Test" default="main">
    <property file="agent.properties" />
    <property file="temp_updates.txt" />
    <taskdef name="propertycopy" classname="net.sf.antcontrib.property.PropertyCopy" />
    <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" />

    <target name="main">
        <property name="Agent Updates" value="6.1,6.2" />
        <antcall target="create_temp_files" />
        <antcall target="agent_updates_target" />
        <propertycopy name="custom.agent.release.group" from="updates" silent="true" override="true" />
    </target>

    <target name="agent_updates_target">
        <foreach list="${Agent Updates}" target="agent_version_to_path" param="var" delimiter="," />
    </target>

    <target name="agent_version_to_path">
        <propertycopy name="var" from="agent.installer.${var}" silent="true" override="true"/>
        <echo message="${var};" file="temp_updates.txt" append="true" />
    </target>

    <target name="create_temp_files">
        <echo message="updates=" file="temp_updates.txt" />
    </target>

</project>

on another file, "agent.properties" I had that;

agent.installer.6.3=something3
agent.installer.6.2=something2
agent.installer.6.1=something1
agent.installer.6.0=...
agent.installer.5.6=...
agent.installer.5.0.12=...
agent.installer.5.0.11=...
agent.installer.5.0.9.5=...
agent.installer.3.8=...
agent.installer.3.7=...

As a result, a new file "temp_updates.txt" was created, having

updates=something1;something2;

Which I then loaded into the actual program.

May not be pretty, but it works quite well.

Thank you Skoll and Mark O'Connor for all your help, I used those ideas to come up with this one. I would rate you, but I can't :( Sorry!

Cristobal answered 24/6, 2013 at 9:11 Comment(2)
Of course you can :) just click on the up sign! Glad you're pb is solved.Hofstetter
I will, once I am rank 15 or above haha :)Cristobal
H
1

The target attribute of your foreach should be the name of the target called.

I guess here it should be <foreach list="${list}" target="agent_version_to_path" param="var" delimiter="," />

If I'm wrong, post your target2 and explain what you're trying to do.

Edit:

Ok for your edit, did you already try override="yes"?

And cannot you change your name of property (var) it is quite confusing!

Hofstetter answered 23/6, 2013 at 12:7 Comment(3)
Thank you for your comment, it was my bad - I wanted to remove anything that has to do with the real code (to make it less confusing) - I forgot to change "agent_version_to_path" to "target2" (only in the example) I edited it, thanks again for notifying meCristobal
The name "var" was just for testing purposes, I am quite new to ANT - as a matter of fact, I never used it before, but I need to modify much of our scripts for our new needs. Where do I put the override? In the PropertyCopy part?Cristobal
yes, see Ant-contrib Tasks: Propertycopy. For your name "var" of property copy, you might use name="version.number" or something more understandable.Hofstetter
L
1

I'm not a fan of the ant-contrib tasks. Have you considered embedding a scripting language instead?

  <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

  <groovy>
     properties["list"].split(",").each {
        println properties["x.y.${it}"]
     }
  </groovy>

Update

Here's a more complete example that loops and calls another target:

$ ant
Buildfile: build.xml

process:

doSomething:
     [echo] something1

doSomething:
     [echo] something2

BUILD SUCCESSFUL
Total time: 0 seconds

build.xml

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

   <property file="build.properties"/>

   <path id="build.path">
      <pathelement location="lib/groovy-all-2.1.5.jar"/>
   </path>

   <target name="process" description="Process values in a list">
      <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

      <groovy>
         properties["list"].split(",").each {
            properties.var = properties["x.y.${it}"]
            ant.ant(target:"doSomething")
         }
      </groovy>
   </target>

   <target name="doSomething">
      <echo>${var}</echo>
   </target>

</project>
Lordship answered 23/6, 2013 at 12:28 Comment(2)
That approach sounds interesting - but you seem to print the list variables - is it possible to make it so list(0) = x.y.${it}? Currently it's list(0) = ${it}.Cristobal
@AdamCohen Not clear what you're trying to do. I've included a more complete example.Es
C
1

I have solved the problem, in an icky way, but it works great!

<project name="Test" default="main">
    <property file="agent.properties" />
    <property file="temp_updates.txt" />
    <taskdef name="propertycopy" classname="net.sf.antcontrib.property.PropertyCopy" />
    <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" />

    <target name="main">
        <property name="Agent Updates" value="6.1,6.2" />
        <antcall target="create_temp_files" />
        <antcall target="agent_updates_target" />
        <propertycopy name="custom.agent.release.group" from="updates" silent="true" override="true" />
    </target>

    <target name="agent_updates_target">
        <foreach list="${Agent Updates}" target="agent_version_to_path" param="var" delimiter="," />
    </target>

    <target name="agent_version_to_path">
        <propertycopy name="var" from="agent.installer.${var}" silent="true" override="true"/>
        <echo message="${var};" file="temp_updates.txt" append="true" />
    </target>

    <target name="create_temp_files">
        <echo message="updates=" file="temp_updates.txt" />
    </target>

</project>

on another file, "agent.properties" I had that;

agent.installer.6.3=something3
agent.installer.6.2=something2
agent.installer.6.1=something1
agent.installer.6.0=...
agent.installer.5.6=...
agent.installer.5.0.12=...
agent.installer.5.0.11=...
agent.installer.5.0.9.5=...
agent.installer.3.8=...
agent.installer.3.7=...

As a result, a new file "temp_updates.txt" was created, having

updates=something1;something2;

Which I then loaded into the actual program.

May not be pretty, but it works quite well.

Thank you Skoll and Mark O'Connor for all your help, I used those ideas to come up with this one. I would rate you, but I can't :( Sorry!

Cristobal answered 24/6, 2013 at 9:11 Comment(2)
Of course you can :) just click on the up sign! Glad you're pb is solved.Hofstetter
I will, once I am rank 15 or above haha :)Cristobal

© 2022 - 2024 — McMap. All rights reserved.