# Building multiple jars per project

**URL:** <https://discuss.gradle.org/t/building-multiple-jars-per-project/5454>\
**Category:** Old Forum Archive\
**Created:** [October 31, 2012, 2:08pm UTC](https://discuss.gradle.org/t/building-multiple-jars-per-project/5454 "2012-10-31T14:08:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![j\_serup](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@j\_serup](https://discuss.gradle.org/u/j_serup)\
**Post date:** [October 31, 2012, 2:08pm UTC](https://discuss.gradle.org/t/building-multiple-jars-per-project/5454/1 "2012-10-31T14:08:00Z")

</div>

In a project I have two source folders: “src” and “model”. I need to build a separate jar for each folder which works fine with:

```gradle
apply plugin: 'java'
sourceSets {
 main {
  java {
   srcDir 'src'
  }
 }
    model {
  java {
   srcDir 'model'
  }
 }
  }
  task modelJar(type: Jar) {
 from sourceSets.model.output
 classifier = 'model'
 customName = 'model.jar'
}

```

Now to the problem. I have a class A in “src” which have a dependency on a class B in “model”. Therefore the classes in “model” must be available before building the classes in “src”.

Is it possible to introduce a build order for source folders in a project?

I have tried to swap the above folders in the sourceSets and I have also tried to use “dependsOn” when bulding the extra jar but it does not solve the problem. Any suggestions?

EDIT:

Adding this:

```gradle
sourceSets.main.java.srcDirs 'model'

```

fixes the compile error but as a result the default jar also contains the model code which I don’t want.

---

<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:** [October 31, 2012, 3:04pm UTC](https://discuss.gradle.org/t/building-multiple-jars-per-project/5454/2 "2012-10-31T15:04:00Z")

</div>

```gradle
dependencies {
  compile sourceSets.model.output // add classes/resources dirs
  // OR
  compile modelJar.outputs.files // add Jar
}

```

In both cases, the necessary task dependencies will be inferred automatically.

---

<div class="post-metadata">

**Author:** ![j\_serup](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@j\_serup](https://discuss.gradle.org/u/j_serup)\
**Post date:** [October 31, 2012, 3:34pm UTC](https://discuss.gradle.org/t/building-multiple-jars-per-project/5454/3 "2012-10-31T15:34:00Z")

</div>

Thanks. It also works using the approach described here:

[http://gradle.1045684.n5.nabble.com/How-do-I-add-a-second-source-set-to-compile-and-jar-td2641199.html](http://gradle.1045684.n5.nabble.com/How-do-I-add-a-second-source-set-to-compile-and-jar-td2641199.html)

but your suggestion seems simpler. Why do you write “//OR” in the above snippet? Currently I need both and it appears to work.

I also need a test.jar so I have extended it to:

```gradle
sourceSets {
 main {
  java {
   srcDir 'src'
  }
 }
 model {
  java {
   srcDir 'model'
  }
 }
 test {
  java {
   srcDir 'test'
  }
 }
}
  task modelJar(type: Jar) {
 from sourceSets.model.output
 classifier = 'model'
 customName = 'model.jar'
}
  task testJar(type: Jar) {
 from sourceSets.test.output
 classifier = 'test'
 customName = 'test.jar'
}
  dependencies {
 compile sourceSets.model.output
  compile modelJar.outputs.files // add model Jar
 compile testJar.outputs.files // add test Jar
}

```

but I get:

“Circular dependency between tasks. Cycle includes [task ‘:compileTestJava’, task ‘:testJar’].”

If I remove “compile testJar.outputs.files” and pass the task testJar on the commandline it works.

---

<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:** [October 31, 2012, 4:56pm UTC](https://discuss.gradle.org/t/building-multiple-jars-per-project/5454/4 "2012-10-31T16:56:00Z")

</div>

> Why do you write “//OR” in the above snippet? Currently I need both and it appears to work.

You shouldn’t need both, as they are variations of the same thing (get the model classes on the compile class path of the main code).

> but I get: “Circular dependency between tasks. Cycle includes [task ‘:compileTestJava’, task ‘:testJar’].” If I remove “compile testJar.outputs.files” and pass the task testJar on the commandline it works.

You don’t need that line, because you don’t want your test classes on the compile class path of your main code. It wouldn’t even work because it contradicts having the main classes on the compile class path of the test code, which is something that you do want (and is already set up for you by the Java plugin).

Just to be clear, ‘dependencies.compile’ means the compile dependencies of the main code. There’s also ‘dependencies.testCompile’ and ‘dependencies.modelCompile’ (and likewise for ‘runtime’).
