简体   繁体   English

如何在Eclipse Helios中的另一个项目中引用库?

[英]How can I reference libraries in another project within Eclipse Helios?

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. 基本上,我们有一个产品,我称之为Leviathan。 This project has quite a number of other versions, each of which we maintain as separate projects in Eclipse. 这个项目有很多其他版本,我们在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中同时打开多个项目(多个版本),因为我们获得了有关旧版本的帮助热线调用(以及同时开发多个版本)。

We also have test code, which is in a different project for each distribution of Leviathan. 我们还有测试代码,这是针对Leviathan的每个发行版的不同项目。 I name my projects Leviathan_<branch name> . 我将我的项目命名为Leviathan_<branch name> So, for example, in my Eclipse workspace, I might have projects like: 因此,例如,在我的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. 问题是我们在测试代码中引用了Leviathan \\ lib中的一些库。 Each developer may name their projects slightly differently. 每个开发人员可能会略微区别他们的项目 So, in developing the test project's .classpath, we reference the project Leviathan\\ (no additions). 因此,在开发测试项目的.classpath时,我们引用了项目Leviathan \\(没有添加)。 The classpath for this project (which is checked into our source control system) might look like: 此项目的类路径(在我们的源代码管理系统中检查)可能如下所示:

<?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. 当开发人员将Test项目加载到他/她的个人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". 问题是,这当然不会改变任何引用“Leviathan \\ lib”的<classpathentry ... >条目。 So, we have to do one of two things: 所以,我们必须做两件事之一:

  1. Change all of the references from Leviathan to Leviathan_<branch name> in my .classpath using a text editor find/replace. 使用文本编辑器find / replace将我的.classpath中Leviathan所有引用更改为Leviathan_<branch name>
  2. Remove all of the references to Leviathan*.jar in Java Build Path and re-add the jars in Leviathan_scott*.jar. 删除Java Build Path中对Leviathan * .jar的所有引用,并在Leviathan_scott * .jar中重新添加jar。

The problem with these is that it needs to be done every time the .classpath is changed, which makes both of these seem less than ideal. 这些问题是每次更改.classpath时都需要完成,这使得这两者看起来都不太理想。 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. 我希望能够链接我的项目,并且必须只执行一个步骤才能链接这些项目。

It seems like we could set up an environment variable within Eclipse and add this into the .classpath entries. 看起来我们可以在Eclipse中设置一个环境变量并将其添加到.classpath条目中。 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. 但是,现在我们遇到了一个问题,因为如果我们想要编译的工作空间中有多个Test项目,那么这将无法工作。 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. 两个测试项目的.classpath条目都将引用相同的环境变量,当然这只能指向一个超级项目位置。

It seems like I should be able to do this, but I don't know exactly how to accomplish it in Eclipse. 看起来我应该能够做到这一点,但我不知道如何在Eclipse中完成它。 Any thoughts would be greatly appreciated. 任何想法将不胜感激。

~Scott 〜斯科特

This is a dependency management issue and there's lots of de-facto standard tools to help! 这是一个依赖管理问题,有许多事实上的标准工具可以提供帮助! I strongly suggest you utilise ANT with Ivy's dependency Managment or use Maven (or any otehr build tool that has dependency management). 我强烈建议您将ANTIvy的依赖Managment一起使用或使用Maven (或任何具有依赖关系管理的工具构建工具)。

With either of those I'd use a artifact repository maanger such as Nexus or 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. 通过这种方式,您可以随时拥有所需库的规范版本。

Try using Eclipse's 'Classpath Variables'. 尝试使用Eclipse的'Classpath Variables'。

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. 定义一个或多个变量,如“Leviathan_under_test”,并将其指向要测试的版本。 Then, in the project build path, change the library reference to include the variable. 然后,在项目构建路径中,更改库引用以包含该变量。

When you change the version of Leviathan you want to test, you change the variable and go. 当您更改要测试的Leviathan版本时,您可以更改变量并转到。 If you have to, you can even have multiple variables for versions that are tested more often, ie "LEVIATHAN_LATEST", OR "LEVIATHAN_PROBLEM_CHILD", etc. 如果必须,您甚至可以为经常测试的版本提供多个变量,即“LEVIATHAN_LATEST”或“LEVIATHAN_PROBLEM_CHILD”等。

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. 如果您不想使用外部工具来管理各种项目依赖项,可以尝试转到单个Leviathan项目构建路径属性,在“顺序和导出”选项卡中,并检查运行测试所需的库。 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 通过这种方式,您无需在测试项目中导入库,并且通过切换测试项目,您可以自动更新库

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

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