Now, what about mavenLocal?
Up to here, everything works as expected. Yet, you may come across some Gradle build scripts with a repositories block that would read as follows:
At first, this looks sensible: don’t query the internal or central repository if the dependency is already present in the local repository. Whatever “the local repository” may actually mean…
It turns out that the
mavenLocal() repository is not the Gradle dependency cache. It is a repository where you can deploy components with
./gradlew publishToMavenLocal (assuming you use the
maven-publish Gradle
plugin ). Now, why would you like to deploy to a local repository? My take is, you don’t.
The typical case where I would consider using a local repository is to test a change in a shared library without publishing it to a repository visible to other people than me, so that I’m sure of what I publish, when I publish it. However, there are better ways to achieve that.
First case: the shared library that I want to test is in the same Gradle build as the code that uses it. (Let me remind that in Gradle parlance, a 'build' is the code base that is considered when running a command such as
./gradlew build. This is what Eclipse refers to as a workspace, or IntelliJ IDEA as a project.) In that case, where the common DTOs library is built from, say, a
common-dtos subfolder of the project root, specifying the dependencies of the application
as a project dependency ) is enough:
Second case: the shared library is in a different Gradle build, typically its source code is in a different Git repository (let us name it
common-components). Then I will use the
composite build feature to instruct Gradle to use the version of the common components that I cloned, rather than the one from the repository, when building my application. To this end, assuming that I have cloned the two Git repositories next to each other on my disk (if not, it’s just a matter of setting up the right relative path), I simply have to add the following line in the
settings.gradle file at the root of of my application: