How to use relative paths for projects added to an Eclipse workspace?

让人想犯罪 __ 提交于 2019-12-17 21:59:13

问题


I am working on some Eclipse projects that are stored on a USB key. I have added them to a workspace whose root folder is also on the USB key, but the projects are not located directly into this directory. As a result their absolute paths are written in the .projects folder of the workspace.

This raises problems when I use the key on several computers, because the drive letter assigned to the USB key is not always the same (and I do not want to assign a specific letter to it on all computers). Eclipse cannot open my projects when the drive letter differs from that of the USB key at the time I added the project to the workspace.

Is there a solution to this problem ? Specifically, is there a way to make Eclipse add projects to a workspace by using their relative path from the workspace folder ?


回答1:


The short answer is that you cannot do it without hacking your workspace metadata (under [workspace.dir]/.metadata). Eclipse workspaces aren't designed to be portable and even though you are not physically moving your workspace, you are in effect moving it by changing drive letters.

I'd recommend trying to find a way to have your projects under workspace.dir. Not guaranteed that you will not have problems that way, but may improve your odds. Depending on what features of Eclipse you use, you may very well end up with other types of absolute paths in your workspace metadata.




回答2:


Its really not that hard. You just have to assign a new variable that points to WORKSPACE/..;. For example, you can use the Forge Minecraft modder pack - it has a workspace that is immediately ready no matter what machine downloads it, where you place it or even if you move it.

Download this: http://files.minecraftforge.net/minecraftforge/minecraftforge-src-1.6.2-9.10.0.789.zip

And then unzip it and run install.bat/cmd. Then check the contents of forge/mcp/eclipse/Minecraft/{.project,.workspace} to see some examples.

I've got my workspace working on github like this.




回答3:


In my experience it will break at some time if you try to "hack" the metadata of eclipse.

Consider the substcommand in windows (As you mention drive letter I assume windows)

create a bat file containing

subst N: .

Running this will mount the current dir as N: So You can place this on the usb drive, and run that prior to running eclipse, then You will always find the same content on drive N:

To remove the mount do

subst /D N:

You might consider making a bat file that mount N:, start eclipse and after eclipse exit unmount N: something like

startMyProject.bat:

subst N: .
N:/eclipse <yadayada options>
subst /D N:



回答4:


This question is kinda old, but there's an entirely different approach: You can create a Junction from the Folder where Eclipse expects the Project, for example E:\foo\workspace\myproject and link it to where the project lays, for example E:\bar\myproject.

A Junction essentially means that you can access the same folder through 2 different paths, thus you don't need to worry with the weird relative path limitations of Eclipse. To create it easily and fast, I recommend you to get Link Shell Extension




回答5:


I haven't tried this myself, but this might work. Try:
Add your source folder as linked resource & remove previous definition: http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.user/tasks/tasks-45.htm

Close eclipse and find your project folder in your workspace (ex: C:\Documents and Settings\username\workspace\yourproject\) Replace absolute paths in files .project & .classpath with relative paths.



来源:https://stackoverflow.com/questions/5178725/how-to-use-relative-paths-for-projects-added-to-an-eclipse-workspace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!