【问题标题】:Maven requires plugin version to be specified for the managed dependency spring-boot-configuration-processorMaven 需要为托管依赖项 spring-boot-configuration-processor 指定插件版本
【发布时间】:2022-01-09 10:20:06
【问题描述】:

我有一个带有模块的 Maven 项目。我的根项目的父级是spring-boot-starter-parent,它提供了很多依赖管理。

在我的模块中,我使用spring-boot-configuration-processor,它是由spring-boot-starter-parent 管理的依赖项之一(嗯,实际上由其父级spring-boot-dependencies 管理)。

如果我在插件部分指定版本,我的构建将失败并显示:

Resolution of annotationProcessorPath dependencies failed: For artifact {org.springframework.boot:spring-boot-configuration-processor:null:jar}: The version cannot be empty. -> [Help 1]

所以,我不得不让插件部分看起来像这样:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <annotationProcessorPaths>
                <path>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-configuration-processor</artifactId>
                    <version>2.6.0</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
    </plugin>
</plugins>

但是,我更愿意引用继承的版本。虽然spring-boot-dependencies对于各种依赖的版本有很多属性,但是对于spring-boot-configuration-processor的版本没有属性。插件管理中也不包含spring-boot-configuration-processor

如何使用这个插件的继承版本,而不必自己明确指定版本?

【问题讨论】:

  • 我不认为你可以。 Maven 允许您继承依赖项和插件的版本,但这是一种特殊情况,因为它是插件的配置。您至少可以利用 spring-boot-configuration-processor 的版本将匹配 spring-boot-starter-parent 的事实,因此您可以将自己的属性spring.version 设置为 2.6.0,然后将其用于他们两个。
  • @Michael 我试过了,但它给出了一个不同的错误:Non-resolvable parent POM: Could not find artifact org.springframework.boot:spring-boot-starter-parent:pom:${spring.version} in central (https://repo.maven.apache.org/maven2) and 'parent.relativePath' points at no local POM

标签: java spring spring-boot maven


【解决方案1】:

我确实相信,而不是配置 maven-compiler-plugin,您只需使用 scope=providedspring-boot-configuration-processor 声明为项目的依赖项 - maven-compiler-pluginannotationProcessorPathsannotationProcessors 参数的目的不是添加注解处理器,但强制编译器仅使用已定义的注解处理器。

<plugins>
...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
...
</plugins>

<dependencies>
...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <scope>provided</scope>
    </dependency>
...
</dependencies>

【讨论】:

  • 我查看了“提供的”范围,但我仍然不明白为什么它会起作用。为什么它需要有这个范围,在这种情况下真正“提供”依赖的东西是什么?
  • 在你的情况下 scope=provided 导致 maven 编译器插件在编译时向类路径添加依赖项,因此 javac 将能够发现此类依赖项提供的注释处理器,但生成的工件将不依赖于它们. scope=provided 的另一个用例是通过dependencyManagement 禁止不需要的依赖项,而不是放置大量的排除项
  • @FractalLotus 它不应该需要传递才能工作。提供的范围基本上只是意味着依赖项不应该是可传递的。如果没有项目依赖于您的项目,则范围无关紧要。该名称来自 Jboss 等时代,应用服务器将 提供 一些依赖项,您只需在 POM 中声明它们,以便您可以针对 API 进行编译。考虑到您使用的是 Spring Boot,几乎可以肯定您没有使用应用服务器,因此从这些方面考虑它是没有帮助的。
  • "导致 maven 编译器插件在编译时向类路径添加依赖" 隐式/默认范围(编译)也会这样做。提供的唯一额外功能是防止传递性。
  • @AndreyB.Panfilov 如果您要尝试纠正我,请确保您实际上是正确的:github.com/spring-projects/spring-boot/issues/413 我过去确实遇到过这个问题,令我惊讶的是,Lombok尽管提供了范围,但 jar 最终出现在我的最终工件中。
【解决方案2】:

你需要从父 POM 继承:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>{YOUR_VERSION}</version>
</parent>

【讨论】:

  • 正如我在原始问题中所写,spring-boot-starter-parent 已经是我的根项目的父项目。
猜你喜欢
  • 2020-03-06
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 2013-04-07
  • 2012-12-28
  • 2021-08-22
相关资源
最近更新 更多