初学者查询 Maven 依赖关系和外部库
即使我必须使用 Maven 依赖项,我是否也必须添加外部 jar 文件。我对 Maven 非常陌生,真的无法理解它的实际用途。请指导我理解maven。谢谢。
Should I have to add external jar files even I have to use Maven dependencies. I am really new to maven and really cant understand what its actually use for. Please guide me understanding maven.Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
传统上,如果您的项目需要使用一些开源工具和框架,您必须从这些工具的官方网站手动下载它们。如果你只想使用库A,但是这个库A依赖于另一个库B的代码,那么你必须同时下载库A和B。这很麻烦,因为你不仅要下载你想使用的库,还可以下载这些库所依赖的任何其他库。
Maven依赖特性的要点就在于它解决了这样的问题。您只需在配置文件(
pom.xml
)中定义您想要在项目中使用哪些库,然后 Maven 将帮助您自动下载这些库并检索这些库需要的任何其他库从公共 Maven 存储库或您在 pom.xml 中定义的存储库进行工作。然而,出于商业和版权的原因,并非所有常用的库都可以在 Maven 公共存储库上使用。其中一个例子是 Oracle JDBC 驱动程序。对于这种情况,您必须手动下载该库并将其导入为外部库您的项目使用的库。您也可以将其导入到本地 Maven 存储库。
Traditionally , if your project requires to use some open source tools and frameworks , you have to manually download them from those tools' official websites . If you want to use libraries A only , but this libraries A depends on the code from another libraries B , you have to download both libraries A and B. It is very troublesome as you not only have to download the libraries you want to use , but also download any additional libraries that those libraries depend on.
The points of Maven dependency feature is that it solves such problem . You only have to define what libraries you want to use in your project in the configuration file (
pom.xml
) , then Maven will help you to download these libraries automatically and retrieve any additional libraries that those libraries need to work from the public Maven repositories or the repositories you defined in thepom.xml
.However ,for commercial and copyright reasons, not all of the commonly used libraries are available on the Maven public repositories .One of the example is the Oracle JDBC Driver .For this case,you have to manually download the library and import it as the external libraries for your project uses .You can also import it to your local Maven repository.
您不需要手动添加任何外部库。您需要将这些库添加为maven中的依赖项,此后,maven将为您处理该库。
例如,如果您需要将
slf4j
外部库(jar)作为依赖项添加到项目中,请将以下内容添加到 mavenpom.xml
文件中。如果您有一个不在任何公共存储库中的个人库,那么您需要将该 jar 文件安装到本地 Maven 存储库。
You don't need to add any external libraries manually. You need to add those lib as dependencies in maven, thereafter, maven will handle the library for you.
For example if you need to add
slf4j
external library(jar) to your project as a dependency, add the following to the mavenpom.xml
file.If you have a personal library which is not in any public repositories, then you need to install that jar file to your local maven repository.
我建议阅读这本书: http://www.sonatype.com/books /mvnref-book/reference/。一般来说,Maven 项目不应该有包含 jar 的 lib 文件夹,因为在 99% 的情况下,可以从各种存储库检索必要的依赖项。
I'd recomment to read this book: http://www.sonatype.com/books/mvnref-book/reference/. In general, Maven project should not have lib folder with jars since in 99% of the cases, necessary dependencies can be retrieved from various repositories.