# Add sources manually for a dependency which lacks of them

**URL:** <https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456>\
**Category:** Help/Discuss\
**Created:** [September 3, 2015, 1:03am UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456 "2015-09-03T01:03:20Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 3, 2015, 1:03am UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/1 "2015-09-03T01:03:20Z")

</div>

I have a case in which the sources for a dependency are not in jcenter/maven. How can I compile that dependency from a global repository but add the sources manually from a local file?

Specifically, this happens with Hibernate and the library `antlr-2.7.7.jar`. This library has no sources in jcenter or maven and I would like to add them manually. How can I do this?

Eclipse is my IDE.

---

<div class="post-metadata">

**Author:** ![Shinya\_Mochida](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/shinya_mochida/32/2393_2.png) [@Shinya\_Mochida](https://discuss.gradle.org/u/Shinya_Mochida)\
**Post date:** [September 3, 2015, 7:17am UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/2 "2015-09-03T07:17:04Z")

</div>

You can add dependencies manually with method `files`.

```groovy
dependencies {
  compile files('path/to/antlr-2.7.7.jar')
}

```

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 3, 2015, 1:37pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/3 "2015-09-03T13:37:50Z")

</div>

**That’s not what I asked**. I know how to add dependencies. I just want to add the sources for an already added dependecy.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [September 3, 2015, 1:56pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/4 "2015-09-03T13:56:06Z")

</div>

You could use the [whenMerged](https://docs.gradle.org/current/userguide/eclipse_plugin.html#N14341) hook of the eclipse plugin to tweak the eclipse classpath model prior to writing to xml.

Another option is to tweak the xml using the [withXml](https://docs.gradle.org/current/userguide/eclipse_plugin.html#N14354) hook.

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 3, 2015, 8:11pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/5 "2015-09-03T20:11:35Z")

</div>

The only thing that I found is this, but it is not working:

```
eclipse.classpath.file.whenMerged { cp ->
  // Add sources to a classpath entry
  def fileReferenceFactory = new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory()
  
  def entry = cp.entries.find { entry -> entry.path.endsWith('antlr-2.7.7.jar') }
  entry.sourcePath = fileReferenceFactory.fromFile(file('lib/sources/antlr-2.7.7-sources.jar'))
}
```

---

<div class="post-metadata">

**Author:** ![Shinya\_Mochida](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/shinya_mochida/32/2393_2.png) [@Shinya\_Mochida](https://discuss.gradle.org/u/Shinya_Mochida)\
**Post date:** [September 4, 2015, 12:44am UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/6 "2015-09-04T00:44:36Z")

</div>

You can add source path, with following script.

```groovy

eclipse.classpath.file.withXml {x ->
  def n = x.asNode()
  n.classpathentry.find {
    it.@kind == 'lib' && it.@path.contains('antlr-2.7.7')
  }.@sourcepath = 'path/to/antlr-2.7.7-source.jar'
}

```

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 4, 2015, 4:05pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/7 "2015-09-04T16:05:22Z")

</div>

@Shinya_Mochida I just tried that code, but it is still not working

---

<div class="post-metadata">

**Author:** ![Shinya\_Mochida](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/shinya_mochida/32/2393_2.png) [@Shinya\_Mochida](https://discuss.gradle.org/u/Shinya_Mochida)\
**Post date:** [September 4, 2015, 5:10pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/8 "2015-09-04T17:10:54Z")

</div>

Did you try it with command?

```gradle
$ gradle --daemon cleanEclipse eclipse

```

* * *

In my environment, I don’t have any antlr source code, so I try this script to modify…

- output directory to be `out` directory instead of `bin` directory.
- junit to have javadoc.

```gradle
configurations {
  javadocDep (group: 'junit', name: 'junit', version: '4.12', classifier: 'javadoc') {
    exclude module: 'hamcrest-core'
  }
  testCompile 'junit:junit:4.12'
}
eclipse.classpath.file {
  withXml{xml ->
    def node = xml.asNode()
    node.classpathentry.find {it@kind == 'output'}.@path = 'out'
    node.classpathentry.find{
      it.@kind == 'lib' && it.@path.contains('junit')
    }.@javadocpath = configurations.javadocDep.asPath
  }
}

```

And a command `gradle --daemon cleanEclipse eclipse` generates `.classpath` file as follows.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <!-- eclipse task generates bin as output path, but it is changed to out -->
  <classpathentry kind="output" path="out"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <!-- javadocpath attribute is not created by default, but it is added -->
  <classpathentry
      sourcepath="/Users/name/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/a6c3/junit-4.12-sources.jar"
      kind="lib"
      path="/Users/name/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973/junit-4.12.jar"
      javadocpath="/Users/name/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/941a/junit-4.12-javadoc.jar"/>
  <classpathentry
      sourcepath="/Users/name/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/1dc3/hamcrest-core-1.3-sources.jar"
      kind="lib"
      path="/Users/name/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a2/hamcrest-core-1.3.jar"/>
</classpath>

```

(for making it to easy to read, I inserted indentation in each attributes, and shorten hash code.)

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 4, 2015, 5:39pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/9 "2015-09-04T17:39:39Z")

</div>

I forgot to mention that I use the eclipse plugin “Buildship: Eclipse Plug-ins for Gradle” and I think my issue is there.

I just tried the first code that I posted with the `gradle eclipse` task and it works. That is, it adds the sourcepath for the classpathentry.

However, in `Project and External Dependencies` the library has no sources attached. So, I guess the problem is with the eclipse plugin.

---

<div class="post-metadata">

**Author:** ![sterling](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/sterling/32/5395_2.png) [@sterling](https://discuss.gradle.org/u/sterling)\
**Post date:** [September 4, 2015, 6:29pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/10 "2015-09-04T18:29:43Z")

</div>

@etiennestuder @donat Is there anything @IvanRF can tie into when importing with Buildship?

---

<div class="post-metadata">

**Author:** ![etiennestuder](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/etiennestuder/32/7153_2.png) [@etiennestuder](https://discuss.gradle.org/u/etiennestuder)\
**Post date:** [September 5, 2015, 3:48am UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/11 "2015-09-05T03:48:48Z")

</div>

The Tooling API is decoupled from the XML files that are generated via ‘gradlew eclipse’. There is currently no way to attach custom sources such that the Tooling API picks them up. As consequences, Buildship does not see these sources neither.

This issue has been reported before and is likely to be tackled in the near future.

---

<div class="post-metadata">

**Author:** ![IvanRF](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ivanrf/32/2901_2.png) [@IvanRF](https://discuss.gradle.org/u/IvanRF)\
**Post date:** [September 5, 2015, 2:46pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/12 "2015-09-05T14:46:34Z")

</div>

I’m new to Gradle. So, if I’ve understood you correctly, this is an issue from Gradle core and not from Buildship?

---

<div class="post-metadata">

**Author:** ![etiennestuder](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/etiennestuder/32/7153_2.png) [@etiennestuder](https://discuss.gradle.org/u/etiennestuder)\
**Post date:** [September 6, 2015, 6:42pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/13 "2015-09-06T18:42:42Z")

</div>

It’s a feature we first need to implement in Gradle core, i.e. in its Tooling API. We can then make use of in Buildship.

---

<div class="post-metadata">

**Author:** ![mauromol](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mauromol/32/994_2.png) [@mauromol](https://discuss.gradle.org/u/mauromol)\
**Post date:** [September 8, 2015, 12:01pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/14 "2015-09-08T12:01:53Z")

</div>

The following issues are related to this subject:  
[https://issues.gradle.org/browse/GRADLE-2046](https://issues.gradle.org/browse/GRADLE-2046)  
[https://issues.gradle.org/browse/GRADLE-2887](https://issues.gradle.org/browse/GRADLE-2887)  
i.e.: the ability to supply artifacts (sources, javadocs, or even JARs when just the POM is published on a Maven repository) via an alternate repository (a flatDir repository, for instance).

---

<div class="post-metadata">

**Author:** ![grdoozy](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@grdoozy](https://discuss.gradle.org/u/grdoozy)\
**Post date:** [March 22, 2017, 7:19pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/15 "2017-03-22T19:19:29Z")

</div>

A year and half later It’s a little past the ‘near future’ that you mentioned. Any hope of getting this working? I am stuck with some local file dependencies and would really love to have the tooling properly support it. Thanks!

---

<div class="post-metadata">

**Author:** ![js402882](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@js402882](https://discuss.gradle.org/u/js402882)\
**Post date:** [April 3, 2017, 6:38am UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/16 "2017-04-03T06:38:14Z")

</div>

+1  
I need to attach custom sources to dependencies too 😉

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [April 4, 2017, 9:36pm UTC](https://discuss.gradle.org/t/add-sources-manually-for-a-dependency-which-lacks-of-them/11456/17 "2017-04-04T21:36:57Z")

</div>

I think gradle can reference a single dependecy from multiple repositories (ie jar from one repo and sources from another) but I’m not 100% sure… you could try:

```gradle
repositories {
   maven {
      // get the main jars (classes) from here
      url 'http://main/maven/repo'
   }
   maven {
      // get the sources from here (local directory)
      url uri('localrepo')
   }
}

```

Then you could store the sources using maven directory conventions  
eg: `projectRoot/localrepo/${group}/${artifact}/${version}/${artifact}-${version}-sources.jar`
