简体   繁体   English

在Eclipse项目中放置和链接外部JAR的Java最佳实践?

[英]Java best practices of placing and linking external JARs in a Eclipse project?

I'm just getting back into Java and I've been trying to figure out how exactly I should be organizing my project. 我刚刚回到Java,我一直在试图弄清楚我应该如何组织我的项目。 I come from a C# background where you just have to make sure that the libraries you compile are retrievable by the executable (or other libraries) by keeping them in the same folders or subfolders, but java seems to do things differently. 我来自C#背景,你必须确保你编译的库可以被可执行文件(或其他库)检索,方法是将它们保存在相同的文件夹或子文件夹中,但java似乎做了不同的事情。

In my Eclipse workspace I have my main project I'm working on (just one for now, more later) and it references external JARs (in my case, the gazillion of files of Apache Axis2). 在我的Eclipse工作区中,我有我正在处理的主项目(现在只有一个,后来更多),它引用了外部JAR(在我的例子中,Apache Axis2的大量文件)。

Where should I be storing these libraries that my projects depends on when building? 我应该在哪里存储我的项目在构建时依赖的库? I will need to add everything to source control really soon, and so I can't keep referencing these JARs by absolute path on my machine. 我需要很快将所有内容添加到源代码控制中,因此我无法通过机器上的绝对路径继续引用这些JAR。 I read suggestions for putting this IN a subfolder of my main project, but I feel that's not ideal. 我读到了把这个放在我主要项目的子文件夹中的建议,但我觉得这并不理想。 I think I should be keeping them in the same source-controlled parent folder as the project, and somehow reference them through relative path (eclipse one does absolute path through the UI, really?), so something like: 我想我应该将它们保存在与项目相同的源控制父文件夹中,并以某种方式通过相对路径引用它们(eclipse一个通过UI执行绝对路径,真的吗?),所以类似于:

c:\\JavaProjects\\MyProject c:\\JavaProjects\\axis2-1.5.4 c:\\JavaProjects\\%whatever other library% c:\\ JavaProjects \\ MyProject c:\\ JavaProjects \\ axis2-1.5.4 c:\\ JavaProjects \\%,无论其他库%

Is there an even better way? 还有更好的方法吗?

Ideally, you shouldn't be storing the libraries in your project. 理想情况下,您不应该将库存储在项目中。 They can be retrieved by public (or private) repositories via so called dependency-management tools. 它们可以通过所谓的依赖管理工具由公共(或私有)存储库检索。 The two most prominent ones are Maven and Ivy . 最突出的两个是MavenIvy

Eclipse (and any other IDE) has integration with these tools (via plugins) and adds the desired libraries to the classpath automatically. Eclipse(以及任何其他IDE)与这些工具集成(通过插件),并自动将所需的库添加到类路径中。 You only have to define them (together with their version) in a configuration file. 您只需在配置文件中定义它们(及其版本)。

The learning curve is a bit steap, but I would really consider using Maven . 学习曲线有点小,但我真的考虑使用Maven

The M2Eclipse will be needed for full integration though. 但是,完全集成需要M2Eclipse

Take the time to learn and use Maven 3 . 花点时间学习和使用Maven 3

Set up a private repository manager, I use Archiva . 设置私有存储库管理器,我使用Archiva

Use Jenkins for continuious integration, it works with Maven seamlessly. 使用Jenkins进行持续集成,它可以与Maven无缝协作。

This does take some time and effort, but when it is configured, it doesn't really need to be touched again and makes one step build and deployment a snap! 这确实需要花费一些时间和精力,但是当它配置好后,它实际上不需要再次触摸,只需快速构建和部署一步!

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

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