# How can I compile generated sources?

**URL:** <https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541>\
**Category:** Old Forum Archive\
**Created:** [October 29, 2011, 1:54pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541 "2011-10-29T13:54:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Maxey](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mark\_Maxey](https://discuss.gradle.org/u/Mark_Maxey)\
**Post date:** [October 29, 2011, 1:54pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/1 "2011-10-29T13:54:00Z")

</div>

I need to do source code generation and found [http://gradle.1045684.n5.nabble.com/GenerateSources-functionality-for-SourceSets-td4600652.html](http://gradle.1045684.n5.nabble.com/GenerateSources-functionality-for-SourceSets-td4600652.html). The example given for [SourceSetOutput](http://gradle.org/releases/latest/docs/dsl/org.gradle.api.tasks.SourceSetOutput.html) using buildBy doesn’t exist for SourceSet (as noted in the other post).

Can you point me to an example of how to have a task generate Java source to be built by the Java plugin?

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [October 30, 2011, 12:44am UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/2 "2011-10-30T00:44:00Z")

</div>

Hi Mark,

Dealing with generated source is currently not as nice as we’d like it to be, but here’s a general template you could use:

```gradle
apply plugin: "java"
  task generateSources {
  outputDir = file("$buildDir/generated-src")
  outputs.dir outputDir
    doFirst {
    outputDir.exists() || outputDir.mkdirs()
    new File(outputDir, "Generated.java").write("public class Generated {}")
  }
}
  compileJava.source generateSources.outputs.files, sourceSets.main.java

```

The key parts are:

```gradle
outputs.dir outputDir

```

And then:

```gradle
compileJava.source generateSources.outputs.files, sourceSets.main.java

```

This configures the necessary task dependencies as well as the locations of the extra source.

Please let me know if you’d like me to expand on any of that.

We’ll definitely make this simpler in a future version.

---

<div class="post-metadata">

**Author:** ![Mark\_Maxey](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mark\_Maxey](https://discuss.gradle.org/u/Mark_Maxey)\
**Post date:** [October 30, 2011, 7:30pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/3 "2011-10-30T19:30:00Z")

</div>

Thanks. Here’s how I did it after reading the Antlr plugin code:

```gradle
apply plugin: 'java'
  task generateSource(type: Copy) {
 from 'src/main/mpfw'
 into 'build/generated-src/main/java'
}
  compileJava.dependsOn generateSource
  sourceSets {
 main {
  java {
   srcDir 'build/generated-src/java'
  }
 }
}

```

It seems our solutions are similar. I didn’t know you could construct the compileJava.source the way you did. There are so many clever things like this in Gradle I don’t understand yet.

I’ve almost got the equivalent written in a custom plugin by copying the antlr plugin.

It does seem a generic solution is in order. Would it make sense to have a generic “codeGeneration” plugin that had a DSL accepting the name of the task to do the code generation, the dependencies for that task, etc? I work on a large code baseline with several different kinds of code generation over many modules. It doesn’t make sense to replicate this logic in lots of places.

---

<div class="post-metadata">

**Author:** ![adammurdoch](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/adammurdoch/32/17_2.png) [@adammurdoch](https://discuss.gradle.org/u/adammurdoch)\
**Post date:** [October 30, 2011, 10:47pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/4 "2011-10-30T22:47:00Z")

</div>

@mark, a generic solution would be very useful. Code generation is such a common concept that it should be baked into Gradle’s model.

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [October 30, 2011, 11:22pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/5 "2011-10-30T23:22:00Z")

</div>

Mark,

There’s a key difference between our two approaches in that you’ve had to duplicate the location of the generated source. In you’re example they are also actually different, which makes a good case for removing the duplication 🙂

---

<div class="post-metadata">

**Author:** ![Szczepan\_Faber](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/szczepan_faber/32/213_2.png) [@Szczepan\_Faber](https://discuss.gradle.org/u/Szczepan_Faber)\
**Post date:** [November 1, 2011, 9:16am UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/6 "2011-11-01T09:16:00Z")

</div>

Implicit task dependencies are very powerful, yet I like Mark’s solution tiny bit better. I’m fine with a bit more redundancy/explicitness if it makes the build script clearer for a non-pro gradle user. The duplication could be removed by a variable or dynamic property on a task.

Also, Mark’s solution will work out of the box with IDE integration (our eclipse and idea plugins, and 3rd party tools like STS, JetBrains plugin).

Obviously, down the road we should have a generic solution, possibly an explicit dsl for code generation stuff.

---

<div class="post-metadata">

**Author:** ![mbjarland1](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mbjarland1](https://discuss.gradle.org/u/mbjarland1)\
**Post date:** [January 18, 2012, 1:53pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/7 "2012-01-18T13:53:00Z")

</div>

A quick request for improved documentation on this.

The current documentation for source sets and SourceSetOutput at:

[DSL Documentation for SourceSetOutput](http://gradle.org/docs/current/dsl/org.gradle.api.tasks.SourceSetOutput.html)

and

[Java docs for SourceSetOutput](http://gradle.org/docs/current/javadoc/org/gradle/api/tasks/SourceSetOutput.html)

gives an example of how to compile generated sources. The example code does not work in M6 and M7 as written. It’s good that the forums had an answer, but we would probably want to update the docs as well.

---

<div class="post-metadata">

**Author:** ![Szczepan\_Faber](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/szczepan_faber/32/213_2.png) [@Szczepan\_Faber](https://discuss.gradle.org/u/Szczepan_Faber)\
**Post date:** [January 19, 2012, 3:40pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/8 "2012-01-19T15:40:00Z")

</div>

Hey, the examples show only how to put generated ‘resources’ on path, e.g. not how to generate/compile sources.

Why do you think they are not working?

---

<div class="post-metadata">

**Author:** ![mbjarland1](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mbjarland1](https://discuss.gradle.org/u/mbjarland1)\
**Post date:** [January 19, 2012, 11:06pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/9 "2012-01-19T23:06:00Z")

</div>

I stand corrected sir. Yes, they do indeed only talk about generated ‘resources’ as opposed to generated ‘sources’ and compiling them. What threw me off were comments like:

```gradle
...
//it is now a part of the 'main' classpath and will be a part of the jar
...
//Java plugin task 'classes' and 'testClasses' will automatically depend on relevant tasks registered with 'builtBy'
...

```

which for a casual (or careless as in my case) reader might indicate compilation.

This does however not necessarily mean we don’t need an improvement of the docs. Is there a section in the docs explaining how to deal with ‘generated sources’ and the compilation of them? I failed to find it, but I’ll refrain from being wrong twice in the same thread and leave the question open : )

---

<div class="post-metadata">

**Author:** ![Szczepan\_Faber](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/szczepan_faber/32/213_2.png) [@Szczepan\_Faber](https://discuss.gradle.org/u/Szczepan_Faber)\
**Post date:** [January 20, 2012, 11:17am UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/10 "2012-01-20T11:17:00Z")

</div>

No worries 🙂

Please suggest improvements to the docs (pull request maybe?). I don’t think we have an idiomatic example for generated compile sources in the user guide (which should be fixed!) but I thought there’s something on that matter in forums.

---

<div class="post-metadata">

**Author:** ![Uyen\_Vu](https://avatars.discourse-cdn.com/v4/letter/u/7bcc69/32.png) [@Uyen\_Vu](https://discuss.gradle.org/u/Uyen_Vu)\
**Post date:** [January 20, 2012, 4:24pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/11 "2012-01-20T16:24:00Z")

</div>

Does anyone know how to disable warnings when compiling generated classes?

I would like to see compile warnings for my source code only. I do not wish to see compile warnings for generated classes.

Here is my code:

File generatedDir = new File(buildDir.toString() + ‘/generated’)

//Include generated classes in the sourceSets for compilation

sourceSets.main.java

{

srcDir generatedDir

}

Any help would be greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![mbjarland1](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@mbjarland1](https://discuss.gradle.org/u/mbjarland1)\
**Post date:** [January 21, 2012, 7:57pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/12 "2012-01-21T19:57:00Z")

</div>

Yes, this thread answers the question and it obviously lives in the forums so we do have an answer in the forums : )

I think Luke’s first answer in this thread is an excellent idiomatic example. Had I found that answer in the docs it would have solved my issue.

---

<div class="post-metadata">

**Author:** ![Szczepan\_Faber](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/szczepan_faber/32/213_2.png) [@Szczepan\_Faber](https://discuss.gradle.org/u/Szczepan_Faber)\
**Post date:** [January 23, 2012, 9:19am UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/13 "2012-01-23T09:19:00Z")

</div>

oh man, I forgot that it’s exactly this thread 😃

---

<div class="post-metadata">

**Author:** ![Russell\_Gold](https://avatars.discourse-cdn.com/v4/letter/r/edb3f5/32.png) [@Russell\_Gold](https://discuss.gradle.org/u/Russell_Gold)\
**Post date:** [April 14, 2014, 4:55pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/14 "2014-04-14T16:55:00Z")

</div>

The information here is two years old, and there is a comment that this should be done more generically. Has a better way been built into gradle now?

---

<div class="post-metadata">

**Author:** ![daz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daz/32/2475_2.png) [@daz](https://discuss.gradle.org/u/daz)\
**Post date:** [April 14, 2014, 11:56pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/15 "2014-04-14T23:56:00Z")

</div>

Not for java sources. But we’ve added some nicer support for generated sources to the new C++ support, and we plan to roll that into the new java plugins in Gradle 2.0.

---

<div class="post-metadata">

**Author:** ![Marcel\_Overdijk](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/marcel_overdijk/32/144_2.png) [@Marcel\_Overdijk](https://discuss.gradle.org/u/Marcel_Overdijk)\
**Post date:** [November 12, 2014, 6:17pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/16 "2014-11-12T18:17:00Z")

</div>

Did a generic solution for Java ended up in 2.x? I want to do use apt.

---

<div class="post-metadata">

**Author:** ![Jason\_Zwolak](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@Jason\_Zwolak](https://discuss.gradle.org/u/Jason_Zwolak)\
**Post date:** [January 14, 2015, 7:43pm UTC](https://discuss.gradle.org/t/how-can-i-compile-generated-sources/7541/17 "2015-01-14T19:43:00Z")

</div>

I’m also wanting to use the Java solution. Did it make it in 2.x?
