Maven 简明教程
Maven - External Dependencies
如你所知,Maven 使用存储库概念完成依赖管理。但如果依赖项在任何远程存储库和中央存储库中都不可用,会发生什么情况呢?Maven 使用 External Dependency 的概念为这种情况提供了答案。
As you know, Maven does the dependency management using the concept of Repositories. But what happens if dependency is not available in any of remote repositories and central repository? Maven provides answer for such scenario using concept of External Dependency.
例如,让我们对在“创建 Java 项目”章节中创建的项目进行以下更改。
For example, let us do the following changes to the project created in ‘Creating Java Project’ chapter.
-
Add lib folder to the src folder.
-
Copy any jar into the lib folder. We’ve used ldapjdk.jar, which is a helper library for LDAP operations.
现在我们的项目结构应如下所示 −
Now our project structure should look like the following −
这里,您拥有自己的特定于项目的库,这是一个常见的情况,它包含 jar 文件,Maven 可能无法从任何存储库中下载这些 jar 文件。如果您的代码使用 Maven 使用此库,则 Maven 构建将失败,因为它在编译阶段无法下载或引用此库。
Here you are having your own library, specific to the project, which is an usual case and it contains jars, which may not be available in any repository for maven to download from. If your code is using this library with Maven, then Maven build will fail as it cannot download or refer to this library during compilation phase.
为了处理这种情况,让我们使用以下方法将此外部依赖项添加到 maven pom.xml 。
To handle the situation, let’s add this external dependency to maven pom.xml using the following way.
<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
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.bank</groupId>
<artifactId>consumerBanking</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>consumerBanking</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ldapjdk</groupId>
<artifactId>ldapjdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
</dependency>
</dependencies>
</project>
查看上面的示例中 dependencies 依赖项下面的第二个依赖项元素,它明确了以下有关 External Dependency 的关键概念。
Look at the second dependency element under dependencies in the above example, which clears the following key concepts about External Dependency.
-
External dependencies (library jar location) can be configured in pom.xml in same way as other dependencies.
-
Specify groupId same as the name of the library.
-
Specify artifactId same as the name of the library.
-
Specify scope as system.
-
Specify system path relative to the project location.
希望你现在已经对外部依赖项有了清晰的了解,你将能够在你的 Maven 项目中指定外部依赖项。
Hope now you are clear about external dependencies and you will be able to specify external dependencies in your Maven project.