我正在创建一个本身具有依赖性 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 定义为“可选”依赖项,如以下问题和链接的博客文章中所述:
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 Plugin或 writing a custom plugin包装/扩展编译器插件,但是 <optional>
可能是最权宜之计。
关于maven - 要求对传递依赖项有明确的 Maven 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17305349/