maven 外部依赖_在Maven中管理外部依赖项

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 ScopeDescription
compileThis is the default scope for any external dependency in maven. These dependencies are made available in the classpath and they are packaged well.
providedThese 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.
runtimeThese are the dependencies which are not required for the compilation, but required to execute and test the system. E.g. JDBC API
testThese dependencies are required only during the test compilation and execution phase.
systemThis 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 外部依赖

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven是一个功能强大的目构建工具,它可以管理依赖关系。在一些情况下,我们希望将依赖的包外部化,即不将这些依赖包打包到目的输出文件。 要实现maven打包依赖外部化,可以通过以下步骤: 1. 在目的pom.xml文件,使用<scope>provided</scope>标签将依赖的范围设置为provided。通过此标签,我们告诉maven这些依赖在运行时已经存在于目标环境,因此不需要将它们打包到最终生成的目文件。例如: ```xml <dependency> <groupId>com.example</groupId> <artifactId>dependency-package</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> ``` 2. 在目部署时,确保将所需的依赖包含在运行时环境。这可以通过将这些依赖放置在运行时环境的类路径来实现。例如,可以将这些依赖放置在Tomcat服务器的共享库目录下,或者在应用程序的WEB-INF/lib目录下放置这些依赖的JAR文件。 通过这种方式,maven在构建目时会将依赖打包到目输出文件,但在运行时却不需要将这些依赖放置在,而是依赖外部环境提供的包。 这种方式的好处是可以减小目的发布包大小,提高目的运行效率和部署速度,并且可以更灵活地管理目的依赖关系。同时,也能够避免由于依赖包重复打包而导致的潜在冲突问题。 然而,需要注意的是,这种方式要求运行时环境必须存在所依赖的包,否则会导致目在运行时出现错误。因此,在部署目之前,需要确保外部环境已经配置了正确的依赖
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值