Open In App

Adding JGit to the project with Gradle

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JGit is the lightweight and pure Java library implementing the Git version control system. It can be widely used for integrating Git functionalities into Java applications without relying on the native Git command line tools. In this article, we will explore the process of adding JGit to the Gradle project covering all the necessary steps and providing a detailed explanation.

JGit can operate independently of the system's Git installation as it is the pure Java implementation of the Git version control system. It can make it an ideal choice for the Java project where direct access to the Git repositories is needed without relying on the installed Git binaries. JGit comes with features that allow you to manage the repositories and handle the various Git operations like commit, fetch, merge, and rebase, and access the repository history.

Implementation of Adding JGit to the project with Gradle

Below is the implementation to add JGit to our project with Gradle build tool.

Step 1: Create the Maven project

Create the maven project using Intelij Idea and on on creating the project, choose the build system as Gradle and click on the create button.

Maven Project Creation


After the project creation done successfully, the file structure will look like the below image.

Folder Structure


Step 2: Add the JGit Dependency

Open the build.gradle.kts file and add the JGit dependency into the project.

// https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '6.10.0.202406032230-r'


Add dependency to build.gradle.kts file:

plugins {
    id("java")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.10.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    // https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit
    implementation("org.eclipse.jgit:org.eclipse.jgit:6.10.0.202406032230-r")

}

tasks.test {
    useJUnitPlatform()
}

Now, we have successfully added the JGit dependency into the Java project with Gradle. Now, we are working on th JGit functionalities into the Java project.

Step 3: Refresh the Gradle project

After updating the build.gradle.kts file, we need to refresh the project to allow the Gradle to download the newly added dependencies into the project. We need to manual update through the command line use the below command.

./gradlew build

Step 4: Using the JGit

With the JGit added into the project, we can start using it perform the Git operations.

Example:

This is a simple example of opening the existing repository and printing the latest commits.

JGitExample.Java:

Java
package org.example;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import java.io.File;

public class JGitExample {
    public static void main(String[] args) throws Exception {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        try (Git git = new Git(builder.setGitDir(new File("c:/Users/Mahesh/Desktop/git-repo/.git")).readEnvironment().findGitDir().build())) {
            System.out.println("Latest Commit: " + git.log().setMaxCount(1).call().iterator().next());
        }
    }
}

This code demonstrates the use of JGit to interact with a local Git repository. It builds a Git object pointing to a specific repository directory and prints the latest commit message from the repository.

Output:

Display the latest commit in the specified Git repository

This integration can be particularly useful for the applications that requires the dynamic interaction with Git repositories.


Next Article
Article Tags :
Practice Tags :

Similar Reads