无法在带有@Value的Spring应用程序中获取maven project.version属性

Jac*_* G. 38 maven spring-boot

如何在带有@Value注释的Spring Boot应用程序中获取maven project.version属性?

Jac*_* G. 72

经过一些关于如何在Spring Boot应用程序中获取maven项目版本的研究和试验,我找不到任何适合我的东西.

由于类加载器问题,使用清单肯定是一个腐烂的路径,即一个获得Spring发现的第一个清单,在我的情况下,这不是我的应用程序之一.

我发现的一个解决方案是使用maven资源插件来"过滤"(替换)资源文件中的属性.在这种情况下,春天application.properties.

以下是使这项工作的步骤.

在pom中,使用以下定义激活资源过滤:

    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <includes>
                <include>application.properties</include>
            </includes>
        </resource>
    </resources>
Run Code Online (Sandbox Code Playgroud)

application.properties文件中:

  application.name=@project.artifactId@
  build.version=@project.version@
  build.timestamp=@timestamp@
Run Code Online (Sandbox Code Playgroud)

注意@ property @而不是$ {property}.application.properties文件中.

spring-boot-starter-parent pom将标准$ {}分隔符重新定义为@:

    <resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
    <delimiters>
        <delimiter>${resource.delimiter}</delimiter>
    </delimiters>
Run Code Online (Sandbox Code Playgroud)

然后可以使用@Value在Spring中访问这些属性,如下所示:

@Value("${application.name}")
private String applicationName;

@Value("${build.version}")
private String buildVersion;

@Value("${build.timestamp}")
private String buildTimestamp;
Run Code Online (Sandbox Code Playgroud)

这里有一个示例项目.

  • 你基本上解决了Spring文档为它描述的内容:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html文档中的一点是如果你使用Spring Boot父级,您不需要显式定义资源过滤器. (4认同)
  • 时间戳实际上应该是`build.timestamp = @maven.build.timestamp @`. (4认同)
  • 关于如何使用 yaml 执行此操作有什么建议吗?我试图使用值周围的引号来转义“@”字符(即“@project.artifactId@”,但在 Cent-OS 中,当我将应用程序作为 jar 运行时,属性不会被解析 (2认同)

Cep*_*pr0 29

要获得访问Maven的性质春季启动应用程序的所有我们需要的是用分隔符将它们映射@application.properties这样的:

app.version=@project.version@
app.name=@project.name@
Run Code Online (Sandbox Code Playgroud)

然后在应用程序中像普通属性一样使用它们,例如:

@Service
public class SomeService {

   @Value("${app.version}")
   private String appVersion;

   // other stuff
}
Run Code Online (Sandbox Code Playgroud)

来源:使用Maven自动扩展属性

但是如果我们使用yaml来存储应用程序属性,我们必须将分隔符替换@为其他分隔符,例如^在我们的pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <delimiters>
            <delimiter>^</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

或者更简单-只需更换变量resource.delimiterpropeties你的pom.xml的块:

<properties>
    <java.version>11</java.version>
    <resource.delimiter>^</resource.delimiter>
</properties>
Run Code Online (Sandbox Code Playgroud)

然后用它application.yml:

app:
  version: ^project.version^
  name: ^project.name^
Run Code Online (Sandbox Code Playgroud)


Mil*_*sai 11

有一种更简便的方法,不需要添加application.properties或定界符更改。只需添加带有目标build-info的插件,并使用bean BuildProperties来自动启动类。

<plugin>
<groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   <version>2.1.3.RELEASE</version>
   <executions>
     <execution>
       <goals>
         <goal>build-info</goal>
       </goals>
     </execution>
   </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

启动班将有

@Autowired
  BuildProperties buildProperties;
Run Code Online (Sandbox Code Playgroud)

稍后在@PostConstruct中,您可以调用多个方法来检索构建时间戳,版本,名称,工件等。

buildProperties.getName();
buildProperties.getVersion();
buildProperties.get("time");
Run Code Online (Sandbox Code Playgroud)

所述info致动器将自动使用,并且如果它检测到它,以及显示显示该信息的git信息如果发现任何。

  • 这是最干净的答案。@MilanDesai 非常感谢你! (6认同)
  • 对于“BuildProperties 自动装配失败”,只需运行“mvn clean package”,然后从 Eclipse/IntelliJ 重新启动 Spring Boot 应用程序。 (5认同)
  • 在 SpringBoot 1.5.1 中,我们没有这样的 bean:“考虑重新审视上述条件或在配置中定义一个类型为 'org.springframework.boot.info.BuildProperties' 的 bean。” (2认同)