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

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")
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.

This should also be done by the thrift plugin.

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

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. 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.

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

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).

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

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?

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

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 :slight_smile: