Ral*_*alf 5 configuration maven spring-boot
我有一个 maven 多模块项目,其中包含一个父模块和三个子模块。该应用程序使用弹簧引导。在其中一个子模块中,我有 SpringBootApplication:
@SpringBootApplication
@EnableConfigurationProperties({AppProperties.class})
public class MainSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MainSpringBootApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
应用属性在同一个模块中:
@Data
@ConfigurationProperties(prefix = "asdf")
public class AppProperties {
...
}
Run Code Online (Sandbox Code Playgroud)
在该模块的 pom.xml 中有一个 spring-boot-configuration-processor 的依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我在父项目上运行 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>
Run Code Online (Sandbox Code Playgroud)
直接在子模块上做mvn install,生成target/classes/META-INF/spring-configuration-metadata.json文件。
你有什么提示吗?
我知道有两种选择。第一个是我最喜欢的(特别是因为它配置了 APT 库的顺序 - 代码生成的顺序)。但基于 IDE 自动发现机制,第二个也是一个不错的选择。
两者都主要针对最终工件(依赖项范围)的最小大小,这对我来说非常重要。在 k8s/docker/cloud 时代(资源效率),不要通过无用的依赖项(仅在编译时需要 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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
笔记:
target/classes/META-INF/spring-configuration-metadata.jsonjava doc 生成文档,对于基于 lombok 的 java 类,你也需要这些(@Getter 和 @Setter - 都需要)。@Setter
@Getter
@ConfigurationProperties(prefix = "asdf")
public class AppProperties {
/**
* foo - Should not be null or empty.
*/
private Map<String, String> foo;
Run Code Online (Sandbox Code Playgroud)
以及以下 Maven 编译器插件配置作为属性(或在插件配置中)。
<maven.compiler.parameters>true</maven.compiler.parameters>
Run Code Online (Sandbox Code Playgroud)
spring-configuration-metadata.json文件并在 application.properties/application.yml 中提供建议/快速文档/自动完成。克尔
我明确地补充道:
<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>
Run Code Online (Sandbox Code Playgroud)
到plugins子模块的 pom 中包含@ConfigurationProperties带注释的类的部分。现在target/classes/META-INF/spring-configuration-metadata.json已经生成了。