Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this tutorial, we'll see how we can use multiple repositories in a Gradle project. This is useful when we need to use JAR files that aren't available on Maven Central. We'll also see how to use GitHub to publish Java packages and share them among different projects.

2. Using Multiple Repositories in Gradle

While working with Gradle as a build tool, we've often come across mavenCentral() in the repositories section of build.gradle. If we want to add other repositories, we can add them to the same section to indicate the source of our libraries:

repositories {
    mavenLocal()
    mavenCentral()
}

Here, mavenLocal() is used to look up all the dependencies in Maven's local cache. Any repository not found in this cache will be downloaded from Maven Central.

3. Using Authenticated Repositories

We can also use repositories that aren't publicly available by providing valid authentication. For example, GitHub and some other platforms provide a feature to publish our packages to its registry and later use it in other projects.

3.1. Publishing Packages to GitHub Package Registry

We'll publish the below class to the GitHub registry and later use it in another project:

public class User {
    private Integer id;
    private String name;
    private Date dob;
    
   // standard constructors, getters and setters
}

To publish our code, we'll need a personal access token from GitHub. We can create one by following the instructions provided in the GitHub docs. We then add a publishing task to our build.gradle file with our username and this token:

publishing {
    publications {
        register("jar", MavenPublication) {
            from(components["java"])
            pom {
                url.set("https://github.com/eugenp/tutorials.git")
            }
        }
    }
    repositories {
        maven {
            name = "GitHubPackages"
            url = "https://maven.pkg.github.com/eugenp/tutorials"
            credentials {
                username = project.USERNAME
                password = project.GITHUB_TOKEN
            }
        }
    }
}

In the above snippet, username and password are project-level variables supplied while executing Gradle's publishing task.

3.2. Using a Published Package as a Library

After successfully publishing our package, we can install it as a library from an authenticated repository. Let's add the below code in build.gradle to use the published package in a new project:

repositories {
    // other repositories
    maven {
        name = "GitHubPackages"
        url = "https://maven.pkg.github.com/eugenp/tutorials"
        credentials {
            username = project.USERNAME
            password = project.GITHUB_TOKEN
        }
    }
}
dependencies {
    implementation('com.baeldung.gradle:publish-package:1.0.0-SNAPSHOT')
    testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.0")
    // other dependencies
}

This installs the library from the GitHub Package Registry and allows us to extend the class in our project:

public class Student extends User {
    private String studentCode;
    private String lastInstitution;
    // standard constructors, getters and setters
}

Let's test our code using a simple test method:

@Test
public void testPublishedPackage(){
    Student student = new Student();
    student.setId(1);
    student.setStudentCode("CD-875");
    student.setName("John Doe");
    student.setLastInstitution("Institute of Technology");

    assertEquals("John Doe", student.getName());
}

4. Conclusion

In this article, we saw how we could use libraries from multiple repositories when working on a Gradle project. We also learned how to use GitHub Package Registry for authenticated repositories.

As always, the source code for the examples can be found over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!