java 如何使用 Gradle 和 Spring Boot 捕获构建信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47283048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to capture Build Info using Gradle and Spring Boot
提问by robbie70
I am trying to get access to build info values such as version
in my Java main application using Spring Boot and Gradle.
我正在尝试version
使用 Spring Boot 和 Gradle访问构建信息值,例如在我的 Java 主应用程序中。
I can't find any documentation / examples of how to configure the
我找不到有关如何配置的任何文档/示例
build.gradle
application.yml
(if required)Java main class
build.gradle
application.yml
(如果需要的话)Java main class
could someone please help with a small code example for the above files.
有人可以为上述文件提供一个小代码示例吗?
In my build.gradle
file I will have the version
entry, so how to get this into Java main class using Spring Boot and Gradle.
在我的build.gradle
文件中,我将有version
条目,那么如何使用 Spring Boot 和 Gradle 将其放入 Java 主类中。
build.gradle
构建.gradle
version=0.0.1-SNAPSHOT
I've tried adding
我试过添加
build.gradle
构建.gradle
apply plugin: 'org.springframework.boot'
springBoot {
buildInfo()
}
but the buildInfo()
isn't recognised as a keyword in Intellij
但buildInfo()
在 Intellij 中未被识别为关键字
In my Java main class I have the following:
在我的 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")
}
But when I run this - the output from env.getProperty("version")
is showing as null
.
但是当我运行它时 - 的输出env.getProperty("version")
显示为null
.
回答by Vampire
Spring Boot auto-configures a BuildProperties
bean with the information generated by buildInfo()
.
Spring BootBuildProperties
使用生成的信息自动配置bean buildInfo()
。
So to get the information use context.getBean(BuildProperties.class).getVersion();
.
所以要获取信息使用context.getBean(BuildProperties.class).getVersion();
.
回答by robbie70
I managed to get it working now - using the help pointer that Vampire gave below and some other sources. The key was adding the actuator class to the project dependency. Note: Intellj doesn't seem to recognise buildInfo() in the springBoot tag - but it does run ok - so don't be put off.
我现在设法让它工作 - 使用吸血鬼在下面提供的帮助指针和其他一些来源。关键是将执行器类添加到项目依赖项中。注意:Intellj 似乎无法识别 springBoot 标记中的 buildInfo() - 但它确实运行正常 - 所以不要推迟。
build.gradle
构建.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") + ">");
}
}
Screenshot of Console output when running the Application in Intellj
pasting the output as well incase the image doesn't display
粘贴输出以及柜面图像不显示
> 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>
UPDATE
更新
After reviewing this with my colleague we decided to move the some of the build properties, e.g. version
(above) out of the build.gradle
file and into gradle.properties
file. This gives us a cleaner separation for build details and properties. When you run Gradle build it automatically pulls these values in and they are available in the BuildProperties bean in the Java main class as shown in the example above.
在与我的同事之后,我们决定将一些构建属性,例如version
(以上)从build.gradle
文件中移到gradle.properties
文件中。这使我们可以更清晰地分离构建细节和属性。当您运行 Gradle build 时,它会自动提取这些值,并且它们在 Java 主类的 BuildProperties bean 中可用,如上例所示。
gradle.properties
gradle.properties
group=com.xxx.examplesource
version=0.0.1-SNAPSHOT
gradleVersion=3.3
回答by akshaya pandey
add following to your Gradle script.It inserts the version into the jar manifest correctly, as shown here:
将以下内容添加到您的 Gradle 脚本中。它会将版本正确插入到 jar 清单中,如下所示:
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
Your code will be able to pick up the version from that jar manifest file:
您的代码将能够从该 jar 清单文件中获取版本:
public class BuildVersion {
public static String getBuildVersion(){
return BuildVersion.class.getPackage().getImplementationVersion();
}
}
Refer the link below for more details: https://github.com/akhikhl/wuff/wiki/Manifest-attributes-in-build.gradle
更多详情请参考以下链接:https: //github.com/akhikhl/wuff/wiki/Manifest-attributes-in-build.gradle
回答by Vytautas
Easy way to get version number in Spring boot
在 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";
}
}
And dont forget generate application-build.properties
并且不要忘记生成 application-build.properties
springBoot {
buildInfo()
}