有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Ant中,<pathelement>属性“path”和“location”之间有什么区别?

我使用Ant Java任务在TestNG中运行Selenium单元测试,如下所示:

<java classpathref="runtime.classpath"
    classname="org.testng.TestNG"
    failonerror="false">
    <arg value="-d" />
    <arg value="${grid.location}/target/reports" />
    <arg value="${lib.location}/testng.xml"/>   
</java>

runtime.classpath是一个包含<pathelement path="${basedir}/target/classes/" />的类似路径的结构,我认为这是让TestNG知道要运行哪些类所必需的

<path id="runtime.classpath">
        ...
        <!-- Target classes -->
        <pathelement path="${basedir}/target/classes/" />
</path>

但是,我一直在日志中看到TestNG找到了0个适用的类

我最终从一位同事那里得到了一些帮助,这似乎是关键的改变:

<path id="runtime.classpath">
        ...
        <!-- path attribute changed to location -->
        <pathelement location="${basedir}/target/classes/" />
</path>

这也会正确地引入测试类:

   <java classpathref="runtime.classpath"
       classname="org.testng.TestNG"
       failonerror="false">
       <arg value="-d" />
       <arg value="${grid.location}/target/reports" />
       <arg value="${lib.location}/testng.xml"/>
       <classpath>
           <pathelement location="${basedir}/target/classes/" />
       </classpath> 
   </java>

pathlocation属性之间有什么区别?我已经看过了Writing a Simple Buildfile(特别是类似路径的结构部分),但在那本手册中,我觉得locationpath更具体。从经验上看,情况似乎并非如此,但我不太明白为什么


共 (1) 个答案

  1. # 1 楼答案

    看起来路径和位置之间的区别是多个条目和一个条目。位置是文件或目录,路径可以是列表

    manual

    The location attribute specifies a single file or directory relative to the project's base directory (or an absolute filename), while the path attribute accepts colon- or semicolon-separated lists of locations. The path attribute is intended to be used with predefined paths - in any other case, multiple elements with location attributes should be preferred.

    注意,ant使用的JVM与java任务使用的JVM几乎没有关系。默认情况下,ant的环境与通过ant启动java任务的环境不同。当您想要使用与ant想要使用的JVM不同的JVM时,这实际上是很有用的,可以使事情变得明确,从而避免以后出现意外

    查看java task的文档,尤其是clonevm

    clonevm: If set to true, then all system properties and the bootclasspath of the forked Java Virtual Machine will be the same as those of the Java VM running Ant. Default is "false" (ignored if fork is disabled). since Ant 1.7