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

时间:2023-01-24 17:21:08

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_ 。因此,例如,在我的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工作区中时,他/她将需要通过偏好(>Java构建路径)>项目并添加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:

问题是,这当然不会改变任何 条目指的是“利维坦(Leviathan\lib)”。所以,我们必须做两件事中的一件: …>

  1. Change all of the references from Leviathan to Leviathan_<branch name> in my .classpath using a text editor find/replace.
  2. 在.classpath中使用文本编辑器find/replace将所有引用从Leviathan更改为Leviathan_
  3. Remove all of the references to Leviathan*.jar in Java Build Path and re-add the jars in Leviathan_scott*.jar.
  4. 删除所有对《利维坦》的引用*。在Java构建路径中的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. 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

~斯科特

3 个解决方案

#1


2  

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”,等等。

#2


2  

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).

这是一个依赖项管理问题,有很多事实上的标准工具可以帮助您!我强烈建议您使用ANT与Ivy的依赖项管理或使用Maven(或任何具有依赖项管理的otehr构建工具)。

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.

对于其中任何一种,我都会使用一个工件库maanger,比如Nexus或Artifactory,这样您就有了内部构建工件的一个真正来源。

This way you'll alway have the canonical version of the library that you want, when you want it.

这样,当您想要的时候,您就可以拥有您想要的库的标准版本。

#3


1  

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项目构建路径属性,在选项卡订单和导出中,并检查您需要运行测试的库。通过这种方式,您不需要在测试项目中导入库,通过切换测试项目,您可以自动更新库

#1


2  

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”,等等。

#2


2  

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).

这是一个依赖项管理问题,有很多事实上的标准工具可以帮助您!我强烈建议您使用ANT与Ivy的依赖项管理或使用Maven(或任何具有依赖项管理的otehr构建工具)。

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.

对于其中任何一种,我都会使用一个工件库maanger,比如Nexus或Artifactory,这样您就有了内部构建工件的一个真正来源。

This way you'll alway have the canonical version of the library that you want, when you want it.

这样,当您想要的时候,您就可以拥有您想要的库的标准版本。

#3


1  

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项目构建路径属性,在选项卡订单和导出中,并检查您需要运行测试的库。通过这种方式,您不需要在测试项目中导入库,通过切换测试项目,您可以自动更新库