ANT junit任务中的相对路径问题

发布于 2024-08-22 18:15:12 字数 1701 浏览 2 评论 0原文

我已经设置了一个 ant 脚本作为 eclipse 构建器来自动运行我的所有测试,如下所示:

<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>

在我将测试夹具文件从绝对路径更改为相对路径之前一切都很好:

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

ant 任务现在尝试打开 D:\ eclipse\eclipse\conf\TestData.xml,而不是 C:\STAF\services\custom\TopCoder\conf\TestData.xml,我还尝试从Eclipse 一切都很好。

以前有人遇到过类似的问题吗?

提前致谢。

附言。 ANT_HOME=D:\eclipse\eclipse\plugins\org.apache.ant_1.7.0.v200706080842

跟进: 我尝试从命令行运行 ant 脚本,并找到以下内容:

  1. C:\STAF\services\custom\TopCoder>ant -fc:\STAF\services\custom\TopCoder\task\build.xml ,ant 脚本工作正常。

  2. C:>ant -fc:\STAF\services\custom\TopCoder\task\build.xml,脚本将声明:[junit] C:\conf\TestData.xml (系统找不到指定的路径)

我还检查了 eclipse builder 设置,似乎没有什么可以更改 D:\eclipse\eclipse 的路径。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2024-08-29 18:15:12

Java 根据当前用户目录解析相对路径,该目录通常是调用 java 程序的目录。

解决此问题的一种方法是为基本路径定义环境变量。然后,您可以轻松使用“相对路径”(意思是,通过连接基本路径和相对路径来创建绝对路径)。

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).

梦里的微风 2024-08-29 18:15:12

这是我找到的解决方案:

正如 kgiannakakis 提到的,Ant 也从调用它的位置开始执行其任务,因此我们只需要更改自定义 Eclipse 构建器的工作目录设置。

  1. 在 JRE 选项卡中,选择“执行环境”。

  2. 将工作目录更改为当前工作空间。

替代文字

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

迷雾森÷林ヴ 2024-08-29 18:15:12

看起来我错过了因果报应,但无论如何......

我们这样做:-

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

当然,您可以通过创建自己的 build.computername.properties 来覆盖它,以允许开发人员路径差异等

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文