springboot引入项目信息

需要在application.properties中引用项目信息(项目名、项目版本),但希望和依赖管理工具(maven\gradle)自动同步

MANIFEST.MF

wiki解释

受到banner.txt利用MANIFEST.MF的启发,只需要在打包时写入项目信息就好了。

bootwar\bootjar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bootWar{
manifest{
attributes(
'Implementation-Version': version,
'Implementation-Title': rootProject.name
)
}
}

bootJar{
manifest{
attributes(
'Implementation-Version': version,
'Implementation-Title': rootProject.name
)
}
}

引用MANIFEST.MF

1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
@PropertySource(value = [
"META-INF/MANIFEST.MF",
"classpath:/config.properties",
"classpath:/db.properties",
"file:/\${user.home}\\scooper\\\${root.project.name}\\config.properties",
"file:/\${user.home}\\scooper\\\${root.project.name}\\db.properties",
"file:/icooper/config/\${root.project.name}/db.properties",
"file:/icooper/config/\${root.project.name}/config.properties",
"file:db.properties",
"file:config.properties"], ignoreResourceNotFound = true)
open class AppConfig

application.properties 中使用

冒号后为默认值

1
2
3
4
5
root.project.name=${Implementation-Title:scfm-rest}
root.project.version=${Implementation-Version:notfound}

spring.swagger.version=${root.project.version}
spring.swagger.title=${root.project.name}