Search code examples
javagradledirectorydependencies

Gradle: include directory as dependency


I have multiple Gradle Java projects in which I am attempting to add the source code of a local project, whose root directory is /library, as a dependency that is a directory, just as I can add dependencies that are JARs. I tried using compile files('/library/src/main/java'): the packages were imported recursively, but the *.java files were left alone. Can I add /library/src/main/java as a dependency to the project? My goal is not having to recompile whenever I make changes to the library.


Solution

  • Gradle allows this through composite builds. To include the build /library, add includeBuild("/library") to the settings.gradle file of the dependent build. Then declare it as a dependency in build.gradle with correct group and name; an explicit version is not unnecessary. As it is now, Gradle will be unable to resolve this dependency, so finally substitute this dependency with the included build by changing includeBuild("/library") to

    includeBuild("/library") {
        dependencySubstitution {
            substitute(module: "<group>:<name>").with(project(":"))
        }
    }
    

    , which will cause the dependency declared earlier to be replaced by /library.

    After reloading the Gradle project with your IDE, the classes from /library should be available and any changes made to /library should be immediately visible to the dependent build.