emiles emiles - 1 month ago 17
Java Question

IntelliJ gradle add module dependency

Using IntelliJ 2016.2.

Using Gradle 2.14.1

I have 2 projects, each with their own build.gradle files and separate directory structures:

myLib (meant to be jarred and used by others)
- build.gradle
- settings.gradle
- src/main/java/...

sandbox (spring boot web app)
- build.gradle
- settings.gradle
- src/main/java/...
- src/main/resources/...


Hopefully, you get the picture. Within IntelliJ, I have the following module structure, at the same level (no subprojects):

- myLib
- sandbox


Simple request ... I want to use
myLib
within the
sandbox
app. I want both modules in the same project in order to develop both.

I've tried adding a module dependency to
sandbox
for
myLib
within IntelliJ. No dice. I've tried adding a jar reference, no dice.

I believe I need to add a dependency in the build.gradle file but cannot figure out how. I've tried
compile files '<path to myLib.jar>'
, etc. No dice.

Answer

This is a pattern followed by most Android projects where there is a library, then a sample app that uses that library, so I think it's applicable for what you are asking.

 - module/
    - build.gradle
    - src/main/java
 - library/
    - build.gradle
    - src/main/java
 - settings.gradle
 - build.gradle

In that top-level settings.gradle you have

include ':library', ':module'

And in the module/build.gradle, you compile that included project

dependencies {
    compile project(':library')
}

I'm not entirely sure what goes into the top-level build.gradle, but it's mostly common things like a repositories { } section, for Maven, for example. Full details on that are at Gradle - Multi-project builds