我想让开发人员在 pom.xml 中定义,并在构建过程后将标记显示在 application.yml 中。不知何故,它适用于除开发人员之外的所有属性。
这是在一个 Spring Boot 项目中,我希望在构建过程中填充属性。
这是 pom.xml 的摘录
<description>my description</description>
<developers>
<developer>
<id>12345</id>
<name>John Doe</name>
<email>john@doe.com</email>
</developer>
</developers>
这是在 application.yml 中
info:
description: "@project.description@"
developer: "@project.developers[0].id@"
它适用于描述,但不适用于开发人员。我尝试了很多变体,例如${..}, "@project.developers.0.id"。似乎没有任何工作。
如果有人有想法,我将不胜感激。
最佳答案
您可以阅读开发者id
或 email
地址或来自 pom.xml
的任何值用这种优雅的方式:
build-info.properties
使用默认数据加上您的附加数据 BuildProperties
轻松地从此文件中读取值. pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<developer>${project.developers[0].email}</developer>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
这将产生 build-info.properties
META-INF
下的文件包含以下内容的目录:build.artifact=<artifactId-from-pom>
build.group=<groupId-from-pom>
build.name=<name-from-pom>
build.time=<build-time>
build.version=<version-from-pom>
build.developer=<email-of-the-first-developer-from-pom>
然后你可以用 Spring 读取值:@Configuration
public class OpenApiConfiguration {
@Autowired
private BuildProperties buildProperties;
@Bean
public OpenAPI customOpenAPI()) {
return new OpenAPI().info(new Info()
.title(...)
.version(buildProperties.getVersion())
.contact(new Contact().email(buildProperties.get("developer"))));
}
}
阅读:我希望这会帮助你。
关于java - 在 application.yml (Spring Boot) 中访问 Maven 属性 "developers",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57041326/