Reference external project as dependancy?

Hi Everywhere I have looked in the userguide, all the project dependencies must be in the same project tree. A root project must exist, and then subprojects existing within the directory (or sub-sub projects I guess) of the root project.

Is there a way where I can define a project elsewhere in the filesystem, and then reference it as a project dependency from a project without being locked to the root > subproject directory structure?

A project can live anywhere on the file system. The logical project structure is always a tree; the physical project directory structure is arbitrary. See ‘Settings’ in the Gradle Build Language Reference for how to configure project directories. I’m pretty sure it’s also explained in the Gradle User Guide (see the chapter on multi-project builds).

If i might add a word here.

As far as i understand the DSL reference it is not possible to do this because the only possibility to add Projects is through the include or includeFlat methods, which evaluate the file path relative to the directory where the settings.gradle reside and its parent, respectively.

But if you leave the API behind, you could use the createProjectDescriptor method of the Settings implementation:

Settings.gradle:

def projectADir = file('../projectA')
def projectBDir = file('../projectB')
  def rootProjectDescriptor = settings.rootProject
  settings.createProjectDescriptor(rootProjectDescriptor, 'projectA', projectADir)
settings.createProjectDescriptor(rootProjectDescriptor, 'projectBFake', projectBDir)

‘include’/‘includeFlat’ default to hierarchical/flat directory layouts, but this can be customized arbitrarily, using only public APIs. For example:

include("foo")
  project(":foo") {
    projectDir = new File("$settingsDir/../../bar")
}

See ProjectDescriptor for further configuration options.

1 Like

Got it. Thank you very much.