Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate build-info.properties in the IntelliJ "out" directory on debug/run for a Spring Boot project?

In my build.gradle, I have added spring build info:

springBoot {
    mainClass = "${springBootMainClass}"

    buildInfo() {
        additionalProperties = [
                name: "${appName}",
                version: "${version}-${buildNumber}",
                time: buildTime()
        ]
    }
}

def buildTime() {
    final dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ")
    dateFormat.timeZone = TimeZone.getTimeZone('GMT')
    dateFormat.format(new Date())
}

When I run from the command line, this correctly adds the /META-INF/build-info.properties file into /build/resources/main so that the "/info" endpoint shows the build information in JSON.

When I run from IntelliJ's run/debug button, IntelliJ does not use the /build directory but instead uses the /out directory and also does not run that gradle task, so the /info endpoint has empty JSON.

How can I make it generate that file and put it in the /out directory?

like image 493
Don Rhummy Avatar asked Nov 28 '17 22:11

Don Rhummy


People also ask

How do I get Spring framework in IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Plugins. Open the Installed tab, search for Spring and make sure that the checkboxes next to all relevant plugins are selected.

What is build in IntelliJ?

The Build tool window helps you view the output of your builds that were done by IntelliJ IDEA, delegated to Maven or Gradle, and the results of the projects' synchronization.


2 Answers

Enable Delegate IDE build/run actions to Gradle option in Settings (Preferences) | Build, Execution, Deployment | Build Tools | Gradle | Runner tab.

In latest IDE versions set Gradle for the Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Build and run using option.

like image 98
Andrey Avatar answered Oct 22 '22 09:10

Andrey


I have exactly the same need, and I don't want to use "delegate IDE build/run action to Gradle" for different (good) reasons.

In my case I don't need that file /META-INF/build-info.properties to be up-to-date in IDEA during develop phase, I just need this file to be available in the "run" classpath (under /out/...), else Spring won't be able to create and inject the BuildProperties bean when launching my app from IDEA run/debug tool.

So here is a simple solution if your are also in my case:

  1. create a dummy "dev" (or "snapshot") version of the build-info.properties in the main resources directory ( src/main/resources/META-INF/build-info.properties )

    #DO NOT EDIT (will be overriden by Spring boot plugin ) build.time=2019-05-07T11:32:31.581Z build.artifact=myapp build.group=org.mycompany build.name=myapp build.version=dev

This dev version will be automatically copied into IDEA /out/production/resources directory when building project from IDEA

  1. Create a task dependency between bootBuildInfo task and processResources tasks, to make sure that Spring boot plugin will override the "dev" version with up-to-date version when building the app jar:

    bootBuildInfo.mustRunAfter processResources

This way, SpringBoot gradle plugin will override the file copied from sources by processResources task, with its auto-generated up-to-date file.

EDIT 2020-10

As Dmitry warned in comment below, this solution will break Gradle incremental build, as both processResources and bootBuildInfo tasks share the same output file build-info.properties. To avoid that, we can add exclusion in the processResources to filter out this file, when we detect that tasks graph contains bootBuildInfo tasks

project.gradle.taskGraph.whenReady {
    if (it.allTasks.any {it.name == "bootBuildInfo"}) {
        processResources {
            exclude("META-INF/build-info.properties")
        }
    }
}
like image 37
M.Ricciuti Avatar answered Oct 22 '22 09:10

M.Ricciuti