How to use workspace project as source for dependency

I am trying Gradle for the first time, and I have created a multi-project , to which I a have added a dependency from local Maven repository (only compiled files in a jar), which is developed in the same workspace. Right now, when I try to open declaration of classes in that jar i get source not found. I only found a method to attach a source jar to project, but no information on how can I reference a project as source for a jar.

Is there a way to do it?

“workspace” is not a term that has any defined meaning in the context of Gradle, so it is unclear what you mean.

Generally, using dependencies from mavenLocal() is a very bad idea, as it is broken by design in Maven already, and most often just makes your builds slow and flaky at best, silently broken at worst. Read more details about it at The case for mavenLocal().

Generally, how to get sources displayed also is not a Gradle question, but a question to your IDE vendor. But I guess when consuming the jar from Maven Local, you also have to have the source jar there to get the sources displayed automatically or otherwise need to attach the source directory in your IDE settings.

But what you are probably after anyway, is Composite Builds, assuming that other build is also a Gradle build and has a compatible version. Because then it simply is built when needed and you get the sources open in the IDE alongside automatically and can work on both projects simultaneously.

Other build is done in Maven, I am using/learning Gradle for the first time with a new project and I want to open relevant declarations from that dependency in their respective project ( I can do it with Eclipse build system by adding the project onto class path, but it does not work with Buildship).

Since workspace is not recognized by Gradle, can I just specify an absolute path to source directory for a dependency?

You are using a local Maven repository. Have you checked your .m2 folder whether a sources JAR is actually published alongside the binary jar?

demo-1.0.0.jar         <- JAR with class files
demo-1.0.0.pom
demo-1.0.0-sources.jar <- JAR with sources
_remote.repositories

When there exists no sources JAR you may need to add the Maven Source Plugin to your pom.xml.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <id>attach-sources</id>
          <phase>verify</phase>
          <goals>
            <goal>jar-no-fork</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Checking the Source attachment

  1. Select your Gradle project in the Package or Project Explorer
  2. Open Project > Properties
  3. Select Java Build Path > Library
  4. Open Classpath > Project and External Dependencies
  5. Locate and open your library
  6. Check the Source attachment

When you navigate from your Gradle project to the included classes from your Maven project, you will notice that these .class files cannot be changed. Your editor is read only.

If you want to reference the Maven Eclipse project (e.g. /demo) directly instead of the JAR (e.g. demo-1.0.0.jar) in your Gradle project, you have to manipulate the Eclipse Classpath in your build.gradle.

import org.gradle.plugins.ide.eclipse.model.ProjectDependency

plugins {
  id 'eclipse'
}

eclipse {
  classpath {
    file {
      whenMerged {
        it.entries = it.entries.collect {
          it.path.endsWith('/demo-1.0.0.jar') ? new ProjectDependency('/demo') : it
        }
      }
    }
  }
}