Java 如何使用Gradle和Spring Boot捕获构建信息
我正在尝试使用SpringBoot和Gradle访问Java主应用程序中的构建信息值,例如Java 如何使用Gradle和Spring Boot捕获构建信息,java,spring-boot,gradle,build.gradle,Java,Spring Boot,Gradle,Build.gradle,我正在尝试使用SpringBoot和Gradle访问Java主应用程序中的构建信息值,例如version 我找不到任何关于如何配置的文档/示例 build.gradle application.yml(如果需要) Java主类 有人能帮我举一个上面这些文件的小代码例子吗 在我的build.gradle文件中,我将有version条目,因此如何使用Spring Boot和gradle将其引入Java主类 格雷德尔先生 我试着加上 格雷德尔先生 但是buildInfo()在Intellij中不被
version
我找不到任何关于如何配置的文档/示例
build.gradle
(如果需要)application.yml
Java主类
build.gradle
文件中,我将有version
条目,因此如何使用Spring Boot和gradle将其引入Java主类
格雷德尔先生
我试着加上
格雷德尔先生
但是buildInfo()
在Intellij中不被识别为关键字
在我的Java主类中,我有以下内容:
public class MyExampleApplication implements CommandLineRunner {
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(MyExampleApplication.class, args);
}
@Override
public void run(String[] args) throws Exception{
Environment env = (Environment) context.getBean("environment");
displayInfo(env);
}
private static void displayInfo(Environment env) {
log.info("build version is <" + env.getProperty("version")
}
公共类MyExampleApplication实现CommandLineRunner{
@自动连线
私有应用程序上下文上下文;
公共静态void main(字符串[]args){
run(MyExampleApplication.class,args);
}
@凌驾
公共无效运行(字符串[]args)引发异常{
Environment env=(Environment)context.getBean(“环境”);
显示信息(环境);
}
私有静态void displayInfo(环境环境){
log.info(“构建版本是将以下内容添加到Gradle脚本中。它将版本正确插入jar清单,如下所示:
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
您的代码将能够从该jar清单文件中获取版本:
public class BuildVersion {
public static String getBuildVersion(){
return BuildVersion.class.getPackage().getImplementationVersion();
}
}
有关更多详细信息,请参阅下面的链接:Spring Boot使用buildInfo()
生成的信息自动配置BuildProperties
bean
因此,要获得信息,请使用context.getBean(BuildProperties.class).getVersion();
我现在就设法让它工作了-使用Vamper在下面给出的帮助指针和其他一些来源。关键是将执行器类添加到项目依赖项中。
注意:Intellj似乎无法识别springBoot标记中的buildInfo(),但它运行正常,所以不要推迟
build.gradle
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
gradleVersion = '3.3'
}
repositories {
mavenLocal()
maven { url "http://cft-nexus.ldn.xxxxxxxxx.com:8081/nexus/content/groups/public/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
springBoot{
buildInfo {
additionalProperties = [
'foo': 'bar'
]
}
}
compile "org.springframework.boot:spring-boot-starter-actuator"
MyExampleApplication
@Slf4j
@EnableIntegration
@EnableLoaderApplication
@SpringBootApplication
@EnableDiscoveryClient
public class MyExampleApplication implements CommandLineRunner {
private static final String SYSTEM_NAME_INFO = "My Example Application";
private static final String VERSION="0.0.1";
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(MyExampleApplication.class, args);
}
@Override
public void run(String[] args) throws Exception{
BuildProperties buildProperties = context.getBean(BuildProperties.class);
displayInfo(buildProperties);
}
private static void displayInfo(BuildProperties buildProperties) {
log.info("build version is <" + buildProperties.getVersion() + ">");
log.info("value for custom key 'foo' is <" + buildProperties.get("foo") + ">");
}
}
在Spring boot中获取版本号的简单方法
@Controller
@RequestMapping("/api")
public class WebController {
private final BuildProperties buildProperties;
public WebController(BuildProperties properties) {
buildProperties = properties;
}
@GetMapping("/test")
public String index() {
System.out.println(buildProperties.getVersion());
System.out.println(buildProperties.getArtifact());
System.out.println(buildProperties.getGroup());
System.out.println(buildProperties.getName());
System.out.println(buildProperties.getTime());
return "index";
}
}
别忘了生成application-build.properties
springBoot {
buildInfo()
}
谢谢你,吸血鬼。我已经按照你的建议更改了我的代码,但现在出现了一个异常,说明:一个组件需要一个类型为“org.springframework.boot.info.BuildProperties”的bean,但找不到它。-未加载“ProjectInfo自动配置”中的bean方法“BuildProperties”,因为@ConditionalOnResource未找到资源${spring.info.build.location:classpath:META-INF/build-info.properties}“我没有运行web应用程序这是一个独立的Springboot应用程序。我将您的回复标记为答案,因为它对我有很大帮助。如果您同意我的答案和示例代码,那么您是否也可以将其标记为答案,因为我无法标记自己的答案我不确定为什么它没有加载,据我所知,这也适用于sta?”ndalone spring boot应用程序。也许你的类路径中没有该文件?除此之外,只有提问者可以将答案标记为答案。并且只有一个答案可以标记为答案。你应该能够将自己的答案标记为答案。这可能需要一些时间或声誉才能完成。此外,请阅读并遵守。还有一点需要注意始终向上投票有帮助的答案,即使您将它们标记为其他答案。找不到build-info.properties的一种可能性是使用bootRun运行项目。我不知道让bootRun和buildinfo都工作的推荐方法。感谢您提供的信息!您知道springBoot buildinfo additionalProperties方法在哪里吗umented?奇怪的是,这里显示的类似但不同的方法不起作用:可能更容易利用内置的弹簧靴“info”执行器(/exactor/info
)在gradle构建中调用buildInfo
后,而不是滚动您自己的控制器method@runderworld对于部署在独立tomcat中的springboot war文件,如何做到这一点?
> 2017-11-14 14:35:47.330 INFO 22448 --- [ main]
> o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase
> 2147483647 2017-11-14 14:35:47.448 INFO 22448 --- [ main]
> s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s):
> 8780 (http) 2017-11-14 14:35:47.451 INFO 22448 --- [ main]
> c.u.o.metrics.MyExampleApplication : build version is
> <0.0.1-SNAPSHOT> 2017-11-14 14:35:47.451 INFO 22448 --- [
> main] c.u.o.myexample.MyExampleApplication : value for custom key
> 'foo' is <bar>
group=com.xxx.examplesource
version=0.0.1-SNAPSHOT
gradleVersion=3.3
@Controller
@RequestMapping("/api")
public class WebController {
private final BuildProperties buildProperties;
public WebController(BuildProperties properties) {
buildProperties = properties;
}
@GetMapping("/test")
public String index() {
System.out.println(buildProperties.getVersion());
System.out.println(buildProperties.getArtifact());
System.out.println(buildProperties.getGroup());
System.out.println(buildProperties.getName());
System.out.println(buildProperties.getTime());
return "index";
}
}
springBoot {
buildInfo()
}