如何在 Eclipse Helios 中引用另一个项目中的库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4156617/
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 can I reference libraries in another project within Eclipse Helios?
提问by jwir3
We have a problem at the place that I work with referencing multiple projects in our code. Basically, we have a product, which I'll call Leviathan. This project has quite a number of other versions, each of which we maintain as separate projects in Eclipse. As developers, we typically have multiple projects (multiple versions) open in Eclipse simultaneously, as we get helpline calls about older versions (as well as developing simultaneously multiple versions).
我们在我使用代码中引用多个项目的地方遇到了问题。基本上,我们有一个产品,我称之为利维坦。这个项目有相当多的其他版本,我们在 Eclipse 中将每个版本作为单独的项目进行维护。作为开发人员,我们通常会同时在 Eclipse 中打开多个项目(多个版本),因为我们会接到有关旧版本(以及同时开发多个版本)的帮助热线电话。
We also have test code, which is in a different project for each distribution of Leviathan. I name my projects Leviathan_<branch name>
. So, for example, in my Eclipse workspace, I might have projects like:
我们还有测试代码,它在每个 Leviathan 发行版的不同项目中。我命名我的项目Leviathan_<branch name>
。因此,例如,在我的 Eclipse 工作区中,我可能有如下项目:
Leviathan_scott\ (my branch) Leviathan_9.2\ Leviathan_9.3\ Leviathan_10.0\ Leviathan_10.1\ Test_scott\ Test_10.0
My branch is a copy of our trunk, so we can consider that the most active development line.
我的分支是我们主干的副本,所以我们可以认为是最活跃的开发线。
The problem is that we reference some libraries in Leviathan\lib in our test code. Each developer may name their projects slightly differently. So, in developing the test project's .classpath, we reference the project Leviathan\ (no additions). The classpath for this project (which is checked into our source control system) might look like:
问题是我们在测试代码中引用了 Leviathan\lib 中的一些库。每个开发人员可能会稍微不同地命名他们的项目。因此,在开发测试项目的 .classpath 时,我们引用了项目 Leviathan\(没有添加)。此项目的类路径(已签入我们的源代码控制系统)可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/*.MySCMServerInfo" kind="src" path="src"/>
<classpathentry kind="lib" path="lib/abbot-1.0.2/abbot.jar"/>
<classpathentry kind="lib" path="lib/abbot-1.0.2/costello.jar"/>
<classpathentry kind="lib" path="lib/dbunit-2.4.8/dbunit-2.4.8.jar"/>
<classpathentry kind="lib" path="lib/easymock-3.0/easymock-3.0.jar" sourcepath="lib/easymock-3.0/easymock-3.0-sources.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/Test/lib/easymock-3.0/easymock-3.0-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/objenesis-1.2/objenesis-1.2.jar"/>
<classpathentry kind="lib" path="lib/privilegedAccessor-1.0.2/privilegedAccessor_1.0.2.jar"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-core/unitils-core-3.1.jar" sourcepath="lib/unitils-3.1/unitils-core/src"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-database/unitils-database-3.1.jar"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-dbmaintainer/unitils-dbmaintainer-3.1.jar"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-dbunit/unitils-dbunit-3.1.jar" sourcepath="lib/unitils-3.1/unitils-dbunit/src"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-inject/unitils-inject-3.1.jar"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-mock/unitils-mock-3.1.jar" sourcepath="lib/unitils-3.1/unitils-mock/src"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-orm/unitils-orm-3.1.jar"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-spring/unitils-spring-3.1.jar" sourcepath="lib/unitils-3.1/unitils-spring/src"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-testng/unitils-testng-3.1.jar" sourcepath="lib/unitils-3.1/unitils-testng/src"/>
<classpathentry kind="lib" path="data"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/unitils-3.1/unitils-core/lib/ognl-2.6.9.jar"/>
<classpathentry kind="lib" path="lib/testng-5.14.1/testng-5.14.1.jar" sourcepath="lib/testng-5.14.1/testng-5.14.1-src.zip"/>
<classpathentry combineaccessrules="false" kind="src" path="/Leviathan"/>
<classpathentry kind="lib" path="/Leviathan/lib/slf4j-api-1.6.1.jar"/>
<classpathentry kind="lib" path="/Leviathan/lib/hibernate3.jar" sourcepath="/Leviathan/lib/src/hibernate-3.6.0-src.zip"/>
<classpathentry kind="output" path="classes"/>
</classpath>
When a developer loads the Test project into his/her individual Eclipse workspace, he/she would need to link the project to the original Leviathan project by going to Preference->Java Build Path->Projects and add the Leviathan_scott project.
当开发人员将测试项目加载到他/她的个人 Eclipse 工作区中时,他/她需要通过转至 Preference->Java Build Path->Projects 并添加 Leviathan_scott 项目将项目链接到原始 Leviathan 项目。
The thing is, this of course doesn't change any of the <classpathentry ... >
entries that refer to "Leviathan\lib". So, we have to do one of two things:
问题是,这当然不会改变任何<classpathentry ... >
引用“Leviathan\lib”的条目。所以,我们必须做两件事之一:
- Change all of the references from
Leviathan
toLeviathan_<branch name>
in my .classpath using a text editor find/replace. - Remove all of the references to Leviathan*.jar in Java Build Path and re-add the jars in Leviathan_scott*.jar.
- 更改所有的引用来自
Leviathan
于Leviathan_<branch name>
我的.classpath使用文本编辑器查找/替换。 - 在 Java Build Path 中删除对 Leviathan*.jar 的所有引用,并在 Leviathan_scott*.jar 中重新添加 jar。
The problem with these is that it needs to be done every timethe .classpath is changed, which makes both of these seem less than ideal. Our classpath isn't changed super-regularly, but I would say it's changed once or twice every two weeks. I'd like to be able to link my project and have to perform only a single step in order to link these projects.
这些的问题在于每次更改 .classpath 时都需要完成它,这使得这两个看起来都不太理想。我们的类路径不会定期更改,但我会说它每两周更改一次或两次。我希望能够链接我的项目,并且只需执行一个步骤即可链接这些项目。
It seems like we could set up an environment variable within Eclipse and add this into the .classpath entries. But, now we have a problem because if we have multiple Test projects in our workspace that we want to compile, this won't work. Both of the test projects' .classpath entries will refer to the same environment variable, which of course can only point to one super project location.
似乎我们可以在 Eclipse 中设置一个环境变量并将其添加到 .classpath 条目中。但是,现在我们遇到了一个问题,因为如果我们要编译的工作区中有多个测试项目,这将不起作用。两个测试项目的 .classpath 条目将引用相同的环境变量,当然只能指向一个超级项目位置。
It seems like I should be able to do this, but I don't know exactly how to accomplish it in Eclipse. Any thoughts would be greatly appreciated.
看起来我应该能够做到这一点,但我不知道如何在 Eclipse 中完成它。任何想法将不胜感激。
~Scott
~斯科特
采纳答案by Kelly S. French
Try using Eclipse's 'Classpath Variables'.
尝试使用 Eclipse 的“类路径变量”。
Window->Preferences->Java->Build Path->Classpath Variables
Define one or more variables like "Leviathan_under_test" and point it to the version you want to test. Then, in the project build path, change the library reference to include the variable.
定义一个或多个变量,例如“Leviathan_under_test”,并将其指向您要测试的版本。然后,在项目构建路径中,更改库引用以包含该变量。
When you change the version of Leviathan you want to test, you change the variable and go. If you have to, you can even have multiple variables for versions that are tested more often, i.e. "LEVIATHAN_LATEST", OR "LEVIATHAN_PROBLEM_CHILD", etc.
当您更改要测试的利维坦版本时,您只需更改变量即可。如果必须,您甚至可以为更频繁测试的版本设置多个变量,即“LEVIATHAN_LATEST”或“LEVIATHAN_PROBLEM_CHILD”等。
回答by Martijn Verburg
This is a dependency management issue and there's lots of de-facto standard tools to help! I strongly suggest you utilise ANTwith Ivy'sdependency Managment or use Maven(or any otehr build tool that has dependency management).
这是一个依赖管理问题,有很多事实上的标准工具可以提供帮助!我强烈建议您将ANT与Ivy 的依赖项管理一起使用或使用Maven(或任何具有依赖项管理的其他构建工具)。
With either of those I'd use a artifact repository maanger such as Nexusor Artifactory as well, so you have the one true source for your internally built artifacts.
对于其中任何一个,我也会使用工件存储库管理器,例如Nexus或 Artifactory,因此您拥有内部构建工件的唯一真正来源。
This way you'll alway have the canonical version of the library that you want, when you want it.
通过这种方式,您将始终拥有所需的库的规范版本,无论何时。
回答by pbanfi
If you don't want to use external tools to manage the various project dependencies you could try to go to the single Leviathan projects Build path property, in the tab Order and Export, and check the libraries you need to run your tests. in this way you don't need to import the libraries in the test project and by switching the project under test you automatically update the libraries
如果您不想使用外部工具来管理各种项目依赖项,您可以尝试转到单个 Leviathan 项目的 Build path 属性,在选项卡 Order and Export 中,并检查运行测试所需的库。这样你就不需要在测试项目中导入库,通过切换被测项目,你会自动更新库