Search code examples
antant-contrib

Ant input called multiple times if used with ant-contrib foreach task


I have this code snippet inside build.xml file.

<input message="Please enter environment:" addproperty="environment" defaultvalue="dev"/>

<target name="DeployComposites">
  <echo>Deploying projects ${composite.list}</echo>
  <foreach list="${composite.list}" param="compositeName" target="compile-and-deploy" inheritall="false"/>
</target>

The input prompts multiple times for the property value.Is there a way to make it ask only once


Solution

  • The way foreach works it creates a new Ant Project for each invocation of the desired target. Since you have the input at the top level it will be called each time a new Project is created.

    Instead, put it inside another target, for example

    <target name="get-env">
      <input message="Please enter environment:" addproperty="environment" defaultvalue="dev"/>
    </target>
    
    <target name="DeployComposites" depends="get-env">
      <echo>Deploying projects ${composite.list}</echo>
      <foreach list="${composite.list}" param="compositeName" target="compile-and-deploy" inheritall="false"/>
    </target>