# How to compile generated and non-generated classes in gradle when they are dependent?

**URL:** <https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148>\
**Category:** Help/Discuss\
**Created:** [March 13, 2016, 7:15pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148 "2016-03-13T19:15:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kant111](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kant111](https://discuss.gradle.org/u/kant111)\
**Post date:** [March 13, 2016, 7:15pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/1 "2016-03-13T19:15:17Z")

</div>

How to compile generated and non-generated classes in gradle when non-generated classes(.java) are dependent on generated classes(.java)?

My generated classes are in $buildDir/generated-sources/thrift My non generated classes are in src/main/java

The generated classes (.java files) are need to compile the non generated classes (.java files again) to finally put everything under $buildDir/classes/main (all .class files should be here)

Another way to say this would be my non generated classes in (src/main/java) need generated classes as a library dependency (.class files) to compile and run. So far I have the below build.gradle but it doesn’t quite work

```
plugins {
    id "org.jruyi.thrift" version "0.3.1"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: "org.jruyi.thrift"

group 'com.peernova'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile files('/usr/local/lib/thrift-0.9.2.jar')
    compile 'org.slf4j:slf4j-log4j12:1.7.12'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    //compile files("$buildDir/classes/main") {
        builtBy 'compileThrift'
    }
}

def generatedDir = "$buildDir/generated-sources/thrift"

compileThrift {
    thriftExecutable "/usr/local/bin/thrift"
    //sourceDir "src/main/thrift"
    sourceDir "$buildDir/../../../lib/service_api"
    createGenFolder false
}

task thrift(type: Exec) {
    commandLine '/usr/local/bin/thrift'
}

compileJava {
    dependsOn 'compileThrift'
}

task(run_server, dependsOn: 'classes', type: JavaExec) {
    if ( project.hasProperty("appArgs") ) {
        args Eval.me(appArgs)
    }
    main = 'com.apple.project.servicehandlers.server'
    classpath = sourceSets.test.runtimeClasspath
}

idea.module.sourceDirs += file("build/generated-sources/thrift")
```

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [March 14, 2016, 2:11pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/2 "2016-03-14T14:11:53Z")

</div>

```gradle
sourceSets.main.java.srcDir 'build/generated-sources/thrift'

```

This should really be done by the Thrift plugin automatically. If that’s not the case, you should open a bug report for them.

> [@kant111](#):
>
> compileJava {  
> dependsOn ‘compileThrift’  
> }

This should also be done by the thrift plugin.

> [@kant111](#):
>
> idea.module.sourceDirs += file(“build/generated-sources/thrift”)

This won’t be necessary after you do the line mentioned above.

---

<div class="post-metadata">

**Author:** ![kant111](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kant111](https://discuss.gradle.org/u/kant111)\
**Post date:** [March 14, 2016, 6:40pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/3 "2016-03-14T18:40:18Z")

</div>

Hi st\_oehme,

The Thrift Plugin doesnt do lot of stuff but I am kind of stuck with it. here is the plugin that I use [https://github.com/jruyi/thrift-gradle-plugin](https://github.com/jruyi/thrift-gradle-plugin). I am just trying hard to see if there is a way to fix my problem. The main problem here is that everything works fine as long as I put my .thrift files under src/main/thrift (The line that has been commented above because I have a requirement that my .thrift files should be outside of my project). so when I give it the right path **sourceDir “$buildDir/…/…/…/lib/service\_api”** it doesn’t work. It only works when I set it to **sourceDir src/main/thrift**. All this because my non generated source code has a compile time library dependency of generated code which means the generated code should be compiled ahead and put its .class files under build/classes/main before I compile my non generated source code. Again everything works fine as long as I set it to **sourceDir src/main/thrift** but the moment I set it to **sourceDir “$buildDir/…/…/…/lib/service\_api”** as per my requirement it doesn’t work.

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [March 14, 2016, 6:51pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/4 "2016-03-14T18:51:23Z")

</div>

> [@kant111](#):
>
> sourceDir “$buildDir/…/…/…/lib/service\_api”

Looking at the plugin, I don’t see why this wouldn’t work. What problem do you get?

---

<div class="post-metadata">

**Author:** ![kant111](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kant111](https://discuss.gradle.org/u/kant111)\
**Post date:** [March 14, 2016, 7:32pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/5 "2016-03-14T19:32:52Z")

</div>

It just throws me bunch of errors saying that “java cannot find symbol class some-non-generated-class location some-genereated-class.java” I tried it again with sourceDir src/main/thrift and it works fine but didn’t work with sourceDir “$buildDir/…/…/…/lib/service\_api” (I made sure This location is correct also).

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [March 14, 2016, 9:53pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/6 "2016-03-14T21:53:58Z")

</div>

Can you create a small example project that reproduces the problem?

---

<div class="post-metadata">

**Author:** ![kant111](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kant111](https://discuss.gradle.org/u/kant111)\
**Post date:** [March 15, 2016, 12:37am UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/7 "2016-03-15T00:37:01Z")

</div>

That would take a good amount of time because thrift is a tricky one to handle. is this a hard problem to solve? can stack trace help that I get from gradle build --debug?

---

<div class="post-metadata">

**Author:** ![kant111](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@kant111](https://discuss.gradle.org/u/kant111)\
**Post date:** [March 15, 2016, 6:14am UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/8 "2016-03-15T06:14:12Z")

</div>

I created a symlink as follows ln -s src/main/thrift/filename.thrift and it worked however It doesn’t make a lot of sense although it fixes my problem for now

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [March 15, 2016, 12:37pm UTC](https://discuss.gradle.org/t/how-to-compile-generated-and-non-generated-classes-in-gradle-when-they-are-dependent/15148/9 "2016-03-15T12:37:12Z")

</div>

Please get in touch with the developer of the plugin, this is an issue he needs to solve, not a Gradle Core issue.

Good thing you at least have a workaround 🙂
