1

We have 2 java multi-module (composite) projects (that lie in 2 repositories) built using Gradle and maven. The Gradle project depends on the maven project and we need to get the changes from the maven project reflected in Gradle project. Gradle project is referring to the maven project via local maven repo using repositories > mavenLocal > content filtering. (Even though Gradle discourage referring to mavenLocal, we don't have a better option here)

repositories {
  mavenLocal {
     content {
        includeGroupByRegex "REGEX"
     }
  }
  maven {
     url "REPO_URL"
  }        
}

The changes maven project is reflected in gradle project when we performed a Gradle build via command.

We can open both maven and Gradle projects in IntelliJ IDEA as well. But when browsing the source (just assume Gradle project implements an interface from the Gradle project, and when we clicked on the interface in the implemented class declaration of Gradle project) it points to the class from the sources jar in local maven repo. Is there a way that we can configure IDEA to point to the exact source on the maven project which has been already imported in IDE.

Please note following:

  • The Gradle project refers to the exact SNAPSHOT of the maven project
  • Using IntelliJ IDEA 2019.1.3 (Community Edition)
Kavindu R
  • 121
  • 1
  • 9
  • When building the JAR (which is now in your maven repo), did you build a source JAR? When you see the decompiled JAR, do you also see an option to "Attach Source"? See: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206966355-How-can-I-see-the-source-code-of-jar-files – Darius X. Jul 01 '20 at 02:11
  • When you say you can open both maven and Gradle projects in IntelliJ, do you mean as separate projects, or as modules inside the same project? Because if you include them as modules inside the same project, IntelliJ should allow you to navigate from one to the other – afterburner Jul 01 '20 at 06:07
  • @DariusX. Thanks for pointing out, it is from the sources jar instead decompiled. A sources .jar also has been deployed to the maven local repository. I checked the path of the class loaded in the editor which has the following syntax. `\\\\--sources.jar!\\ClassName.java` The class shown in the editor is not editable (like when we browse classes, methods between more than one linked maven projects) – Kavindu R Jul 01 '20 at 18:08
  • @afterburner Both of these Maven and Gradle projects are composite / multi-module projects as well. I tried to add them as separate projects (tool windows > add project) as well as modules from an existing source (File > New > Module from existing source). I even tried to create an Empty Project and add both projects as modules. But in all the scenarios, browsing classes/methods of dependent Maven module directs to the sources from the sources.jar in local maven repo, instead directing to the exact source from the opened maven project. – Kavindu R Jul 02 '20 at 07:27
  • And I assume you're using Gradle to build in Intellij, rather than Maven? That should do it automatically. Does this help? https://stackoverflow.com/questions/23523573/intellij-maven-dependency-prefer-local-code – afterburner Jul 02 '20 at 07:29
  • @afterburner Yes, we are using gradle to build. With the help of the clues from the comments, I manually added the required maven sub-module to the corresponding gradle-sub-module as follows and manually moved it up just before the same library dependency resolved from local repository. module:gradle-project-parent > module:gradle-sub-module-impl > main > Dependencies > [1] module : maven-sub-module-api; [2] library: maven-sub-module-api (dependency from local repo); Then I could navigate to maven project sources as expected. Maybe there is a more smarter way as well. – Kavindu R Jul 02 '20 at 14:27

1 Answers1

0

If we assume,

  • sources .jars also have been deployed to the maven local repository (from maven project) and
  • Project Settings > modules look like below in this context

Maven Module

  • module:maven-project-parent
  • module:maven-sub-module-api
  • module:maven-sub-module-impl

Gradle Module

  • module:gradle-project-parent
  • module:gradle-sub-module-api
  • module:gradle-sub-module-impl

As described in this answer from intellij and this answer referred in the comments the order of the module and library dependencies matters in integration. We need to manually add the required maven sub-module to the corresponding gradle-sub-module as follows and manually move it up just before the same library dependency resolved from the local repository.

module:gradle-project-parent > module:gradle-sub-module-impl > main > Dependencies

  1. module : maven-sub-module-api
  2. library: maven-sub-module-api (dependency from local repo)

Then you will be able to navigate to maven project sources from gradle project links as expected.

Kavindu R
  • 121
  • 1
  • 9