简体   繁体   English

我可以在Eclipse中使用变量来引用Java源路径

[英]Can I refer to a Java source path using a variable in Eclipse

Background 背景

I'm using an Eclipse 4.2 (Juno) release to build a plugin for a Java application. 我正在使用Eclipse 4.2(Juno)版本为Java应用程序构建插件。 The source code and classes for my plugin all reside within the project workspace. 我的插件的源代码和类都驻留在项目工作区中。 The application jar and its source code are, for various reasons maintained outside of the eclipse workspace and the application jar is produced by another build mechanism. 由于各种原因,应用程序jar及其源代码在eclipse工作空间之外维护,应用程序jar由另一个构建机制生成。

This isn't a problem as I have referenced the application jar file in my project using drag and drop and the ' Link to files ' option specifying it ' Create link locations relative to : MYDEV ', where MYDEV is a Linked Resource Variable I have created for each Eclipse installation. 这不是问题,因为我使用拖放操作在项目中引用了应用程序jar文件,并且“ 链接到文件 ”选项指定了它“ 创建相对于MYDEV链接位置 ”,其中MYDEV是一个链接资源变量我有为每个Eclipse安装创建。 This lets me use the same eclipse project on multiple machines where the path to the application jar varies, but is always the same relative to MYDEV. 这让我在多个机器上使用相同的eclipse项目,其中应用程序jar的路径变化,但相对于MYDEV总是相同的。

However, I cannot find a way to associate the application source code with this jar unless I use an absolute path. 但是,除非我使用绝对路径,否则我找不到将应用程序源代码与此jar关联的方法。 I only want access to the application source for debugging purposes. 我只想访问应用程序源以进行调试。

Question

In Eclipse, how do I attach java source to a referenced jar in a way that allows a project to be used on multiple installations where the referenced source code has been relocated? 在Eclipse中,如何以允许项目在已重定位引用源代码的多个安装中使用的方式将java源附加到引用的jar?

If the source is outside the proyect, then there's no way to access it without an absolute path. 如果源在proyect之外,那么没有绝对路径就无法访问它。

The best solution is using a symlink inside the eclipse project directory to the actual source folder; 最好的解决方案是在eclipse项目目录中使用符号链接到实际的源文件夹; eclipse won't notice the source is outside the project directory and everything will work fine, without having to relocate the source. eclipse不会注意到源是在项目目录之外,一切都会正常工作,而不必重新定位源。

This is the only way I was able to do what you are suggesting (do not know if there are better ways). 这是我能够做你所建议的唯一方式(不知道是否有更好的方法)。

Package a JAR file that contains the source code of the JAR file. 打包包含JAR文件源代码的JAR文件。 Place it inside your project in a folder (you do not need to add it to your class path) 将它放在项目中的文件夹中(您不需要将它添加到类路径中)

In the eclipse project right click on the JAR file and go to "properties" and then to "Java Source Attachment" From there select "Workspace" button and pick the jar file with the source code we added from the above step. 在eclipse项目中右键单击JAR文件并转到“属性”然后转到“Java源附件”从那里选择“工作区”按钮并选择带有我们从上面步骤添加的源代码的jar文件。

So the source attachment path will always be relative to the project. 因此,源附件路径将始终相对于项目。 That way if you share the project via SVN, GIT or whatever, the source will always be available and it'll work if the user is running eclipse on Windows or Linux. 这样,如果您通过SVN,GIT或其他任何方式共享项目,源代码将始终可用,如果用户在Windows或Linux上运行eclipse,它将起作用。

I've had a similar problem to you where absolute paths were a problem for people using different OS and not even using mapped drives via Samba helped that much. 我遇到了类似的问题,对于使用不同操作系统的人来说,绝对路径是一个问题,甚至连Samba使用的映射驱动器都没有帮助。

Hope this helps. 希望这可以帮助。

The way I have solved this problem is to use a use defined library in eclipse. 我解决这个问题的方法是在eclipse中使用一个use use library。 Here the steps that you can use to solver this problem. 这里是您可以用来解决此问题的步骤。

  1. Create a simple project in eclipse in the directory that contains the jars and the source code files. 在包含jar和源代码文件的目录中的eclipse中创建一个简单的项目。

  2. Define a java user library add the jars to the user library and for each jar in the library specify the location of the source. 定义java用户库将jar添加到用户库,并为库中的每个jar指定源的位置。 If the source files are in a project that is in the workspace then the path will be relative to the workspace folder. 如果源文件位于工作空间中的项目中,则路径将相对于工作空间文件夹。

  3. Export the user library as an .xml file 将用户库导出为.xml文件

When another use wants to setup an eclipse workspace with the same setting as yours they will do two things. 当另一个用户想要设置一个与你的设置相同的eclipse工作区时,他们会做两件事。

  1. Import the project that contains the jars and the sources into their workspace. 将包含jar和源的项目导入其工作空间。

  2. Import the user library into their workpsace. 将用户库导入其工作区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM