在多个项目中共享JAR的最佳方式是什么? [英] What's the best way to share JARs across multiple projects?

查看:133
本文介绍了在多个项目中共享JAR的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您有多个项目都使用同一组JAR库时,每个项目一次又一次地包含相同的JAR是不麻烦的。如果我正在开展20个不同的项目,那么我宁可不会有20个相同的精确JAR文件。让所有这些项目(和新项目)引用同一组JAR的最佳方法是什么?

When you have multiple projects that all use the same set of JAR libraries, it's tedious to include the same JARs over and over again with each project. If I'm working on 20 different projects, I'd rather not have 20 of the same exact set of JAR files lying around. What's the best way to make all those projects (and new projects as well) reference the same set of JARs?

我有一些想法,但每个都有一些缺点:

I have some ideas, but each of them has some disadvantages:


  • 将所有JAR放在一个文件夹中,并让每个项目都在该文件夹中。

  • 使用Eclipse创建一个用户库,并让每个项目都引用该用户库。

  • 创建一个引用每个JAR的Library项目,并为该项目引用该项目。 / li>
  • Place all the JARs in a folder and have each project look in that folder.
  • Using Eclipse, create a "User Library" and have each project reference that user library.
  • Create a "Library" project that references each JAR, and have each project reference that library project.

推荐答案

使用 Maven 常春藤处理这些共享罐子。如果您担心更改项目太多,可以使用常春藤来管理额外的类路径对于你。

Use Maven or Ivy to handle these shared jars. If you're wary of changing your projects too much, you can simply use Ivy to manage the extra classpath for you.

两个都有很好的Eclipse插件:

Both have good Eclipse plugins:

Maven classpath容器http://img229.imageshack.us/img229/4848/mavendependencies.png

IvyDE classpath容器http://img76.imageshack.us/img76/ 3180 / cpnode.jpg

我使用的结果很好。

请注意,他们都将工作空间之外的jars 引用到工作区,因此重复已被删除。

You'll note that both of them reference jars outside the workspace, so the duplication is removed.

更新(由评论提示):

我推荐这种方法的原因是我坚信这更简单并且更清晰地声明依赖关系,而不是手动地包含它们。有一个小的一次性成本与此相关联 - 对于常春藤而言比Maven更小 - 但是从长远来看,它将有所回报。

My reason for recommending this approach is that I strongly believe that it's simpler and clearer to declare dependencies rather then manually include them. There is a small one-time cost associated with this - smaller for Ivy than for Maven - but in the long run it does pay off.

另一个较小的优点是处理传递和冲突的依赖关系。很容易忘记为什么需要classpath中的commons-logging-1.1.jar,以及是否需要升级到1.1.1。拉入所有需要的部分(例如 Hibernate + Annotation + Spring combo)也不是很有趣。专注于编程,而不是建设。

Another, smaller, benefit is the handling of transitive and conflicting dependencies. It's easy to forget why you need that commons-logging-1.1.jar in the classpath and whether you need to upgrade to 1.1.1. And also it's no fun to pull in all the depencies required for e.g. a Hibernate + Annotation + Spring combo. Focus on programming, not building.

这篇关于在多个项目中共享JAR的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆