Use Eclipse classpath variable to replace absolute

2020-08-22 13:56发布

问题:

I would like to use an Eclipse classpath variable to resolve the attached source JAR file for a library in my classpath. This is the contents of my current ".classpath" file in Elcipse (Indigo):

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

When I now add a source JAR file for "spring-ws-1.5.8-all.jar", the ".classpath" files contents is:

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

As you can see, Eclipse added the "sourcepath" attribute to the "classpathentry" element with an absolute path.

Now my idea is to replace the absolute path with the classpath variable "SOURCE_PATH", which is correctly set to "D:/dev/sources".

(Please, don't ask why we have this setup or suggest we must change it; it is an old project and we are unfortunately not able/allowed to change the build structure).

I tried

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

as well as

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

but both variants do not work.

I seems, that I either use the wrong syntax or I don't understand the concept of classpath variables. Maybe the classpath variable can't be used for the "sourcepath" attribute?

Thanks a lot for your help!

回答1:

I might not understand your question completely but it sounds like you added your jar file with an absolute path to begin with. Instead add it with a classpath variable.

Use the Project properties, select Java Build Path option. Then click on the Libraries tab. Then Add Variable.... If you haven't created your SOURCE_PATH classpath variable yet, you can do it from here by clicking the Configure Variables... button (just a shortcut to the proper workspace preference setting). Once configured you should see your SOURCE_PATH class path variable in the box at the top. You can click on it extend to Extend... it to a subfolder or a jar file.



回答2:

Using Tod's answer above, if you have lots of jars in lots of projects, you may find it easier to edit the .classpath files directly. Using a Java Build Path variable in .classpath changes the reference from:

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

to:

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


回答3:

You could use a symbolic link. Windows also supports paths starting with /. This is drive dependent. Try cd / on the Command Prompt to verify.