Eclipse Java project build path using variables

eclipse

I am developing an Eclipse Java project and would like to use "variables" to point to required libraries (.jar files).

Setup:

  • Machine 1: Eclipse Juno, OS X, own laptop, libs under /mylibs
  • Machine 2: Eclipse Juno, Linux, shared machine, libs under /sharedlibs/some/complex/path

Source control: Mercurial

Example of libs I use:

  • "${libs_base}/junit.jar"
  • "${libs_base}/guava.jar"
  • "${libs_base}/logging/slf4j.jar"

How can I configure the project Build Path to use the same "variable" for the base directory for libraries, pointing to different locations on different machines, but still check in my project description to source control so that when I add a new library, provided I specify its location relative to the libs base, it is found on both machines?

I have found the help file but could use an example, maybe even a screenshot please?

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Fconcepts%2Fcpathvars.htm

for instance, do I copy this model:

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Fconcepts%2Fconcepts-exttools.htm

inside "Java > Build Path > Classpath Variables" or elsewhere?

I have done a fair bit of "blind trial and error" already.. like stitching forms like "${env:VAR}" in there..

Oh, and I suppose I should mention that I don't know how to set these Eclipse variables based on environment variables considering that my systems' environment variables are set by Bash's .profile and I doubt Eclipse runs a shell to source that before…

/org.eclipse.jdt.core/org.eclipse.jdt.core.classpathVariable.my_libraries=$MY_SYS_ENV_VAR_LIBS

Of course, I use "Configure Build Path > Libraries > Add Variable" and then Extend from there, but still… an example would be worth a 1000 words.. (could it be that defining an Eclipse variable from which to Extend actual .jar references for inclusion in Build Paths just can't be based on a system environment variable, full stop?!)

PS: please don't ask me to just symlink things around, I am not a sysadmin on the shared machine..

Best Answer

From what I understand, extending a path variable would work perfectly for your case as you can define different base paths for each machine and reference libraries on subpaths based on that path. However if you are looking specifically to use environment variables you can try adding them to the build path this way:

  1. Edit your launch configuration.
  2. Go to the Classpath tab.
  3. Click on User Entries and click the Advanced button.
  4. To add a library based on a system environment variable, select Add Variable String, and in the box type: ${env_var:libs_base}/logging/slf4j.jar. In this case libs_base is your environment variable and logging/slf4j.jar is the path to the jar in the subdirectory.
Related Topic