DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Deploying a Kotlin App to Heroku
  • Auto Deploy Spring Boot App Using Gitlab CI/CD
  • How to Import the Gradle Spring Boot Application in Eclipse? | Spring Boot Tutorial [Video]
  • Microservices and DevOps Using Java, Spring Boot, Git Flow, Jenkins, and Docker

Trending

  • Introduction to ESP32 for Beginners Using the Xedge32 Lua IDE
  • The Promise of Personal Data for Better Living
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  • Supercharge Your Communication With Twilio and Ballerina
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot's Info Endpoint, Git and Gradle

Spring Boot's Info Endpoint, Git and Gradle

Lieven Doclo user avatar by
Lieven Doclo
·
Aug. 17, 14 · Interview
Like (0)
Save
Tweet
Share
8.24K Views

Join the DZone community and get the full member experience.

Join For Free

I’m a huge fan of Spring Boot. I really like how it has raised my level of productivity and the ease of adoption. I also use Git on a day to day basis. However, the integration of information of your Git repository in Spring Boot is not that straightforward if you’re using Gradle. Luckily it’s not that much work, as you’ll see. .

Spring Boot has a very handy feature called the actuator, which adds a lot of REST endpoints to your application which provide a lot of interesting information. One of those endpoints is the info endpoint. This endpoint return a JSON containing some information on your Spring Boot deployment, such as the application name and version, if you’ve configured them correctly in the application configuration. One of the other features of that endpoint is that when there is a git.properties file on the root of the deployment, it’ll use the values in there to display some git information in that JSON as well.

However, in the documentation it’s only documented how you can do this with Maven (it has a plugin for that) and only has a vague pointer to the gradle-git plugin (while mentioning it’ll be a bit more work). Now, writing a Gradle plugin that generates that git.properties wasn’t that hard at all.

I haven’t had the time to put this code in a standalone plugin, but it shouldn’t be that hard to do so.

First, I made a buildSrc folder in my project root so I could write the plugin. Then I added a small build.gradle in there to add a dependency to GrGit, which is a great0 library that integrates Gradle with Git.

 apply plugin: 'groovy'

repositories {
    jcenter()
}

dependencies {
    compile("org.ajoberstar:gradle-git:0.9.0")
}

Then I just had to write the plugin. I wanted to have the generation to run whenever the JavaPlugin’s classes task was run, as this should cover most JVM-oriented builds (most require the Java plugin).

import org.ajoberstar.grgit.Grgit
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.TaskAction

class GitPropertiesPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        def task = project.tasks.create('generateGitProperties', GenerateGitPropertiesTask)
        task.setGroup(BasePlugin.BUILD_GROUP)
        ensureTaskRunsOnJavaClassesTask(project, task)
    }

    private void ensureTaskRunsOnJavaClassesTask(Project project, Task task) {
        project.getTasks().getByName(JavaPlugin.CLASSES_TASK_NAME).dependsOn(task)
    }

    static class GenerateGitPropertiesTask extends DefaultTask {
        @TaskAction
        void generate() {
            def repo = Grgit.open(project.file('.'))
            def dir = new File(project.buildDir, "resources/main")
            def file = new File(project.buildDir, "resources/main/git.properties")
            if (!dir.exists()) {
                dir.mkdirs()
            }
            if (!file.exists()) {
                file.createNewFile()
            }
            def map = ["git.branch"                : repo.branch.current.name
                       , "git.commit.id"           : repo.head().id
                       , "git.commit.id.abbrev"    : repo.head().abbreviatedId
                       , "git.commit.user.name"    : repo.head().author.name
                       , "git.commit.user.email"   : repo.head().author.email
                       , "git.commit.message.short": repo.head().shortMessage
                       , "git.commit.message.full" : repo.head().fullMessage
                       , "git.commit.time"         : repo.head().time.toString()]
            def props = new Properties()
            props.putAll(map)
            props.store(file.newWriter(), "")
        }
    }
}

Now I only need to add the plugin to the project and it’ll be automatically executed when I build the project.

apply plugin: GitPropertiesPlugin

That really wasn’t that hard…

Spring Framework Spring Boot Git Gradle

Published at DZone with permission of Lieven Doclo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Deploying a Kotlin App to Heroku
  • Auto Deploy Spring Boot App Using Gitlab CI/CD
  • How to Import the Gradle Spring Boot Application in Eclipse? | Spring Boot Tutorial [Video]
  • Microservices and DevOps Using Java, Spring Boot, Git Flow, Jenkins, and Docker

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: