maven 外部依赖
In the previous tutorial, we understood on how to build and test a maven project and the commands used for the same. In this chapter, we will learn about managing external dependencies for a project in maven.
在上一教程中,我们了解了如何构建和测试Maven项目以及用于该项目的命令。 在本章中,我们将学习有关在maven中管理项目的外部依赖关系的知识。
As explained earlier, all the basic list of dependencies in maven is handled by the maven repository at which a project downloads the required dependencies. But, there will be certain scenario at which some particular dependencies may not be available in the maven remote and central repositories. Maven still answers this scenario by providing the feature of external dependency management.
如前所述,maven中所有基本的依赖关系列表都由项目下载所需依赖关系的maven存储库处理。 但是,在某些情况下,某些特定的依赖项可能在Maven远程和中央存储库中不可用。 Maven仍然通过提供外部依赖项管理功能来回答这种情况。
An external dependency can be such as sqljdbc.jar or log4j. In maven, any external dependencies can be easily configurable as other dependencies in the pom.xml file. So, let us see a simple example to add sqljdbc.jar as an external dependency into a maven project.
外部依赖项可以是诸如sqljdbc.jar或log4j。 在maven中,可以像pom.xml文件中的其他依赖项一样轻松配置任何外部依赖项。 因此,让我们看一个简单的示例,将sqljdbc.jar作为外部依赖项添加到Maven项目中。
Create a folder called lib in the maven project.
在maven项目中创建一个名为lib的文件夹。
Download the sqljdbc.jar and put it inside the lib folder.
下载sqljdbc.jar并将其放在lib文件夹中。
Now, open the pom.xml of the project and add a new dependency as shown below:
现在,打开项目的pom.xml并添加一个新的依赖项,如下所示:
Specify groupId as name of the jar file
指定groupId作为jar文件的名称
Specify artifactId as name of the jar file
指定artifactId作为JAR文件的名称
Specify the scope of the system
指定系统范围
Specify the relative path of the jar file to project location.
指定jar文件到项目位置的相对路径 。
<dependency>
<groupId>sqljdbc</groupId>
<artifactId>sqljdbc</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\sqljdbc.jar</systemPath>
</dependency>
依赖范围 (Dependency Scope)
Dependency scope in maven is classified into 5 different types. Each of these scope controls the dependencies that are available in the class path and dependencies that are included in the application.
Maven中的依赖关系范围分为5种类型。 这些作用域中的每一个都控制类路径中可用的依赖关系以及应用程序中包含的依赖关系。
Dependency Scope | Description |
---|---|
compile | This is the default scope for any external dependency in maven. These dependencies are made available in the classpath and they are packaged well. |
provided | These dependencies are expected to be provided by the JDK or a container. The best example is the servlet api. These dependencies are made available on the classpath but not at runtime. Also, they are not packaged. |
runtime | These are the dependencies which are not required for the compilation, but required to execute and test the system. E.g. JDBC API |
test | These dependencies are required only during the test compilation and execution phase. |
system | This scope is similar to "provided". The only thing is that developer need to explicitly provide the path of the jar file on the local file system. |
依赖范围 | 描述 |
---|---|
编译 | 这是Maven中任何外部依赖项的默认范围。 这些依赖关系在类路径中可用,并且打包得很好。 |
提供 | 这些依赖关系应由JDK或容器提供。 最好的例子是servlet API。 这些依赖关系在类路径上可用,但在运行时不可用。 此外,它们没有包装。 |
运行 | 这些是编译所不需要的,但是执行和测试系统所必需的依赖。 例如JDBC API |
测试 | 仅在测试编译和执行阶段才需要这些依赖项。 |
系统 | 此范围类似于“已提供”。 唯一的事情是开发人员需要在本地文件系统上显式提供jar文件的路径。 |
翻译自: https://www.studytonight.com/maven/managing-external-dependency-in-maven
maven 外部依赖