0
votes

Let me first provide the background of the problem I'm facing.

I have a directory structure as below.

c:\myDirectory
c:\myDirectory\Project1
c:\myDirectory\Scripts

Under the c:\myDirectory\Scripts there is a script that download the source code (from svn) and creates the c:\myDirectory\Project1 directory.

I have another ant scripts ( c:\myDirectory\Scripts**compile-source.xml ) that compiles the Project1 from an ant script build.xml that is downloaded to c:\myDirectory\Project1

Snippet for c:\myDirectory\Scripts\compile-source.xml

<project name="compile" default="buildAll" basedir=".">
    <property file=".\build.properties">
    </property>
    .......
    <import file="${project.home.path}/${project.name}/build.xml"/>
     <target name="buildAll">
      <antcall target="jar-pack"/>
    </target>
</project>

Snippet for c:\myDirectory\Project1\build.xml.

<project name="CommonFeatures" default="jar-pack" basedir=".">
        <description>
            A build file for the Common Features project
        </description>
        ....
    </project>

Note that the basedir for the project is set as "." for both the above ant scripts.

When I execute the script c:\myDirectory\Scripts\compile-source.xml from the c:\myDirectory\Scripts directory the target "jar-pack" present in the c:\myDirectory\Project1\build.xml gets executed.

However, the problem is that basedir attribude in build.xml ( basedir="." ) is the current working directory and in this case its c:\myDirectory\Scripts. Hence the script build.xml errors out since the basedir for build.xml is expected to be c:\myDirectory\Project1. The build.xml script would have worked, if basedir="." were set to "c:\myDirectory\Project1", but unfortunately build.xml file comes from the source code that is downloaded and I'm unable to edit.

So here's my question, Is it possible to do any of the following.

  1. Override the value of the attribude basedir="." in build.xml when the is done in c:\myDirectory\Scripts\compile-source.xml ?

  2. Is it possible to change the basedir in build.xml by any other mechanism so that the script c:\myDirectory\Project1\build.xml is executed under directory c:\myDirectory\Project1 ?

  3. Any other way to resolve this issue?

Any help from Ant experts to overcome this issue is highly appreciated.

3

3 Answers

0
votes

You can specifically reference the location of your build file, which is described in this stack overflow thread. This would allow you to get and use the directory your build file resides in as a reference point.

0
votes

You can update basedir using subant task. Check this answer

Create the following build.xml file (assuming it is in Z:/any/folder):

<?xml version="1.0" encoding="UTF-8"?>
<project name="project">
    <target name="mytarget">
        <subant target="debug">
            <property name="basedir" value="X:/any/dir/with/project"/>
            <fileset dir="Y:/any/folder/with" includes="build.xml"/>
        </subant>
    </target>
</project>

The you can execute ant mytarget from Z:/any/folder

0
votes

For your case the usage of the subant or ant tasks may be better suited, but nevertheless...

You can (but you should know/consider the side-effects!) extend ant with the common ant-contrib task definitions and use the var task which is able to override properties. Make sure to use the latest version (> 1.0b3).

<!-- adjust to your path and include it somewhere at the beginning of your project file -->
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="lib/ant-contrib-1.0b3.jar" />

<!-- works e.g. for basedir = /foo/bar to update it to /foo/bar/.. ~ /foo -->
<var name="basedir" value="${basedir}/.." />

update: but one has to be careful, because this does not change . (current working directory) (so <property name="x" location="tmp" /> would be relative to . and not to basedir anymore ; update: setting basedir outside of ant or via <project basedir= also sets . to basedir!). Here is some test target proving the effect on both:

<target name="tst.dummy.basedir-override">
    <!-- example output:
        tst.dummy.basedir-override:
             [echo] basedir before: basedir=D:\tst,  '.'=D:\tst\.
             [echo] updating it via 'var' to '..'
             [echo] basedir now:  basedir=D:\tst/..,  '.'=D:\tst\.
    -->
    <property name="cur" location="." /> <!-- makes the relative path absolute -->
    <echo message="basedir before: basedir=${basedir},  '.'=${cur}" />

    <echo message="updating it via 'var' to '..'" />
    <var name="basedir" value="${basedir}/.." />

    <property name="cur2" location="." /> <!-- makes the relative path absolute -->
    <echo message="basedir now:  basedir=${basedir},  '.'=${cur2}" />
</target>