简体   繁体   English

管理各种Java外部库的最优雅解决方案是什么?

[英]What is the most elegant solution to managing various Java external libraries?

Perhaps the reason I stalled learning Java until now is because I HATE how Java handles external libraries. 也许我之前停止学习Java的原因可能是因为我讨厌 Java如何处理外部库。 I'm stuck keeping them in one place, adding them individually, fixing problems with versioning and every time I move/rename them, and copying and writing the classpath over and over each time I release a Java application. 我被困在一个地方,将它们单独添加,修复版本控制问题以及每次移动/重命名它们,以及每次发布Java应用程序时反复复制和编写类路径。

There has to be an elegant solution to all of this. 所有这一切都必须有一个优雅的解决方案。 I keep all of my libraries (regardless of task, platform, or other) in their own little folder inside a "lib" folder in my development folder, kind of like this: 我将我的所有库(无论任务,平台或其他)保存在我的开发文件夹中的“lib”文件夹中的自己的小文件夹中,有点像这样:

 Dev -lib +JS-jQuery +Flex-Degrafa -Java-Xerces +Xerces-1.2.3 +More libraries 

I can use either Netbeans or Eclipse for Java dev, but none of them provide a very streamlined (and not to mention idiot-proof) way of managing all of these. 我可以使用Netbeans或Eclipse for Java dev,但它们都没有提供管理所有这些的非常简化(并且更不用说防止白痴)的方式。

A nudge in the right direction or an online article/tutorial on this would be greatly appreciated. 我们将非常感谢在正确方向上的推动或在线文章/教程。

您可以使用Ant + IvyMaven来管理库依赖项。

If it is only dependency management you're after and you're happy with the rest of your build process, I would use Ivy , as it can unobtrusively manage your dependencies, leaving your existing build process intact. 如果它只是你所依赖的依赖管理,并且你对构建过程的其余部分感到满意​​,我会使用Ivy ,因为它可以不显眼地管理你的依赖项,使你现有的构建过程保持不变。 There is a plugin for Eclipse called IvyIDE that contributes your dependencies via a classpath container. 有一个名为IvyIDE的 Eclipse插件,它通过类路径容器提供依赖项。

Maven 2 has a steeper learning curve but provides a much richer set of functionality for building your projects and Eclipse integration through m2eclipse or IAM . Maven 2具有更陡峭的学习曲线,但通过m2eclipseIAM为构建项目和Eclipse集成提供了更丰富的功能。

Personally I use Maven as I have a large number of projects to work with and Maven is particularly suited to efficient development across lots of projects. 我个人使用Maven,因为我有大量项目可以使用,Maven特别适合于大量项目的高效开发。

Have a look at the introductory documentation to see what works for you. 查看介绍性文档,了解哪些内容适合您。

Netbeans 6.7.1's Maven support is quite good and comes out of the box with the IDE. Netbeans 6.7.1的Maven支持非常好,开箱即用的IDE。

The Eclipse addon was frustrating enough that I gave Netbeans another try. Eclipse插件令人沮丧,我给了Netbeans另一次尝试。

A third choice besides ChssPly76's options is to use Ant with the Maven Ant Tasks . 除了ChssPly76的选项之外,第三个选择是将Ant与Maven Ant任务一起使用 I don't know if I'd call any of these solutions particularly "elegant," but they do spare you the need to manage your own lib/ directory and classpath variables. 我不知道我是否会将这些解决方案中的任何一个称为“优雅”,但它们确实使您无需管理自己的lib /目录和类路径变量。

If you're working on Linux you can install Java libraries with APT or RPM. 如果您正在使用Linux,则可以使用APT或RPM安装Java库。

Otherwise, I normally check precompiled JARs into a lib directory in my project's version control repository and make sure the names of the JAR files include full version information. 否则,我通常会将预编译的JAR检查到项目版本控制存储库中的lib目录中,并确保JAR文件的名称包含完整版本信息。 Eg lib/foo-1.5.6.jar, not lib/foo.jar. 例如lib / foo-1.5.6.jar,而不是lib / foo.jar。

To avoid having to manually set the classpath before running your app, you can set the classpath in the Manifests of the JARs themselves to define the dependencies of each JAR file. 为避免在运行应用程序之前必须手动设置类路径,可以在JAR自身的清单中设置类路径,以定义每个JAR文件的依赖关系。 The JVM will follow all the dependencies when loading classes. 加载类时,JVM将遵循所有依赖项。

Maven is often more trouble than it's worth, but the ability to open a maven project directly into IDEs such as IntelliJ is excellent. Maven通常比它的价值更麻烦,但是直接在诸如IntelliJ之类的IDE中打开maven项目的能力非常好。 For example, IntelliJ will download all dependencies and have them available without having to run a build first, or an mvn command and then a project refresh. 例如,IntelliJ将下载所有依赖项并使其可用,而无需先运行构建,或者使用mvn命令,然后再执行项目刷新。 It also isn't necessary to re-generate the project every time a dependency is added. 每次添加依赖项时,也没有必要重新生成项目。 I work with a number of Eclipse developers who switched to IntelliJ for this alone. 我与许多Eclipse开发人员合作,他们仅为此而切换到IntelliJ。

However, one shortfall of Maven is that many libraries (or versions of libraries) are not available on public repositories. 但是,Maven的一个缺点是公共存储库中没有许多库(或库的版本)。 Therefore it is often necessary to set up a local repository such as archiva . 因此,通常需要设置本地存储库,例如archiva In ant, it would just be a matter of adding it to the lib directory in the repository. 在ant中,只需将其添加到存储库中的lib目录即可。

Maven can also attack when you need to do something that maven doesn't directly support via a plugin. 当你需要做一些maven不能通过插件直接支持的东西时,Maven也会攻击。 What would normally be a few lines of ant can often turn into a morning's worth of work. 什么通常是几行蚂蚁通常可以变成早上的工作。

Finally, buildr is an excellent way of using Maven's dependency management and plugins, while also supporting ad-hoc tasks. 最后, buildr是使用Maven依赖管理和插件的绝佳方式,同时还支持临时任务。

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

相关问题 最优雅的是java的数字()解决方案 - Most elegant isNumeric() solution for java 最优雅的解决方案来链接java中的异步调用? - Most elegant solution to chain asynchronous calls in java? Java中执行空检查的最优雅方法是什么 - What is the most elegant way of doing null checks in Java 对于Java中无法通过引用传递的最优雅的解决方法是什么? - What's the most elegant workaround for inability to pass by reference in Java? 编程成语言:在Java中定义回调的最优雅方法是什么? - Programming into a language: What is the most elegant way to define callbacks in Java? 在Java中将一个列表映射到另一个列表的最优雅方法是什么? - What is the most elegant way to map one list to another in Java? 在Java中使用分隔符连接值列表的最优雅方法是什么? - What's the most elegant way to concatenate a list of values with delimiter in Java? 那里有哪些Java外部软件包/库? - What Java external packages/libraries out there? 在Java中转义$的最优雅方法 - most elegant way of escaping $ in java 用于管理glassfish / tomcat / etc中的.properties文件的优雅解决方案 - Elegant solution for managing .properties files in glassfish/tomcat/etc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM