Compile gradle project with another project as a dependency

imagesHaving project dependant on another project is common situation. How to configure gradle so that it will include your dependency project in build process?

There are two cases:

1. Your project is a root project and dependency is under its root

When dependand project is under root in a directory structure (dependency is not being shared with any other project except this one) you are doing it in recommended gradle strategy :)

To wrap up: you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
  |--Dependency
  |    |--build.gradle

Then to add Dependency to Project, you need to have Project/settings.gradle content like this:

include ':Dependency'

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

2. You have two independent projects and you need to use one of them as a dependency

When both project are on the same level in a directory structure you are not doing it as gradle team wish, but it is more real world situation in my opinion :) This is because you have your library project that can be easily used in several other projects as a dependency.

So you have directory structure like this:

Project
  |--build.gradle
  |--settings.gradle
Dependency
  |--build.gradle

To add Dependency to the Project, you need to include it, and show Dependency path manually. So the Project/settings.gradle content should be like this:

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

and in a Project/build.gradle dependencies section you need to compile the dependent project by adding:

dependencies {
   compile project(':Dependency')
}

Notice that tis is build.gradle is exactly the same as in previous section.

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!

56 thoughts on “Compile gradle project with another project as a dependency

  1. Thank you very much for this article. In fact, it was quite easy to find the line defining the project directory in the gradle documentation, but … I was stuck because I was trying to include the project *after* defining the project directory (it seemed logical to me). Including the project first and then defining its directory did the trick, thank you.

  2. Thank you! This works for one project depending on another. But when I use it on three project transitive dependency, the build was a failure on the secondary dependency. Is there a way to this?

    1. I did not use any – try to google the error you get. Also you can try to build each project independently – firs the one the most nested, then the one that uses it and then the one that uses the second one. Just to make sure that all dependencies compile well

  3. thanks for this, but it’s depressing that gradle doesn’t do this automatically like maven eclipse:eclipse does. if you add a bitcoin or changetip donation link i’ll use it.

    1. I use PayPal as a donation service. Find it on a sidebar.

      Gradle projects should be organized in a tree structure. Maybe then dependency tangling is easier

  4. Thanks man! It was very helpful I was having some trouble trying to configure my dependencies around here.

  5. I got option 2 to work, but there’s a slight inconvenience. When i run a task (for example ‘gradle build’) in Project then it runs the same task in Dependency as well. Is there a way to stop that (exclude Dependency subproject task from being executed when executing the same task in Project)?

  6. if i have a 3rd party dependency on my dependent project
    i.e compile(‘org.springframework.security:spring-security-core:3.2.4.RELEASE’)

    dependency version get changed in Root project
    i.e it gets changed to org.springframework.security:spring-security-core:4.0.4.RELEASE

    Is there any way to fix it ?

  7. The snowflakes is so annoying, and make reading very difficult. (Eyes keep focusing on moving objects)

  8. Great post!

    P.S.: I would like to make a micro-donation, but I see you don’t have a public bitcoin account :( I think it’s the easier way for micro-payments. No need to sign or give a credit-card or whatever.

  9. Thanks, It was very helpful.

    Is there a way to run “gradle install” command from eclipse (I don’t see it as an option there)

    I want to save the jar in my mavenLocal repo

  10. This tip is an essential in (as you say) real-world situations.

    I have added the following snippet to my settings.gradle file as follows. It just processes a list of projects from a main project used by test driver programs

    [ ‘:test-help’, ‘:channels’, ‘:app_config’ ].each {
    include “${it}”
    project(“${it}”).projectDir = new File( settingsDir, “../relay/${it[1..-1]}”)
    }

    This is a big time-saver.

  11. Pingback: Btc usd rate chart
  12. Pingback: pa divorce forms
  13. Pingback: convert2mp3 ios
  14. Pingback: Mini rf connector
  15. I just tried your method and it’s not working for me, because the :Dependency project uses some of the same plugins as the main project and it throws an error when trying to apply a plugin that’s already been applied in the main project.

    Error resolving plugin [id: ‘org.springframework.boot’, version: ‘2.2.6.RELEASE’, apply: false]
    > Plugin request for plugin already on the classpath must not include a version

    Do you have any advice on how to overcome this?

    1. seems you have `springboot` plugin defined in two `build.gradle` configs. This is why it is already on your classpath. And you declare `version` in both of the places. Try removein `version` declaration where it is redundant.

  16. How to exclude it, I tried different ways, but it doesn’t work. Can you please help me on that?

  17. Pingback: ao thanh nien
  18. I was able to do this, but unfortunately the plugins have duplicity issues with error like:
    > Plugin request for plugin already on the classpath must not include a version

Give Your feedback: