How to Use Git Repository as Gradle Package

Introduction

Reason why I'm writing this is because I found a very good library called GraphQL SPQR. It aims for rapid development of GraphQL services. Developers of this library also provides another library for easier integration with Spring Boot. But it is still in alpha stage

I know it isn't a wise decision of using such a library in production. But its simplicity and usability really tempts me using it. Recently it adds an exciting feature of supports Spring Data paging out of the box. But no new version was released.

As a workaround, I figured out several ways of using its Git repository directly in my project.

Easier way

This approach, actually, is not what I found in the first place. But it's really simple, compared with built-in gradle feature. To achieve this, you need to use JitPack.

Firstly, copy and paste the address of the repository, then select the releases or branches, even specify a commit you want to use. Then you will see the steps.

In my cases, I need to do the following steps:

  1. Add the JitPack repository
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
  1. Add the dependency, here I choose to follow the master branch.
dependencies {
        implementation 'com.github.leangen:graphql-spqr-spring-boot-starter:master-SNAPSHOT'
}

Voila!

More direct approach

Another way is to use Gradle's source dependencies feature.

In settings.gradle add the following lines to declare the source.

sourceControl {
    gitRepository("https://github.com/leangen/graphql-spqr-spring-boot-starter.git") {
        producesModule("io.leangen.graphql:graphql-spqr-spring-boot-starter")
    }
}

Finally, include dependency in build.gradle

implementation('io.leangen.graphql:graphql-spqr-spring-boot-starter') {
	version {
		branch = 'master'
	}
}

In my cases, I got an error message after re-import the project

Warning:root project 'pm': Unable to resolve additional project configuration.
Details: org.gradle.api.artifacts.ResolveException: Could not resolve all dependencies for configuration ':compileClasspath'.
Caused by: java.lang.RuntimeException: Problems reading data from Binary store in /tmp/gradle3227785654071705780.bin offset 50827 exists? true
Caused by: java.lang.IllegalStateException: Corrupt serialized resolution result. Cannot find selected module (null) for compileClasspath -> io.leangen.graphql:graphql-spqr-spring-boot-starter

After looking up the documents, I found that this approach is only applicable with repositories using the Gradle build tool. And there is no ways of forcing Gradle use compileMavenProject to compile in my current understanding.

If you have any new ideas, please comment below for discussion. Any ideas will be appreciated.

GitHub Package approach

In addition, I found this while solving the previous problem. But this approach needs the developer publish the package on GitHub Package. This is pretty simple and I think is a good way to include package in a private repository. More details can be seen here

Conclusion

Comparing these approaches, I found the most convient way is to use JitPack. The second approach will be great for teams using Gradle as build tools and have their private Git service while not having a package registry.

One more thing

If you happens to configure the package mentioned above, you will noticed an error of cannot getting one of its transitive dependency due to version mismatch. This can be resolved by adding a constraint.

implementation 'com.github.leangen:graphql-spqr-spring-boot-starter:master-SNAPSHOT'
constraints {
	implementation('io.leangen.graphql:spqr:0.10.0') {
		because 'This is a workaround of using jitpack graphql-spqr-spring-boot-starter'
	}
}

For certain this workaround shouldn't be used in production. However, if you are in the early stage of development and confident about the future of a package, which also in the early stage. You can try this technique to speed up your development using the newest feature provides in the package.