如何为添加到 Eclipse 工作区的项目使用相对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5178725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use relative paths for projects added to an Eclipse workspace?
提问by Dunaril
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.
我正在处理一些存储在 USB 密钥上的 Eclipse 项目。我已将它们添加到工作区,其根文件夹也位于 USB 密钥上,但项目并未直接位于此目录中。因此,它们的绝对路径被写入工作区的 .projects 文件夹中。
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.
当我在多台计算机上使用该密钥时,这会引发问题,因为分配给 USB 密钥的驱动器号并不总是相同(并且我不想在所有计算机上为其分配特定的字母)。当驱动器号与我将项目添加到工作区时 USB 密钥的驱动器号不同时,Eclipse 无法打开我的项目。
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 ?
这个问题有解决方案吗?具体来说,有没有办法让 Eclipse 使用工作区文件夹中的相对路径将项目添加到工作区?
采纳答案by Konstantin Komissarchik
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.
简短的回答是,如果不破解您的工作区元数据(在 下[workspace.dir]/.metadata
),您就无法做到这一点。Eclipse 工作区并非设计为可移植的,即使您没有实际移动工作区,实际上也是通过更改驱动器号来移动它。
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.
我建议尝试找到一种方法将您的项目置于workspace.dir
. 不能保证您不会遇到问题,但可能会提高您的几率。根据您使用的 Eclipse 的哪些功能,您的工作区元数据中很可能会出现其他类型的绝对路径。
回答by hanetzer
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.
它真的没有那么难。您只需要分配一个指向 的新变量WORKSPACE/..;
。例如,您可以使用 Forge Minecraft modder 包 - 它有一个工作区,无论什么机器下载它,你把它放在哪里,甚至你移动它,它都可以立即准备好。
Download this: http://files.minecraftforge.net/minecraftforge/minecraftforge-src-1.6.2-9.10.0.789.zip
下载这个: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.
然后解压并运行install.bat/cmd。然后检查forge/mcp/eclipse/Minecraft/{.project,.workspace} 的内容以查看一些示例。
I've got my workspace working on github like this.
我的工作区像这样在 github 上工作。
回答by Otzen
In my experience it will break at some time if you try to "hack" the metadata of eclipse.
根据我的经验,如果您尝试“破解”eclipse 的元数据,它会在某个时候中断。
Consider the subst
command in windows (As you mention drive letter I assume windows)
考虑subst
Windows 中的命令(正如你提到的驱动器号,我假设是 Windows)
create a bat file containing
创建一个 bat 文件,其中包含
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:
运行它会将当前目录挂载为 N: 所以你可以把它放在 USB 驱动器上,并在运行 eclipse 之前运行它,然后你总是会在驱动器 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
您可能会考虑制作一个挂载 N: 的 bat 文件,启动 eclipse 并在 eclipse 退出后卸载 N: 之类的
startMyProject.bat:
startMyProject.bat:
subst N: .
N:/eclipse <yadayada options>
subst /D N:
回答by Xerus
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
.
这个问题有点老了,但有一种完全不同的方法:例如,您可以从 Eclipse 期望项目的文件夹创建一个连接点,E:\foo\workspace\myproject
并将其链接到项目所在的位置,例如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
Junction 本质上意味着您可以通过 2 个不同的路径访问同一个文件夹,因此您不必担心 Eclipse 奇怪的相对路径限制。为了轻松快速地创建它,我建议您获取Link Shell Extension
回答by Caner
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
我自己没有尝试过,但这可能会奏效。尝试:
将您的源文件夹添加为链接资源并删除以前的定义: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.
关闭Eclipse和找到你的项目文件夹在您的工作(例如:C:\Documents and Settings\username\workspace\yourproject\
)的文件替换绝对路径.project
和.classpath
相对路径。