1. Introduction
1.绪论
Maven is a project management and comprehension tool. It is based on the concept of the project object model, also known as POM. Using the POM as a central piece of information, Maven can manage a project’s build, reporting, and documentation.
Maven是一种项目管理和理解工具。它以项目对象模型(也称POM)的概念为基础。利用POM作为中心信息,Maven可以管理项目的构建、报告和文档。。
A big part of Maven is dependency management. Most developers will interact with this feature of Maven while working on an application.
Maven的一个重要部分是依赖性管理。大多数开发者在开发应用时都会与Maven的这一功能互动。
Maven’s superior dependency management provides automatic updating as well as dependency closure. Another way companies use Maven for dependency management is by using a custom central repository. By doing so, developers can use their own dependencies on other projects within the company.
Maven卓越的依赖性管理提供了自动更新以及依赖性封闭。公司使用Maven进行依赖性管理的另一种方式是使用自定义中央存储库。通过这种方式,开发人员可以在公司内部的其他项目上使用自己的依赖性。
In this tutorial, we’ll learn how to find Maven dependencies.
在本教程中,我们将学习如何寻找Maven的依赖性。
2. What Is a Maven Dependency
2.什么是Maven的依赖性
In the context of Maven, a dependency is simply a JAR file used by a Java application. Based on the POM file, Maven will download and add the JAR file to our Java path. Java will then be able to find and use the classes in the JAR file.
在Maven中,依赖是指Java应用程序使用的JAR文件。根据POM文件,Maven会下载JAR文件并添加到我们的Java路径中。这样,Java就能找到并使用JAR文件中的类。
It is also important to note that, Maven has a local repository where it downloads all the dependencies. By default, this is located in the {user home folder}/.m2/repository.
还需要注意的是,Maven有一个本地仓库,它在那里下载所有依赖项。默认情况下,它位于{用户主文件夹}/.m2/repository。
3. POM File
3.POM文件
The POM file uses the XML syntax, where everything is between tags.
POM文件使用XML语法,一切都在标签之间。
By default, the POM file is populated only with our project information. In order to add dependencies that our project will use, we need to add the dependencies section:
默认情况下,POM文件只填充了我们的项目信息。为了添加我们项目将使用的依赖项,我们需要添加dependencies部分。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>maven.dependency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
....
</dependencies>
</project>
This is only needed when we manually edit the POM file.
只有在我们手动编辑POM文件时才需要这样做。
4. Ways to Find Maven Dependencies
4.寻找Maven依赖的方法
When working on a new project or adding a new feature, we may realize that we need to add a new dependency to our project. Let’s take a simple example where we need to add the JDBC dependency.
当我们在做一个新的项目或添加一个新的功能时,我们可能会意识到我们需要向我们的项目添加一个新的依赖关系。让我们举一个简单的例子,我们需要添加JDBC的依赖关系。
Depending on our IDE and setup, there are different ways to find the needed details for the JDBC dependency.
根据我们的IDE和设置,有不同的方法来找到JDBC依赖的所需细节。
4.1. IntelliJ
4.1.兴发xf187在线娱乐
If we use IntelliJ IDEA, we can add a new dependency to the project’s POM by following a few steps.
如果我们使用IntelliJ IDEA,我们可以通过以下几个步骤在项目的POM中添加一个新的依赖关系。
Firstly, we open the POM file, then press ALT+INSERT and then click on the DEPENDENCY option:
首先,我们打开POM文件,然后按ALT+INSERT,然后点击DEPENDENCY选项:
Then, we can search for the needed dependency and click ADD:
4.2. Eclipse
日蚀
Eclipse IDE has a similar way to IntelliJ for adding a new dependency.
Eclipse IDE有一个与IntelliJ类似的添加新依赖关系的方法。
We need to right-click on the POM file in the package explorer or after opening the file, and then we go to Maven -> Add dependency option:
我们需要在软件包资源管理器中或打开文件后右键点击POM文件,然后进入Maven->添加依赖性选项。
Then, we can search for the needed dependency and click OK:
4.3. Internet Search
4.3.互联网搜索
If we are ok with manually editing the POM file, then we can simply search on search.maven.org or Google to find all the details of the dependency.
如果我们可以手动编辑POM文件,那么我们可以简单地在search.maven.org或Google上搜索,找到依赖关系的所有细节。
When going to search.maven.org, we can simply type the dependency in the search bar, and we’ll find plenty of versions of the dependency. Unless there are other restrictions in our project, we should use the latest stable version:
当进入search.maven.org时,我们只需在搜索栏中输入依赖关系,就能找到大量该依赖关系的版本。除非我们的项目有其他限制,否则我们应该使用最新的稳定版本。
In our POM file, in the dependency section, simply paste the dependency details found.
在我们的POM文件中,在依赖性部分,简单地粘贴发现的依赖性细节。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.21</version>
</dependency>
There are also plenty of other websites similar to search.maven.org that we can use and explore.
还有很多与search.maven.org类似的其他网站,我们可以使用和探索。
5. Conclusion
5.总结
In this tutorial, we saw the different ways to add a Maven dependency to our project.
在本教程中,我们看到了向项目添加Maven依赖的不同方法。
We also learned how to edit the POM file in order to get Maven to download and use the dependency added.
我们还学习了如何编辑POM文件,以便让Maven下载并使用添加的依赖性。