Home > Net >  How to reference local version of a project in another Gradle project dependencies
How to reference local version of a project in another Gradle project dependencies

Time:11-05

I have a Gradle project (say project A) that depends on another Gradle project (say B). Each project live in their own git repository. Sometimes when I have to make a non-trivial change across both projects, I will make the code changes in B and push to remote where our CI will pick it up and release it as a new version.

Then in project A, I will update the version for B with the new version number, then run ./gradlew generateLock saveLock and then I will be able to use the new changes made in B to implement the feature in A.

I want to avoid having to push B to remote and release a new version before I can start using the new code in A. Ideally, I want to make change to B locally and have A reference the local version of the project. That way I can test both and test the whole feature before I push any code. How can I achieve that using Gradle. I am using Gradle 7.5?

I declare my dependency on B in A's build.gradle as implementation 'com.something.project-b'. Note that B might have a bunch of other library dependencies of its own declared in its own build.gradle which should still be transitively pulled when I want to use the local version of B in A.

Any pointers?

There is a local JAR option in Gradle but that will not pull all the transitive dependencies of B and I do not want to build a combined JAR of B and all its dependencies and use that since then I lose all dependency resolution benefits in case of conflicts.

Also note that B is an independent project and cannot simply be moved to Project A as a sub-project

CodePudding user response:

in the project 'A'

settings.gradle

include ':prjB'
project (':prjB').projectDir = new File(settingsDir, '../relativePath')

build.gradle

def prjBExists = file('path-to-prjB').exists

dependencies {
...

   if (prjBExists) {
       implementation project(':prjB')
   } else {
      implementation 'g:a:v'
   }
}

PS - this referencing is under discussions as gradle-8 is expected to deprecate ...

CodePudding user response:

You don't need to do any sort of local workstation hodgepodge. All you need is the following:

  1. Declare mavenLocal() as the first repository in the dependent project (you probably already have mavenCentral().
  2. Publish the dependency to Maven local repository using the task .gradlew publishToMavenLocal.

Now, your dependent project can use the latest code from the dependency.

  • Related