gradle triangle dependency on another project

421 views Asked by At

I spent a lot of time to resolve my as I thought pretty usual project structure, but couldn't find any appropriate solution.

Project structure:

some_local_dir_with_git_repos
    git_repo_of_project_A/
         build.gradle
    git_repo_of_project_B/
         build.gradle
    git_repo_of_project_Core/

Projects A and B should include Core sources.

"some_local_dir_with_git_repos" is not under Git, so I don't want to add any settings.gradle into it.

Is there a graceful solution?


ADDED:

Thanks to @peter-niederwieser and link he provided (with his answer in it - https://stackoverflow.com/a/20807550/1336772).

I made my settings.gradle of A and B projects looks like that:

include ':Core'
project(':Core').projectDir = new File('../git_repo_of_project_Core/')

And added these lines to build.gradle:

dependencies {
    compile project(':Core')
}

After 'File -> Invalidate caches / Restart' (resolves 'cannot resolve symbol' issue in Android Studio) and 'Build -> Rebuild project' (resolves ClassNotFoundException issue) it works fine for me.

2

There are 2 answers

2
Peter Niederwieser On

A good guideline is to build together what's versioned together. So if you have three Git repositories, you'd have three builds, and they would exchange artifacts via a binary repository (e.g. Artifactory or Nexus).

If you must have one build spanning multiple Git repositories, some options are to create an uber repository using Git submodules, to pass the location of settings.gradle via --settings-file, or to run the build from the directory containing settings.gradle. Also, you may have to configure the locations of project directories as shown in defining subprojects in a deeply nested directory tree.

0
akhikhl On

Including sources of one repository into another means that something is wrong with the infrastructure. As @peter-niederwieser mentioned, it is normal to reuse code on the level of binary artifacts (not source code).

Typically you would do "gradle build install" on git_repo_of_project_Core and "gradle build" on git_repo_of_project_A and git_repo_of_project_B. Meaning of "build" and "install" tasks:

You will probably want to automate compilation/installation of multiple projects coming from multiple git-repositories. Gradle is a perfect tool for that (IMHO), because it is project-nature-agnostic and extensible with plugins. Two features are especially useful for multiple-repositories setup:

  • GradleBuild task, which allows gradle script to start another build (or even arbitrary tasks) against the specified directory or "build.gradle" file.

  • org.ajoberstar:gradle-git gradle plugin, which implements set of gradle tasks, dealing with git.

Please, have a look at multiproject-git-gradle script ( https://github.com/akhikhl/multiproject-git-gradle ) - it implements powerful DSL for configuring multiproject gradle/git setup. It can save you a lot of time and effort :)