Search code examples
javagradleproject-managementproject-structuremulti-project

How would one configure a Gradle project in which a subproject depends on the main project?


I have a mini-application in my source code which I am trying to move to a separate subproject. However, the mini-application depends on the main project. How would one set up a build.gradle file to reflect this?

Here is my directory structure:

/
    src/
        main/java/<main project source>
    build.gradle
    settings.gradle
    subproject/
        src/
            main/java/<subproject source>
        build.gradle

I was thinking that one could do something like this in the build.gradle file for the subproject:

build.dependsOn ":build"

But that doesn't seem to work.


Solution

  • You need to set up compile dependencies correctly for this to work.

    The statement build.dependsOn ":build" will only set up task dependencies (i.e. that :subproject:build is executed after main project ":build"). This is probably not necessary, main project build is executed before subproject builds anyway. It will not set up compile dependencies (i.e. which classes/libraries to include).

    Instead (or in addition) add a compile dependency to your main project in your subproject/build.gradle file:

    dependencies {
        compile project(':')
    }