'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?



Solution 1:[1]

  1. Open module A in IntelliJ IDEA;
  2. Go to File-> Project Structure -> Modules -> module A -> Dependencies (Tab) -> find and delete B.jar (there is "-" button for that);
  3. Next click "+" button and add folder which contains compiled classes of your local module B.

Solution 2:[2]

Since you have the tag "gradle", I'm assuming you are using it for both projects. Run "gradle publishToMavenLocal" in project B and then refresh the dependencies of project A.

Just make sure you remember you did this and delete project B from your local repository when you are done with it.

Solution 3:[3]

Solution with gradle:

In Project A build.gradle add mavenLocal() to the repositories.

...
repositories {
  mavenLocal()
  maven { url ... }
}
...

In Project B you need the maven-publish plugin, and the related task.

plugins {
  id 'maven-publish'
}

sourceSets {
  main {
    java {
      srcDir 'src/main/java'
    }
  }
}

task sourcesJar(type: Jar) {
  from sourceSets.main.allJava
  classifier = 'sources'
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      artifactId = rootProject.name //or some string eg.: 'projectb'
      groupId = project.group //or some string eg.: 'com.example'
      version = project.version //or some string eg.: '1.0.3.9'
      from components.java
      artifact sourcesJar
    }
  }
}

Now you can run the publishToMavenLocal task from your terminal, or from Gradle View in IntelliJ. (It is on the right side by default, gradle tab, expand your project and bellow publishing you can find the task.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aliaksandr Veramkovich
Solution 2 Mike Brazinski
Solution 3