如何使用Gradle和Sping Boot 捕获构建信息

qoefvg9y  于 9个月前  发布在  其他
关注(0)|答案(4)|浏览(88)

我正在尝试使用Sping Boot 和Gradle访问Java主应用程序中的构建信息值,例如version
我找不到任何关于如何配置

  • build.gradle
  • application.yml(如果需要)
  • Java main class

有人可以请帮助与上述文件的一个小的代码示例。
在我的build.gradle文件中,我将有version条目,所以如何使用Sping Boot 和Gradle将其导入Java主类。

build.gradle

version=0.0.1-SNAPSHOT

我试着把

build.gradle

apply plugin: 'org.springframework.boot'

springBoot {    
    buildInfo() 
}

但是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")     
    }

但是当我运行这个命令时,env.getProperty("version")的输出显示为null

xvw2m8pv

xvw2m8pv1#

Sping Boot 使用buildInfo()生成的信息自动配置BuildProperties bean。
因此,要获取信息,请使用context.getBean(BuildProperties.class).getVersion();

y53ybaqx

y53ybaqx2#

我设法让它现在工作-使用帮助指针,吸血鬼给下面和其他一些来源。关键是将执行器类添加到项目依赖项中。注意: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") + ">");
    }

}

在Intellj中运行应用程序时控制台输出的屏幕截图x1c 0d1x

  • 粘贴输出以及以防图像不显示 *
> 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>

更新

在与我的同事一起审查之后,我们决定移动一些构建属性,例如。version(上图)从build.gradle文件输出到gradle.properties文件。这给了我们一个更清晰的构建细节和属性的分离。当你运行Gradle build时,它会自动拉入这些值,它们在Java主类的BuildProperties bean中可用,如上面的示例所示。

gradle.properties

group=com.xxx.examplesource
version=0.0.1-SNAPSHOT 
gradleVersion=3.3
os8fio9y

os8fio9y3#

在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();
    }
}

请参阅下面的链接了解更多详情:https://github.com/akhikhl/wuff/wiki/Manifest-attributes-in-build.gradle

23c0lvtd

23c0lvtd4#

在Sping 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()
}

相关问题