【问题标题】:Making a requiring an explicit maven dependency on a transitive dependency在传递依赖上创建一个明确的 maven 依赖
【发布时间】:2013-06-22 17:21:28
【问题描述】:

我正在创建一个本身具有依赖项 DEP 的 maven 插件。 DEP 可以是多个版本之一。我需要的是消费者必须明确提及 DEP 及其版本。我不希望消费者使用对 DEP 的隐式依赖,并在我发布新 DEP 时自动获取新版本。

这是我现在所拥有的,但这意味着如果消费者的 pom 中没有指定 DEP,那么他们将始终获得最新的。

在我的插件的依赖部分,我有这个:

<dependencies>
    <dependency>
        <groupId>org.foo.bar</groupId>
        <artifactId>DEP</artifactId>
        <version>[1.8.3-01,9.9.9)</version>
    </dependency> 
    ...
</dependencies>

然后,有人使用我的插件:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <compilerId>my-compiler-id/compilerId>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.foo.bar</groupId>
                    <artifactId>my-compiler</artifactId>
                    <version>1.0.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

在上述情况下,一切都适用于消费者,他们总是获得最新版本的DEP。如果他们想要特定版本的 `DEP,那么他们需要在他们的 pom 中显式添加它,就像在 maven-compiler-plugin 的依赖项部分中一样:

                <dependency>
                    <groupId>org.foo.bar</groupId>
                    <artifactId>DEP</artifactId>
                    <version>1.8.6-09</version>
                </dependency>

现在,这个额外的显式依赖是可选的。如果缺少,则使用最新的。我想让这个依赖强制。我该怎么做?

【问题讨论】:

  • 您想强制插件的用户定义DEP 和特定版本,而不是默认为最新版本?
  • 正确。这就是我需要的。
  • 您希望构建失败吗?
  • 是的。如果消费者的依赖关系不明确,则构建应该失败。理想情况下,如果出现有意义的错误会很好:“缺少 DEP 依赖项。请添加它...”,但这不是必需的。
  • 您是否尝试在插件 pom 中将 DEP 声明为 &lt;optional&gt;

标签: maven dependencies maven-plugin


【解决方案1】:

您应该能够将您的 DEP 定义为“可选”依赖项,如以下问题和链接的博客文章中所述:

Best strategy for dealing with optional dependencies

http://axelfontaine.com/blog/optional-dependencies.html

基本上只是:

            <dependency>
                <groupId>org.foo.bar</groupId>
                <artifactId>DEP</artifactId>
                <version>1.8.6-09</version>
                <optiona>true</optional>
            </dependency>

现在您的用户在使用此插件时必须显式添加此依赖项,否则会遇到错误。

也可能有一些方法可以实现相同的结果,但会显示更多信息错误消息 - 使用包装/扩展编译器插件的 Maven Enforcer Pluginwriting a custom plugin,但 &lt;optional&gt; 可能是最方便的方法。

【讨论】:

  • 谢谢。现在工作得很好。我能够构建 my-compiler-id 类,以便它在尝试从中加载任何内容之前首先检查 DEP 是否存在。我捕捉到 ClassNotFoundError 并打印出一条可读的错误消息,告诉用户该做什么。
猜你喜欢
  • 2018-07-06
  • 2019-10-29
  • 1970-01-01
  • 2019-08-23
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多