在 bootclasspath 中使用 ant 路径作为 属性

Use ant path as property in bootclasspath

在 ant 脚本中,我 path 设置:

<path id="classpath.id">
  <pathelement path="somepath_1" />
  ...
  <pathelement path="somepath_n" />
</path>

以便我在 java 任务中使用它:

<java ... classpathref="classpath.id">
 ...
</java>

如何使用 classpath.idjava ant 任务中设置 bootclasspath 类似于:

<java ...>
  <jvmarg value="-Xbootclasspath/a:${myjar.jar}${path.separator}${classpath.id}"/>
</java>

${classpath.id} 此时蚂蚁还不知道。

有一种特殊的语法可用于获取 Ant 引用 ID 引用的内容的值。使用 ${ant.refid:classpath.id} 而不是 ${classpath.id}.

参考Getting the value of a Reference with ${ant.refid:}

为了完整起见,这些是可能的解决方案:

使用 ${ant.refid:} 前缀

这是最干净的解决方案,所有功劳都归功于 martin clayton for 。只需使用

<java ...>
  <jvmarg value="Xbootclasspath/a:${myjar.jar}${path.separator}${ant.refid:classpath.id}"/>
</java>

创建一个新的属性

<property name="classpath.property" refid="classpath.id"/>
<java ...>
  <jvmarg value="Xbootclasspath/a:${myjar.jar}${path.separator}${classpath.property}"/>
</java>

使用引导类路径和引导类路径引用

虽然 bootclasspathref 仅适用于 javac, bootclasspath can be nested in java:

<java fork="true" ...>
  <bootclasspath>
    <path refid="classpath.id"/>
    <pathelement path="${myjar.jar}" />
  </bootclasspath>
</java>

不过,此解决方案存在一些复杂性。

  • <bootclasspath> 替换了实际的 bootclasspath,从中删除了 java 的 jars,目前 ant 中没有 <bootclasspath/a> 嵌套标记。
  • 在 java SE 9 中,-Xboothclasspath 属性 不再可用。只有-Xboothclasspath/a,所以<bootclasspath>在那里不起作用。