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)
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)
但是如果我们使用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.delimiter在propeties你的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信息如果发现任何。
| 归档时间: |
|
| 查看次数: |
37292 次 |
| 最近记录: |