【问题标题】:Access Maven property "developers" in application.yml (Spring Boot)在 application.yml (Spring Boot) 中访问 Maven 属性“开发者”
【发布时间】:2019-11-24 05:59:30
【问题描述】:

我想要开发者,它在 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”。似乎没有任何工作。

如果有人有想法,我将非常感激。

【问题讨论】:

  • 它可能只是“@project.developers.developer[0].id@”?
  • 不,这也不起作用。 @wemu
  • 无论如何,这很容易。也找不到任何例子。带有点的属性语法转换为 .getXXX() - 所以可能是 project.developers.0。 - 或一些变体。如果完全支持,问题跟踪器可能包含一些提示
  • 我得出的结论是它不受支持,因为它是一个集合。现在我试图在运行时解析 pom.xml 以获取开发人员。 @wemu

标签: java spring maven spring-boot pom.xml


【解决方案1】:

您可以通过这种优雅的方式读取开发者idemail 地址或pom.xml 中的任何值:

  1. 使用默认数据和您的附加数据生成build-info.properties
  2. 使用 Spring 通过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>

这将在META-INF目录下生成一个build-info.properties文件,内容如下:

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"))));
    }
}

阅读:

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    将其添加到属性上并用于 pom 和属性文件,例如:

    <properties>
         <team.name>John Doe</team.name>
    </properties>
    

    用于开发者数据:

    <developers>
        <developer>
            <name>${team.name}</name>
    ...
    

    并在应用程序中使用该属性:

     description: "@team.name@"
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 2018-11-06
      • 2019-02-24
      • 2021-07-17
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多