emiles
emiles

Reputation: 779

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.

Upvotes: 53

Views: 90493

Answers (6)

Supun Sandaruwan
Supun Sandaruwan

Reputation: 2418

This is the latest solution that I found, In my case, there are two separate projects. lets called those projects Main and Library.

=== Library project configs changes. ===

1. Add these changes to the build.gradle file.

plugins {
    id 'maven-publish'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            name = "localMaven"
            url = uri("${System.properties['user.home']}/.m2/repository")
        }
    }
}

2. Publish the JAR to your Local Maven Repository

./gradlew publish

=== Main project configs changes. ===

1. Add these changes to the build.gradle file.

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation 'com.supun.app:photo-library:0.0.2-SNAPSHOT'
}

2. Publish the JAR to your Local Maven Repository

./gradlew build

Upvotes: 0

DAflamingFOX
DAflamingFOX

Reputation: 31

I know this is old but I recently just had the same problem, and here is my solution:

Assuming this file tree:

.
|- app
|  |- src
|  |  |- ...
|  |- build.gradle
|
|- lib
|  |- src
|  |  |- ...
|  |- build.gradle
|
|- settings.gradle

You can set up your gradle project as such:

  • ./settings.gradle
rootProject.name = 'some-project'
include 'app'
include 'lib'
  • ./app/build.gradle
// ...
dependencies {
    // depend on sibling module lib
    implementation project(':lib')
}
// ...

Lastly, make sure that what you are tying to import from the lib module is not in the base package.

Upvotes: 3

OneCricketeer
OneCricketeer

Reputation: 191681

Local Modules

This is a pattern followed by most Gradle projects where there is a library, then a sample app that uses that library - ref Gradle docs

 - app/
    - build.gradle
    - src/main/java  # where your main class would be 
 - library/
    - build.gradle
    - src/main/java  # dependencies of the app module 
 - settings.gradle
 - build.gradle

In that top-level settings.gradle you have

include ':library', ':app'

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

dependencies {
    compile project(':library')
}

Basically, the top-level build.gradle, is a wrapper for all common configs of the sub projects and variables. For example, it's most commonly used for a repositories { } section for Maven urls, for example. Full details on that are at Gradle - Multi-project builds

Remotes Modules

The above is fine for working locally, but let's say you wanted to share your repo with many other developers without making them download extra source code. Then your would publish the other libraries to a remote server.

This is what you already doing when you add these lines to your project

implementation("org:artifact:version")

If your projects are public on GitHub, use a service like jitpack.io. You can also setup an account on Bintray OSS or Maven Central to have your libraries be available like most others.

If your projects are private within your company, you will need some Maven type server, whether that is a generic web server, or Nexus or Artifactory, you can add that with an addition to the repositories block.

repositories {
    maven { url "http://some.maven.site/" }
}

Then add the compile or implementation sources, as normal

Upvotes: 83

Pratik Agrawal
Pratik Agrawal

Reputation: 49

Simplest way is just to keep lib folder at parent dir of project and include below line in your module build.gradle file

implementation fileTree(dir: '../libs', include: ['*.jar'])
implementation fileTree(dir: '../libs', include: ['*.aar'])

Upvotes: 2

Richard Tingle
Richard Tingle

Reputation: 17226

This answer is outdated in modern gradle and intellij, please refer to darwinbaisa's answer

Original answer

Its discussed in the article https://blog.jetbrains.com/idea/2016/10/intellij-idea-2016-3-eap-gradle-composite-builds-and-android-studio-2-2/

Basically you need to use composite builds. So View > Tool windows > Gradle > Right Click on the project using the library > Composite Build Configuration > Tick the library project you want to use the local version of.

Do a rebuild and then the local dependancy should be used

Upvotes: 0

darwinbaisa
darwinbaisa

Reputation: 688

Finally Gradle 3.1 has sorted out this issue. Composite builds are now supported natively. More here. In short add this line to sandbox settings.gradle file-

includeBuild '<PATH>/myLib'

If you can't upgrade Gradle, then the only hope for you is to publish mylib artifact to local maven repo and add mavenLocal() to sandbox/build.gradle.

Upvotes: 12

Related Questions