使用Eclipse classpath变量来替换绝对的“sourcepath”路径?

时间:2013-04-28 21:56:02

标签: java eclipse build classpath

我想使用Eclipse类路径变量来解析类路径中库的附加源JAR文件。这是我在Elcipse(Indigo)中当前“.classpath”文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src"/>
  <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry exported="true" kind="lib" path="lib/ApacheCommons/commons-logging.jar"/>
  <classpathentry exported="true" kind="lib" path="lib/Spring-WS/spring-ws-1.5.8-all.jar"/>

  <!-- [other entries] -->

  <classpathentry kind="output" path="bin"/>
</classpath>

当我现在为“spring-ws-1.5.8-all.jar”添加源JAR文件时,“。classpath”文件内容为:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src"/>
  <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry exported="true" kind="lib" path="lib/ApacheCommons/commons-logging.jar"/>
  <classpathentry exported="true" kind="lib" path="lib/Spring-WS/spring-ws-1.5.8-all.jar"
    sourcepath="D:/dev/sources/spring-ws-1.5.8-sources.jar"/>

  <!-- [other entries] -->

  <classpathentry kind="output" path="bin"/>
</classpath>

如您所见,Eclipse将“sourcepath”属性添加到具有绝对路径的“classpathentry”元素中。

现在我的想法是用类路径变量“SOURCE_PATH”替换绝对路径,该变量被正确设置为“D:/ dev / sources”。

(请不要问为什么我们有这个设置或建议我们必须更改它;这是一个旧项目,遗憾的是我们无法/允许更改构建结构。)

我试过

sourcepath="SOURCE_PATH/spring-ws-1.5.8-sources.jar"

以及

sourcepath="${SOURCE_PATH}/spring-ws-1.5.8-sources.jar"

但两种变体都不起作用。

我似乎,我要么使用错误的语法,要么我不理解类路径变量的概念。也许classpath变量不能用于“sourcepath”属性?

非常感谢你的帮助!

3 个答案:

答案 0 :(得分:5)

我可能完全不了解你的问题,但听起来你添加了一个带有绝对路径的jar文件。而是使用classpath变量添加它。

使用项目属性,选择Java Build Path选项。然后单击Libraries选项卡。然后Add Variable....如果您还没有创建SOURCE_PATH类路径变量,可以通过单击Configure Variables...按钮(只是正确工作区首选项设置的快捷方式)从此处执行此操作。配置完成后,您应该在顶部的框中看到您的SOURCE_PATH类路径变量。您可以单击它将Extend...扩展到子文件夹或jar文件。

答案 1 :(得分:1)

使用Tod的上述答案,如果你在很多项目中有很多jar,你可能会发现直接编辑.classpath文件更容易。在.classpath中使用Java Build Path变量会更改引用:

<classpathentry kind="lib" path="/full/pathTo/Jar.jar"/>

为:

<classpathentry kind="var" path="NEW_PATH_VARIABLE/Jar.jar"/>

答案 2 :(得分:0)

您可以使用symbolic link。 Windows还支持以/开头的路径。这取决于驱动器。在命令提示符上尝试cd /进行验证。