Adding a source dependency to a Gradle build

Gradle allows adding source dependencies to a build since 4.10. Even though the process is quite simple, I stumbled upon a small problem when using them.

I wanted to add the Lazo library to a Gradle build at work. It follows the requirements for using a source dependency with Gradle:

  • uses the Gradle build system
  • no published jars (otherwise it would have been simpler)

Following the documentation

Following the blog post, I added the following:

  • update settings.gradle to define a source mapping:
settings.gradle
sourceControl {
    gitRepository("https://github.com/mitdbg/lazo") {
        producesModule("lazo:lazo")
    }
}
  • add a dependency in build.gradle:
build.gradle
dependencies {
    implementation("lazo:lazo") {
        version {
            branch = "master"
        }
    }
}
  • build the project (./gradlew build):
...
   > Git repository at https://github.com/mitdbg/lazo did not contain a project publishing the specified dependency.
     Required by:
         project :

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

Nice error message…

Adding the missing property

After some time fidling with different parameters and trying to understand why Gradle could not resolve the library, it turned out the error was with the Lazo configuration.

Most java packages follow a convention declared with the following properties:

  • groupId
  • artifactId
  • version

Gradle will use the same scheme, but:

  • groupId is just named group
  • artifactId is mapped to name
  • version has the same name

It turns out that Lazo missed the group property in its build. Gradle does not take into account empty groups, so I could not use :lazo to include the library.

The solution was to fork the library on my github account, and just add the group property.
Afterwards, I just had to replace https://github.com/mitdbg/lazo by https://github.com/jfyuen/lazo in settings.gradle above, and the builds finally succeeded.