Optional Dependency in Maven – Maven中的可选依赖性

最后修改: 2020年 8月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

This brief tutorial will describe Maven’s <optional> tag and how we can use it to reduce the size and scope of a Maven project’s artifact, such as a WAR, EAR, or JAR.

本教程将介绍Maven的<optional>标签,以及我们如何利用它来缩小Maven项目的工件(如WAR、EAR或JAR)的大小和范围。

For a refresher on Maven, check out our comprehensive guide.

如果想了解Maven的最新情况,请查看我们的综合指南

2. What Is <optional>?

2.什么是<可选>

Sometimes we’ll create a Maven project to be a dependency for other Maven projects. When working on such a project, it may be required to include one or more dependencies that are only useful to a subset of that project’s features.

有时我们会创建一个Maven项目,作为其他Maven项目的依赖项。在处理这样的项目时,可能需要包括一个或多个仅对该项目功能子集有用的依赖项。

If an end-user doesn’t use that feature subset, the project still transitively pulls in those dependencies. This bloats the user’s project size unnecessarily, and might even introduce conflicting dependency versions with other project dependencies.

如果最终用户不使用该功能子集,该项目仍然会过渡性地拉入这些依赖项。这不必要地扩大了用户的项目规模,甚至可能引入与其他项目依赖性冲突的依赖性版本。

Ideally, we should split the project’s feature subset into its own module and therefore not pollute the rest of the project. However, this is not always practical.

理想情况下,我们应该将项目的特征子集分割成自己的模块,从而不对项目的其他部分造成污染。然而,这并不总是实用的。

In order to exclude these special dependencies from the main project, we can apply Maven’s <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn’t need them.

为了将这些特殊依赖从主项目中排除,我们可以对它们应用Maven的<optional>标签。这就迫使任何想使用这些依赖的用户明确声明它们。然而,这并不能将这些依赖关系强加给不需要它们的项目。

3. How to Use <optional>

3.如何使用<可选>

As we’re going to see we can include the <optional> element with a value of true to make any Maven dependency optional.

我们将看到,我们可以将<optional>元素的值设为true,使任何Maven依赖成为可选项。

Let’s assume we have the following project pom:

让我们假设我们有下面的项目pom。

<project>
    ...
    <artifactId>project-with-optionals</artifactId>
    ...
    <dependencies>
        <dependency>
            <groupId>com.baeldung</groupId>
            <artifactId>optional-project</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

In this example, although optional-project is labeled as optional, it remains as a usable dependency of project-with-optionals as if the <optional> tag was never there.

在这个例子中,尽管optional-project被标记为可选的,但它仍然是project-with-optionals的一个可用的依赖项,就像<optional>标签从未出现过一样。

In order to see the effect of the <optional> tag, we need to create a new project that depends on project-with-optionals:

为了看到<optional>标签的效果,我们需要创建一个依赖project-with-optionals的新项目。

<project>
    ...
    <artifactId>main-project</artifactId>
    ...
    <dependencies>
        <dependency>
            <groupId>com.baeldung</groupId>
            <artifactId>project-with-optionals</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Now if we try to reference optional-project from within main-project, we see that optional-project doesn’t exist. This is because the <optional> tag prevents it from being transitively included.

现在,如果我们试图从main-project中引用optional-project,我们会发现optional-project并不存在。这是因为<optional>标签阻止了它被过渡性地包含。

If we find that we need optional-project in our main-project, we simply need to declare it as a dependency.

如果我们发现在我们的主项目中需要optional-project,我们只需要将它声明为一个依赖关系。

4. Conclusion

4.总结

In this article, we looked at Maven’s <optional> tag. The main benefits of using the tag are that it can reduce a project’s size and help prevent version conflicts. We also saw that the tag doesn’t affect the project that uses it.

在本文中,我们研究了Maven的<optional>标签。使用该标签的主要好处是,它可以减少项目的规模,并有助于防止版本冲突。我们还看到,该标签并不影响使用它的项目。

The source code in this article is available over on Github.

本文的源代码可在Github上获得