Project dependencies getting wrong paths

I’m trying to make a project dependency that fits our directory structure setup, but I’m getting problems when gradle tries to resolve the libraries for compiling.

My setup is as follows:

ProjectA/
   Base/
        build.gradle:
            repositories {
                println "ProjectA.Base, pwd = ${new File('.').canonicalPath}"
                flatDir dirs: 'lib'
            }
            dependencies {
                compile ':mylib'
            }
  ProjectB/
    Base/
        settings.gradle:
            include "ProjectA.Base"
            project (':ProjectA.Base').projectDir = file('../../ProjectA/Base')
        build.gradle:
            dependencies {
                compile project(':ProjectA.Base')
            }

I can compile ProjectA fine from within itself, but when I try to build ProjectB I’m getting the error:

ProjectA.Base, pwd = /path/to/ProjectB/Base
:compileJava
    FAILURE: Build failed with an exception.
  * What went wrong:
Could not resolve all dependencies for configuration ':compile'.
> Could not find group:, module:mylib, version:
   Required by:
      :Base:unspecified > Base:ProjectA.Base:unspecified

so it seems when ProjectA is getting loaded as part of ProjectB’s compile phase, it’s trying to resolve them relative to ProjectB’s directory, not the dependant project.

How can i fix this? We don’t use repositories anywhere, it’s all flatDir loading, and all libraries are in the “lib” dir of the appropriate project.

Thanks for any help. Mark

Your debug output is misleading because ‘new File(someRelativePath)’ gets resolved based on the working directory of the Gradle process. You should always use ‘project.file()’ instead.

Gradle always uses the repositories of the project that resolves the configuration, even for transitive dependencies. This doesn’t map very well to having a separate lib directory per project. Possible solutions:

  • Add more repository declarations. For example, declare all repos in ‘allprojects {}’. * Use file-based dependencies (‘files(“lib/someLib.jar”)’) instead of ‘flatDir’ repositories.

Changing to “files(path)” has fixed it thanks.

I do have a follow up question though, I can’t seem to get the testCompile dependency to work. All my test jars (mockito etc) defined in the ProjectA/Base/build.gradle dependencies section are not being seen by ProjectB/Base.

I’ve got it configured like this:

(ProjectA/Base/build.gradle)

dependencies {
    ...
    compile files("lib/gson-2.2.1.jar")
    testCompile files("lib/mockito-all-1.8.5.jar")
    ... other test jars
}

(ProjectB/Base/build.gradle)

dependencies {
    compile project(':ProjectA.Base')
    testCompile project(':ProjectA.Base')
}

When I try to build ProjectB/Base, the compileTestJava task fails unable to find any of the mockito classes. If I change from “testCompile” to “compile” in ProjectA’s build script, projectB compiles and the tests run fine.

Is there something else I need to do to allow project B to pick up Project A’s testCompile jars?

Please search the forum for an answer to this frequent question, or post a new question.

ok, i’ll post a question. this question can be marked answered, thanks again.