Search code examples
antant-contrib

Ant <chdir> task?


How can I change the current working directory in Ant?

The Ant documentation has no <chdir> task, and it seems to me that it is the best practice NOT to change the current working directory.

But let's assume we still want to do it - how would you accomplish that? Thanks!


Solution

  • Following Mark O'Connor's hint in the question comments, I came up with the following solution:

    <project name="cwd" default="parent">
        <target name="parent">
            <echo message="Current working directory in parent: ${basedir}" />
            <ant antfile="../build.xml" dir="subdir" target="subDirTarget"
                inheritAll="true" inheritRefs="true" /> 
        </target>
    
        <target name="subDirTarget">
            <echo message="Current working directory in subDirTarget: ${basedir}" />
        </target>
    </project>
    

    This works - output:

    Buildfile: C:\Sandbox\parent\build.xml
    parent:
         [echo] Current working directory in parent: E:\Sandbox\parent
    subDirTarget:
         [echo] Current working directory in subDirTarget: E:\Sandbox\parent\subdir
    BUILD SUCCESSFUL
    Total time: 1 second
    

    I also tried using Ant-Contrib's <var> task to change basedir, but that seemed to have no effect.