Skip to content
Advertisement

Replace external jar dependency with local intellij project

So I have an intellij IDEA project (project A) which includes a bunch of external jar libraries. I am currently working on one of those external jar libraries (project B) and I want to replace it with the local project (project B) on my computer.

So, in short: I have Project A which depends on jar B I want to replace jar B with my local project (project B)

That way, when I run project A, it uses Project B local rather than Jar B external. Anyone know any good easy ways of doing this?

Advertisement

Answer

Using Composite Builds

Your use case is exactly what composite builds in Gradle are made for. The docs even mention your precise use case:

Composite builds allow you to […] combine builds that are usually developed independently, for instance when trying out a bug fix in a library that your application uses

A composite build allows you to temporarily and easily replace an external dependency library with a locally available build project of that library. (It would even work with multiple different dependencies.)

Complete Example

Here’s how you’d set this up for your two projects (leaving out Gradle Wrapper files and Java source code for conciseness):

├── projectA
│   ├── build.gradle
│   └── settings.gradle
└── projectB
    ├── build.gradle
    └── settings.gradle

Note that the two project directories don’t actually have to be siblings. I’ve only done that here to keep it simple. In my minimal sample build, the two settings.gradle files can be empty. The two build scripts look as follows.

projectA/build.gradle

plugins {
    id 'java'
}

dependencies {
    implementation 'com.example:projectB:1.2.+'
}

repositories {
    // where you get the projectB dependency from normally
}

projectB/build.gradle

plugins {
    id 'java-library'
}

group = 'com.example'
version = '1.2.3'

Running the Sample

On the Command Line

Under projectA/, simply run ./gradlew --include-build ../projectB build (or any other Gradle task(s) you’re interested in). Make sure to use the right path to the directory of projectB. The --include-build option automatically creates the composite build on-the-fly.

In IntelliJ IDEA

You can also create a composite build in IntelliJ. To do that:

  1. Import projectA as usual (via its build.gradle file to make sure that IntelliJ automatically uses the Gradle configuration).
  2. In the Gradle tool window, you click the + button (“Link Gradle Project”) and select the build.gradle file of projectB.
  3. Right click projectA in the Gradle tool window and select “Composite Build Configuration”. In the opening dialog, select projectB and click “OK”.
  4. In the Gradle tool window, click the 🗘 button (“Reload All Gradle Projects”).

That’s it. You should now be able to use projectA with its locally available dependency projectB.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement