Last active
April 28, 2020 20:28
-
-
Save p120ph37/f85677b102aa9247159dd9233769fbea to your computer and use it in GitHub Desktop.
Using BuildInfo in Gradle without Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
baseName=build-info-example | |
group=com.github.gist.p120ph37 | |
version=1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rootProject.name = 'build-info-example' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment