Search code examples
buildant

ANT build : Variable inside resolver:artifacts is undefined


I have setup this ANT build.xml file which pulls in version info from a txt file , reads the first line, trims it and copies it into a variable called 'versionVal' with below code:

<target name="clean"
      .
      . do something more
      .
      <loadfile property="versionValTxt" srcfile="version.txt">
                <filterchain>
                    <filterreader classname="org.apache.tools.ant.filters.HeadFilter">
                        <param name="lines" value="1" />
                    </filterreader>
                </filterchain>
            </loadfile>
            <loadresource property="versionVal">
              <propertyresource name="versionValTxt"/>
              <filterchain>
                <tokenfilter>
                  <filetokenizer/>
                  <replacestring from="V" to=""/>
                </tokenfilter>
                <striplinebreaks/>
              </filterchain>
            </loadresource>
            <echo>"Building for version: ${versionVal}"</echo>
    </target>

And in one of the targets I am trying to refer a resolver artifact which uses this versionVal to find a file with that specific version in its name as shown below:

<resolver:artifacts id="producedArtifacts" >
        <resolver:artifact file="${dist.dir}/App.${versionVal}.zip"/>
    </resolver:artifacts>

    <target name="nexus">
        <echo>"versionVal: ${versionVal}"</echo>
        <resolver:deploy artifactsref="producedArtifacts">
            <resolver:remoterepo refid="ossrh"/>
        </resolver:deploy>
    </target>

And the build keeps failing as below, where it shows the variable versionVal is undefined.

C:\Users\XYZ\git\App\WebContent\dist\App.${versionVal}.zip does not exist

Note that the block is able to recognize ${dist.dir} but it doesnt recognize ${versionVal}. However I am able to print the value using an echo inside the target-nexus.

Much appreciated if anyone can point me in the right direction. I am not able to figure out why this variable is not recognized under "resolver:artifact file" and if there are any alternatives to this problem.


Solution

  • Realised I had to include below resolver artifact inside a target block and then use that as depends in the nexus block. After making this change the variable 'versionVal' was recognized.

    Solution:

    <target name="packagedArtifact" >
            <resolver:artifacts id="producedArtifacts" >
                    <resolver:artifact file="${dist.dir}/App.${versionVal}.zip"/>
                </resolver:artifacts>
          </target>
      
        <target name="nexus" depends="packagedArtifact">
            <resolver:deploy artifactsref="producedArtifacts">
                <resolver:remoterepo refid="ossrh"/>
            </resolver:deploy>
    </target>