# Add jar to exported classpath

**URL:** <https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543>\
**Category:** Old Forum Archive\
**Created:** [February 18, 2013, 11:19pm UTC](https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543 "2013-02-18T23:19:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@Benjamin](https://discuss.gradle.org/u/Benjamin)\
**Post date:** [February 18, 2013, 11:19pm UTC](https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543/1 "2013-02-18T23:19:00Z")

</div>

I have a task that is producing a jar outside of the java plugin (running an external tool). I would like it so any project that includes the project that gens this jar to use it as part of their classpath. I don’t want to just add this jar to the dependencies section, as it is being generated by a task in this project (not pre-gen).

I would like to, on my consumer project, just do this and have it compile against the produced jar, just as if gradle had compiled it (Well, not exactly, since gradle pulls in the classes folder, and this is a jar, but functionally the same):

```gradle
apply plugin: JavaPlugin
dependencies {
   compile ":ExternalWrappedProject"
}

```

In the external project, I have something like:

```gradle
task genProject //Makes a jar
  //This would seem to be the way to do this, but no luck
Configuration compileConfig = project.getConfigurations().add(JavaPlugin.COMPILE_CONFIGURATION_NAME);
 artifacts {
   compileConfig file new File(project.buildDir, "someOutputJar"), builtBy: genProject
}
  task compileJava (dependsOn: genProject);

```

This works to have my genProject code called at the right time, but I am not getting the jar onto the classpath.

---

<div class="post-metadata">

**Author:** ![Rene](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/rene/32/8082_2.png) [@Rene](https://discuss.gradle.org/u/Rene)\
**Post date:** [February 18, 2013, 11:30pm UTC](https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543/2 "2013-02-18T23:30:00Z")

</div>

Hello Benjamin,

you can use what we call a “buildable filecollection” for that:

```gradle
dependencies
    compile files("$buildDir/someGeneratedOutput") {
        builtBy 'genProject'
    }
}

```

Have a look at the “file dependencies” section in the gradle userguide for the details: [http://www.gradle.org/docs/current/userguide/dependency\_management.html](http://www.gradle.org/docs/current/userguide/dependency_management.html)

cheers, René

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [February 18, 2013, 11:35pm UTC](https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543/3 "2013-02-18T23:35:00Z")

</div>

Try:

```gradle
artifacts {
  runtime(file("$project.buildDir/someJar.jar")) {
     builtBy genProject
   }
 }

```

Make sure that the Jar path is correct, and that you reference the project from another project like this:

```gradle
dependencies {
  compile project(":foo")
}

```

---

<div class="post-metadata">

**Author:** ![Benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@Benjamin](https://discuss.gradle.org/u/Benjamin)\
**Post date:** [February 19, 2013, 7:31pm UTC](https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543/4 "2013-02-19T19:31:00Z")

</div>

Both of these methods work if the java plugin is applied to the project producing the jar. However, I was hoping to not apply that plugin, as it does no work on the project (the jar is created by an external utility), and so adding all the tasks from the Java plugin is silly, as they do nothing.

Since adding the plugin got it working, I’ll leave it, but it means that when java builds happen, this project run empty tasks, which could possibly cause confusion later down the road ☹

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [February 19, 2013, 8:23pm UTC](https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543/5 "2013-02-19T20:23:00Z")

</div>

I don’t see why the producing project would have to apply the ‘java’ plugin. Should be good enough to apply the ‘base’ or ‘java-base’ plugin, and add the artifact to the ‘default’ configuration (or your own).

PS: A project dependency on a ‘java’ project does get resolved to the Jar (not the classes folder).

---

<div class="post-metadata">

**Author:** ![Benjamin](https://avatars.discourse-cdn.com/v4/letter/b/3d9bf3/32.png) [@Benjamin](https://discuss.gradle.org/u/Benjamin)\
**Post date:** [February 20, 2013, 8:32pm UTC](https://discuss.gradle.org/t/add-jar-to-exported-classpath/6543/6 "2013-02-20T20:32:00Z")

</div>

The following case (Applying Java Plugin and using one of its configurations) works

```gradle
apply plugin: 'java'
artifacts {
   //also works with compile
   runtime(artifact)
}

```

The following do not work:

```gradle
apply plugin: 'java'
artifacts {
   archives(artifact)
}

```

```gradle
// base also does not work
apply plugin: 'java-base'
artifacts {
   archives(artifact)
}

```

```gradle
// base also does not work
apply plugin: 'java-base'
configurations {
   // or compile
   runtime
}
artifacts {
   // or compile
   runtime(artifact)
}

```

In all these cases, by ‘not work’ I mean that the project compiles fine but the consumer projects gets compile errors (jar not on classpath).

Yea, I see it does get the full jar, I misread the debug output, sorry 🙂
