java java中处理外部库的正确方法(使用eclipse)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3683677/
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
The right way to deal with external libraries in java (using eclipse)
提问by posdef
This is in a way a follow-up of a previously unanswered question of mine (link) which excalated over the past weeks, and now it has come to a point where I cant really develop anymore...
这在某种程度上是我之前未回答的问题(链接)的后续行动,该问题在过去几周内升级,现在已经到了我无法真正发展的地步......
So here's the deal; I have more of a mathematics/engineering background than pure CS, so I dont have a lot of experience with proper/large-scale software development, but rather scripting and algorithms. Now that I am working on a large project, on my own, I am confused with some of the aspects of development. One of them being handling libraries/dependencies
所以这是交易; 与纯 CS 相比,我有更多的数学/工程背景,所以我在适当的/大规模软件开发方面没有很多经验,而是脚本和算法。现在我自己在做一个大项目,我对开发的一些方面感到困惑。其中之一是处理库/依赖项
I have initially created a folder called lib
under my project folder (in Eclipse workspace) and copied my external libraries in there and then added them to the build path. However later on I needed some other stuff like JCommons
, JFreeChart
, Apache Commons Math
etc.. According to instructions these libraries can/should be included as user libraries, allowing the developer to see the documentation and source code from within IDE (such as Eclipse). I have gotten it right so far, I hope...
我最初lib
在我的项目文件夹(在 Eclipse 工作区中)下创建了一个文件夹,并将我的外部库复制到其中,然后将它们添加到构建路径中。然而后来我需要一些其它的东西一样JCommons
,JFreeChart
,Apache Commons Math
等。根据这些指令库可以/应当被包括作为用户库,允许开发者看到从内IDE(如Eclipse)的文档和源代码。到目前为止我已经做对了,我希望......
So where is the problem? Well first off it appeared as the user libraries mentioned above are not included on the SVN copy of the project, meaning that my colleagues who wanted to test-drive my project couldn't do so, by just simply acquiring the project from SVN. Now the second phase of the problem unsurfaced when I changed my workstation at work, I wanted to import my project into Eclipse from my backup, but then everything except those user libraries are there. To make things more complicated, when this software is done, it will be implemented on a server so it would be absolutely best if everything could be packed into one single library or a even better a runnable jar
file.
那么问题出在哪里呢?好吧,首先它似乎是因为上面提到的用户库不包含在项目的 SVN 副本中,这意味着我想要测试驱动我的项目的同事无法这样做,只需从 SVN 获取项目即可。现在,当我在工作中更换工作站时,问题的第二阶段没有浮出水面,我想将我的项目从备份导入 Eclipse,但是除了那些用户库之外的所有内容都在那里。更复杂的是,当这个软件完成后,它将在服务器上实现,所以如果所有东西都可以打包到一个单独的库中,或者甚至是一个更好的可运行jar
文件,那将是绝对最好的。
I have been previously advised to take a look at Maven, or Ivy, but my initial understanding after checking them both is that they are used from the beginning and mostly for more complicated projects. Honestly I am completely puzzled as to how I am supposed to manage my dependencies. Any ideas?
之前有人建议我看看 Maven 或 Ivy,但在检查它们之后我的初步理解是它们从一开始就被使用,并且主要用于更复杂的项目。老实说,我完全不知道我应该如何管理我的依赖项。有任何想法吗?
(Sorry for keeping it long, but I figured better complete and long then inadequate information)
(很抱歉把它写得太长了,但我认为完整和冗长的信息比不充分的信息更好)
EDIT:I have managed to sort out the problem; the problem has apparently originated from the simple fact that eclipse doesn't copy all resources to one place, and the installation instructions for some libraries doesn't really warn you about how to manage the libraries in the best way.
编辑:我已经设法解决了这个问题;问题显然源于一个简单的事实,即 eclipse 不会将所有资源复制到一个地方,并且某些库的安装说明并没有真正警告您如何以最佳方式管理这些库。
Thanks to everyone, who took their time to try and help me. I will in time look more into projects like Maven and Ivy, it's definitely interesting stuff. However for now I just need to get back to the software into running state, been wrestling with new stuff for too long.. :)
感谢所有花时间尝试帮助我的人。我会及时更多地研究像 Maven 和 Ivy 这样的项目,这绝对是有趣的东西。但是现在我只需要让软件回到运行状态,与新东西搏斗太久了..:)
回答by Ben
At the risk of committing an act of heresy, I'd say Maven and wot-not are overkill here. You need your dependencies in your SVN project, simple as that. You've added your lib folder to your build path in Eclipse, and that's fine. But unless you specifically add the contents of the lib folder to your project (as Fred describes), those items won't be eligible for committing to SVN -- they will simply be referenced in your set-up's classpath. That's good for you, no good for anyone looking to check out your project (as you have found).
冒着犯下异端行为的风险,我想说 Maven 和 wot-not 在这里是矫枉过正的。您的 SVN 项目中需要您的依赖项,就这么简单。您已将 lib 文件夹添加到 Eclipse 中的构建路径中,这很好。但是,除非您专门将 lib 文件夹的内容添加到您的项目中(如 Fred 所述),否则这些项目将没有资格提交给 SVN——它们只会在您的设置的类路径中被引用。这对你有好处,对任何想要检查你的项目的人都没有好处(如你所见)。
I appreciate that we could have endless discussions about best practice, the validity of committing third party libraries to version control, and so on and so forth... but I think most people have work to do ;-)
我很感激我们可以就最佳实践、将第三方库提交给版本控制的有效性等进行无休止的讨论……但我认为大多数人都有工作要做;-)
回答by Boris Pavlovi?
回答by Fred Grott
Okay I think this can be solved..
好吧,我认为这可以解决..
In Eclipse in the java build path screen there is also an import jar libraries choice that allows you to choose the project lib folder..in fact there is a choice labeled folder I think..
在 Eclipse 的 java 构建路径屏幕中,还有一个导入 jar 库选择,允许您选择项目 lib 文件夹..事实上,我认为有一个标记为文件夹的选择..
The other thing since it will be deployed to a server you will have to have every jar in the lib folder.
另一件事,因为它将部署到服务器,所以您必须将每个 jar 都放在 lib 文件夹中。
There is an ant technique where you check for a class and a property in the jar to verify its the correct jar to import from the lib folder before proceeding with that sequence of the build.. do a Google search and you will find the posts about it..
有一种 ant 技术,您可以检查 jar 中的类和属性,以验证它是从 lib 文件夹导入的正确 jar,然后再继续构建的序列.. 进行 Google 搜索,您将找到关于它..
回答by Bozho
The three options are:
这三个选项是:
- Maven(refcard) - a very powerful tool, but at the same time very easy to use. I use it in all my projects, no matter how small they are. It is dependency management + build tool in one
- Ivy- much like maven, but it is only a dependency management tool. You'd have to do your builds with ant
- committing jars & eclipse project files - this is not portable between IDEs, but actually isn't that horrible and is used in many projects
- Maven( refcard) - 一个非常强大的工具,但同时也非常易于使用。我在我所有的项目中都使用它,无论它们有多小。依赖管理+构建工具合二为一
- Ivy- 很像 maven,但它只是一个依赖管理工具。你必须用蚂蚁做你的构建
- 提交 jars 和 eclipse 项目文件 - 这在 IDE 之间是不可移植的,但实际上并没有那么可怕并且在许多项目中使用
Update: a few words explaining maven ideology:
更新:解释 Maven 意识形态的几句话:
- convention over configuration - you structure your project in a predefined way. That doesn't have to do with dependency management, so just mentioning it.
- repositories - that's where the jar files actually reside. They don't reside in your SVN, because they have a separate mechanism of versioning and because they take up unnecessary space.
- IDEs integrate with maven. For example m2eclipsegets the maven dependencies and appends them to your eclipse build path, thus making the usage transparent
- dependency resolution - in
pom.xml
you define multiple<dependency>
tags, with name and version, and maven fetches all the required jars from the remote repositories. It also fetches jars on which your dependencies depend (transitive dependencies). Thus you don't end up havingNoClassDefFoundException
.
- 约定优于配置 - 您以预定义的方式构建项目。这与依赖管理无关,所以只提一下。
- 存储库 - 这是 jar 文件实际所在的位置。它们不驻留在您的 SVN 中,因为它们具有单独的版本控制机制并且占用了不必要的空间。
- IDE 与 Maven 集成。例如m2eclipse获取 maven 依赖项并将它们附加到您的 eclipse 构建路径,从而使使用透明
- 依赖解析 - 在
pom.xml
你定义多个<dependency>
标签,带有名称和版本,maven 从远程存储库中获取所有需要的 jars。它还获取依赖项所依赖的 jar(传递依赖项)。因此,您最终不会拥有NoClassDefFoundException
.
To me this is extremely straightforward: you define whatyou need, and maven decides how to fetch it, and how to add it to your classpath.
对我来说,这是非常简单的:你定义什么你需要,和Maven决定如何获取,以及如何将它添加到你的classpath。
回答by huo73
At our company we have a separate SVN Repository for third party libraries, and we have a company rule that on every development workstation this repository is checked out to C:\dev. So every one, has the libraries in the right place and the projects works fine.
在我们公司,我们有一个单独的第三方库的 SVN 存储库,我们有一个公司规则,即在每个开发工作站上,这个存储库都被检出到 C:\dev。因此,每个人都将库放在正确的位置,并且项目运行良好。
回答by tddmonkey
You don't have to have a complicated project or a greenfield project to benefit from Maven so I would take the advice you've already been given to try and give it a go.
您不必拥有复杂的项目或新建项目才能从 Maven 中受益,因此我会采纳您已经获得的建议,尝试一下。
If you really don't want to, then you should really be checking your libraries into SVN along with your code. If a version of your code relies on a specific library at a certain point in time then there is a contractthere and by not including the libraries in SVN you're breaking that contract- something you've already found as you can't recreate your application from scratch elsewhere.
如果您真的不想这样做,那么您真的应该将您的库与您的代码一起检入 SVN。如果您的代码版本在某个时间点依赖于特定的库,那么那里就有一个合同,如果不将这些库包含在 SVN 中,您就违反了该合同-您已经发现了无法重新创建的内容您在别处从头开始的应用程序。
As for packing up in a single JAR- this is possible with Ant, but why would you need to?
至于打包在单个 JAR 中——这对 Ant 来说是可能的,但你为什么需要这样做?