# How to use workspace project as source for dependency

**URL:** <https://discuss.gradle.org/t/how-to-use-workspace-project-as-source-for-dependency/49147>\
**Category:** Buildship\
**Created:** [August 16, 2024, 10:37am UTC](https://discuss.gradle.org/t/how-to-use-workspace-project-as-source-for-dependency/49147 "2024-08-16T10:37:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jragan-work](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@jragan-work](https://discuss.gradle.org/u/jragan-work)\
**Post date:** [August 16, 2024, 10:37am UTC](https://discuss.gradle.org/t/how-to-use-workspace-project-as-source-for-dependency/49147/1 "2024-08-16T10:37:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [August 16, 2024, 12:53pm UTC](https://discuss.gradle.org/t/how-to-use-workspace-project-as-source-for-dependency/49147/2 "2024-08-16T12:53:08Z")

</div>

“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()](https://docs.gradle.org/current/userguide/declaring_repositories_adv.html#sec:case-for-maven-local).

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](https://docs.gradle.org/current/userguide/composite_builds.html), 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.

---

<div class="post-metadata">

**Author:** ![jragan-work](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@jragan-work](https://discuss.gradle.org/u/jragan-work)\
**Post date:** [August 16, 2024, 1:36pm UTC](https://discuss.gradle.org/t/how-to-use-workspace-project-as-source-for-dependency/49147/3 "2024-08-16T13:36:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![oleosterhagen](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/oleosterhagen/32/11914_2.png) [@oleosterhagen](https://discuss.gradle.org/u/oleosterhagen)\
**Post date:** [August 17, 2024, 9:21am UTC](https://discuss.gradle.org/t/how-to-use-workspace-project-as-source-for-dependency/49147/4 "2024-08-17T09:21:47Z")

</div>

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

```gradle
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](https://maven.apache.org/plugins/maven-source-plugin/) to your pom.xml.

```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_

 ![grafik](https://global.discourse-cdn.com/gradle/original/2X/3/366758838be662057a496bacd8b0da4770c898ef.png)

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](https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.eclipse.model.EclipseClasspath.html) in your `build.gradle`.

```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
        }
      }
    }
  }
}

```
