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 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.)
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.)
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.gradleplugins {
id 'java'
}
dependencies {
implementation 'com.example:projectB:1.2.+'
}
repositories {
// where you get the projectB dependency from normally
}
projectB/build.gradleplugins {
id 'java-library'
}
group = 'com.example'
version = '1.2.3'
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.
You can also create a composite build in IntelliJ. To do that:
build.gradle file to make sure that IntelliJ automatically uses the Gradle configuration).build.gradle file of projectB.That’s it. You should now be able to use projectA with its locally available dependency projectB.
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.