maven - 要求对传递依赖项有明确的 Maven 依赖项

标签 maven dependencies maven-plugin

我正在创建一个本身具有依赖性 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 Pluginwriting a custom plugin包装/扩展编译器插件,但是 <optional>可能是最权宜之计。

关于maven - 要求对传递依赖项有明确的 Maven 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17305349/

相关文章:

.net - 如何使 MSBuild 自动将所有间接引用复制到输出 (bin) 文件夹

maven - 在没有pom.xml的情况下运行自定义Maven目标

java - Spring MVC。 Tomcat 。 HTTP 状态 404 – 未找到

java - 如何使用 AngularJS 使用 war 名称发送请求?

java - Elasticsearch7.3 中 SpringBoot 出现 NoClassDefFoundError 异常

java - 获得可由 STDOUT 中的脚本解析的干净的机器友好 Maven 行/xml/json 输出?

jquery - 如何使用peerDependency解决NPM中的依赖 hell

python - 如何从 s3 存储桶和单独的事件 JSON 加载依赖项?

java - Maven 阴影插件 - 无法执行目标

java - 生成 java 类时无法使用 maven protobuf-maven-plugin 插件解析 google protobuf 文件中的导入语句