java Ant 中的 <pathelement> 属性“路径”和“位置”之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7195717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:01:44  来源:igfitidea点击:

What is the difference between the <pathelement> attributes 'path' and 'location' in Ant?

javaant

提问by Feanor

I was running Selenium unit tests in TestNG with the Ant Java task like so:

我在 TestNG 中使用 Ant Java 任务运行 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.classpathis a pathlike structure that included <pathelement path="${basedir}/target/classes/" />, which I thought was needed to let TestNG know which classes to run.

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

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

However, I kept seeing in the log that TestNG found 0 applicable classes.

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

I eventually got some help from a colleague and it appears this was the key change:

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

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

This also pulls in the test classes correctly:

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

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

What is the difference between the pathand locationattributes? I've looked at Writing a Simple Buildfile(specifically the Path-like Structures section), but in that manual it looks to me like locationis more specific than path. That doesn't appear to be the case empirically, but I can't quite figure out why.

pathlocation属性和有什么不一样?我看过编写一个简单的构建文件(特别是类似路径的结构部分),但在该手册中,在我看来它locationpath. 从经验上看,情况似乎并非如此,但我无法弄清楚原因。

采纳答案by Paul Rubel

It looks like the difference between path and location is many entries vs one. A location is a file or directory, a path can be a list.

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

From the 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.

location 属性指定相对于项目基本目录(或绝对文件名)的单个文件或目录,而 path 属性接受以冒号或分号分隔的位置列表。path 属性旨在与预定义的路径一起使用 - 在任何其他情况下,应首选具有位置属性的多个元素。

Note that the JVM used by ant has just about no relation to the JVM used by the java task. By default the environment of ant isn't the same as that of things started with the java task via ant. This is actually helpful when you want to use a different JVM from the one ant wants to use and makes things explicit, helping avoid surprises later on.

请注意,ant 使用的 JVM 与 java 任务使用的 JVM 几乎没有关系。默认情况下,ant 的环境与通过 ant 以 java 任务启动的事物的环境不同。当您想使用与一只蚂蚁想要使用的 JVM 不同的 JVM 时,这实际上很有帮助,并使事情变得明确,有助于避免以后出现意外。

Check out the docs for the java task, particularly clonevm

查看java任务的文档,特别是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

clonevm:如果设置为true,那么分叉的Java虚拟机的所有系统属性和引导类路径将与运行Ant的Java VM相同。默认值为“false”(如果 fork 被禁用,则忽略)。自蚂蚁 1.7