tags:

views:

459

answers:

3

I have setup an ant script as eclipse builder to automatically run all my tests, like below:

<project name="auto-test" default="test">
    <property name="tst-dir" location="C:\STAF\services\custom\TopCoder\bin" />
    <path id="classpath.base" />
    <path id="classpath.test">
        <pathelement location="D:\eclipse\eclipse\plugins\org.junit4_4.3.1\junit.jar" />
        <pathelement location="${tst-dir}" />
        <path refid="classpath.base" />

    </path>
    <target name="test" description="Run the tests">
        <junit>
            <classpath refid="classpath.test" />
            <formatter type="brief" usefile="false" />
            <test name="testDataGenerator.test.AllTests" />
        </junit>
    </target>
</project>

It was all good before I changed a test fixture file from absolute path to relative path:

SAXReader reader = new SAXReader();
Document document = reader.read(new File(".").getCanonicalPath()+"\\conf\\TestData.xml");

The ant task now try to open D:\eclipse\eclipse\conf\TestData.xml, instead of C:\STAF\services\custom\TopCoder\conf\TestData.xml, I've also try to run AllTests manually from Eclipse and it's all good.

Has anyone met similar problem before?

Thanks in advance.

PS. ANT_HOME=D:\eclipse\eclipse\plugins\org.apache.ant_1.7.0.v200706080842

Follow up: I tried to run the ant script from command line, and find below:

  1. C:\STAF\services\custom\TopCoder>ant -f c:\STAF\services\custom\TopCoder\task\build.xml, the ant script works correctly.

  2. C:>ant -f c:\STAF\services\custom\TopCoder\task\build.xml, the script will claim: [junit] C:\conf\TestData.xml (The system cannot find the path specified)

I've also checked eclipse builder setting, there seems nothing to change the path to D:\eclipse\eclipse.

+1  A: 

Java resolves relative paths against the current user directory, which is typically the directory from where the java program was invoked.

One way to overcome this issue is to define an environmental variable for your base path. Then, you could easily use "relative paths" (meaning, create absolute paths by concatenating the base path and the relative path).

kgiannakakis
A: 

Here is the solution I find:

Just as kgiannakakis mentioned, Ant also start executing its task from the location it was invoked, so we just need to change the working directory setting of our custom eclipse builder.

  1. In the JRE tab, choose "Execution Environment".

  2. Change the Working directory to your current workspace.

alt text

eric2323223
A: 

Looks like I've missed the karma but anyway...

We do this:-

Build.xml

<project name="whatever">
  <property file="build.${env.COMPUTERNAME}.properties"/>
  <property file="build.properties"/>

build.properties

project.root=..
build.file.dir=${project.root}/buildfiles
deploy.dir=${project.root}/deploy

which of course you can override by creating your OWN build.computername.properties to allow for developer path differences etc

mcottle