How can we integrate maven and gradle projects in IntelliJ IDEA with source browsing support?

Answered

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 decompiled class from the 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)
0
2 comments

When navigating to the source code of a class in the Editor IDEA uses classpath of the module in question which consists of the dependencies entries, listed under the dependencies tab. If the current module has a dependency on another module (Module dependency type) then navigation will use the class from dependent module. If as a dependency compiled jar library is used and it does not have attached sources the IDE will navigate to compiled class code. Also order of the libraries is important - the first library with matched class will be used.

0
Avatar
Permanently deleted user

Konstantin Annikov Thanks a lot for the information and the hints. 

I have done a mistake in the description. The sources are not decompiled content, but from the sources jar as you pointed. 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. <maven_local_repo_path>\<group_id_path>\<artifact_id>\<version>\<artifact_id>-<version>-sources.jar!\<package_id_path>\ClassName.java The class shown in the editor is not editable (like when we browse classes, methods between more than one linked maven projects)


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


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 dipendency 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 to tell IDE to always pick the modules first and then the libraries, and also to get rid of manually adding module dependencies to each sub-module.

 

 

0

Please sign in to leave a comment.