【问题标题】:spring-boot-configuration-processor is not working on maven submodule projectspring-boot-configuration-processor 不适用于 maven 子模块项目
【发布时间】:2019-10-07 14:36:41
【问题描述】:

我有一个包含一个父模块和三个子模块的 Maven 多模块项目。 该应用程序使用弹簧启动。在其中一个子模块中,我有 SpringBootApplication:

@SpringBootApplication
@EnableConfigurationProperties({AppProperties.class})
public class MainSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainSpringBootApplication.class, args);
    }
}

应用程序属性在同一个模块中:

@Data
@ConfigurationProperties(prefix = "asdf")
public class AppProperties {
...
}

在该模块的 pom.xml 中有一个 spring-boot-configuration-processor 的依赖项:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

现在的问题是,当我在父项目上运行 mvn install 时,target/classes/META-INF/spring-configuration-metadata.json 未创建此子模块中的文件。当我将该子模块的 pom 修改为直接继承自:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

直接在子模块上做mvn install,生成target/classes/META-INF/spring-configuration-metadata.json文件。

你有什么提示吗?

【问题讨论】:

    标签: maven spring-boot configuration


    【解决方案1】:

    我明确添加:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <annotationProcessorPaths>
            <annotationProcessorPath>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-configuration-processor</artifactId>
              <version>2.1.5.RELEASE</version>
            </annotationProcessorPath>
          </annotationProcessorPaths>
        </configuration>
      </plugin>
    

    到包含 @ConfigurationProperties 注释类的子模块的 pom 的 plugins 部分。现在target/classes/META-INF/spring-configuration-metadata.json 已生成。

    【讨论】:

    【解决方案2】:

    我知道有两种选择。 第一个是我最喜欢的(特别是因为它配置了 APT 库的顺序——代码生成的顺序)。 但是基于 IDE 自动发现机制,第二个也是一个不错的选择。

    两者都主要针对最终工件(依赖项的范围)的最小大小,这对我来说非常重要。 在 k8s/docker/cloud 时代(资源效率),不要用无用的依赖项(仅在编译时需要 apt 库)增加可交付成果/原型的大小。

    所以,事不宜迟,选项:

    1. 仅在 maven 编译器插件配置中使用 APT 库(在依赖项中没有)。
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>${maven-compiler-plugin.version}</version>
                   <configuration>
                       <annotationProcessorPaths>
                           <path>
                               <groupId>org.projectlombok</groupId>
                               <artifactId>lombok</artifactId>
                               <version>${lombok.version}</version>
                           </path>
                           <path>
                               <groupId>org.springframework.boot</groupId>
                               <artifactId>spring-boot-configuration-processor</artifactId>
                               <version>${spring-boot.version}</version>
                           </path>
                       </annotationProcessorPaths>
                   </configuration>
               </plugin>
    
    1. 此选项适用于未在 plugins/pluginsManagement 中配置 maven-compiler-plugin 的情况(但可能通过其属性)。
    
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-configuration-processor</artifactId>
               <scope>provided</scope>
           </dependency>
    
           <dependency>
               <groupId>org.projectlombok</groupId>
               <artifactId>lombok</artifactId>
               <scope>provided</scope>
           </dependency>
    
    

    注意事项:

    • 对于选项 2,提供的范围很重要,因为它允许在编译期间使用 APT,但不会包含在最终工件中。
    • 另一方面(回到您的问题),为了从 java 文档生成target/classes/META-INF/spring-configuration-metadata.json 中的文档,对于基于 lombok 的 java 类,您也需要这些(@Getter 和 @Setter - 都需要) .
    @Setter
    @Getter
    @ConfigurationProperties(prefix = "asdf")
    public class AppProperties {
    
       /**
        * foo - Should not be null or empty.
        */
       private Map<String, String> foo;
    

    并将以下 maven 编译器插件配置作为属性(或在插件配置中)。

       <maven.compiler.parameters>true</maven.compiler.parameters>
    
    • 编译后,IDE 将解析 spring-configuration-metadata.json 文件并在 application.properties/application.yml 中提供建议/快速文档/自动完成。

    【讨论】:

    • 第二个选项非常适合org.mapstruct: &lt;dependency&gt; &lt;groupId&gt;org.mapstruct&lt;/groupId&gt; &lt;artifactId&gt;mapstruct-processor&lt;/artifactId&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt;
    猜你喜欢
    • 1970-01-01
    • 2015-10-10
    • 2019-08-15
    • 1970-01-01
    • 2014-06-02
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多