Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Last active April 28, 2020 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p120ph37/f85677b102aa9247159dd9233769fbea to your computer and use it in GitHub Desktop.
Save p120ph37/f85677b102aa9247159dd9233769fbea to your computer and use it in GitHub Desktop.
Using BuildInfo in Gradle without Spring Boot
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE' apply false
}
// Doc: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#integrating-with-actuator-build-info
// same as: springBoot { buildInfo { ... } }
task bootBuildInfo(type: org.springframework.boot.gradle.tasks.buildinfo.BuildInfo) {
classes.dependsOn bootBuildInfo
destinationDir new File(sourceSets.main.output.resourcesDir, 'META-INF')
properties {
artifact = project.archivesBaseName // Would normally get autodetected from BootJar
}
}
// Trivial example app which loads the properties from classpath.
// Run app like this: java -jar build/libs/build-info-example-1.0.0.jar
def mainClass = 'BuildInfoExample'
sourceSets {
main {
java {
srcDirs = ['.']
include "${mainClass}.java"
}
}
}
jar {
manifest {
attributes('Main-Class': mainClass)
}
}
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
public class BuildInfoExample {
public static void main(String[] args) throws IOException {
Properties buildInfo = new Properties();
buildInfo.load(BuildInfoExample.class.getClassLoader().getResourceAsStream("META-INF/build-info.properties"));
for(Entry<Object, Object> entry : buildInfo.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
baseName=build-info-example
group=com.github.gist.p120ph37
version=1.0.0
rootProject.name = 'build-info-example'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment