How to use gradle with generated sources?

Dear,

I use this gradle plugin https://github.com/institut-de-genomique/hibernatetools-gradle-plugin to reverse engineering classes from a mysql database.
This plugins put source files into $buildDir/generated

So for this I use this build.gradle:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath   "org.hibernate.gradle.tools:hibernatetools-gradle-plugin:0.1.0"
    }
}
apply plugin: "hibernatetools-gradle-plugin"
apply plugin: 'java'

sourceCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'org.hibernate:hibernate-entitymanager:4.3.9.Final'
    compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'
    compile 'javax.inject:javax.inject:1'
    compile 'commons-logging:commons-logging:1.1.1'
}

database{
    name        = "foo"
    schema      = "foo"
    url         = "jdbc:mysql://foo.bar"
    basePackage = "foo.bar"
    tables      = "Organism,Genomic_Object,GO_process,GO_reaction"
}

sourceSets {
    generated{
        java.srcDir "${buildDir}/generated/"
    }

    main {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }

    test {
        compileClasspath += generated.output
        runtimeClasspath += generated.output
    }
}

compileGeneratedJava.dependsOn(hbm2dao)
compileJava.dependsOn(compileGeneratedJava)
compileJava.source sourceSets.generated.java, sourceSets.main.java

When I run gradle build I have a lot of error as:

build/generated/foo/bar/dao/GoReactionHome.java:5: error: package javax.ejb does not exist
import javax.ejb.Stateless;
                ^
build/generated/foo/bar/dao/GoReactionHome.java:6: error: package javax.persistence does not exist
import javax.persistence.EntityManager;
                        ^
build/generated/foo/bar/dao/GoReactionHome.java:7: error: package javax.persistence does not exist
import javax.persistence.PersistenceContext;
                        ^
build/generated/foo/bar/dao/GoReactionHome.java:8: error: package org.apache.commons.logging does not exist
import org.apache.commons.logging.Log;
                                 ^
build/generated/foo/bar/dao/GoReactionHome.java:9: error: package org.apache.commons.logging does not exist
import org.apache.commons.logging.LogFactory;
                                 ^
build/generated/foo/bar/dao/GoReactionHome.java:16: error: cannot find symbol
@Stateless
 ^
  symbol: class Stateless
build/generated/foo/bar/dao/GoReactionHome.java:19: error: cannot find symbol
    private static final Log log = LogFactory.getLog(GoReactionHome.class);
                         ^
  symbol:   class Log
  location: class GoReactionHome

Thank for your help

regards

1 Like

Is GenomicObjectHome one of the generated files?

I think you need a dependency between compileJava and the compileGeneratedJava tasks, not just the task that generates the source.

yes is exactly I just done (I think), I updated result in the first post.
I do:

compileGeneratedJava.dependsOn(hbm2dao)
compileJava.dependsOn(compileGeneratedJava)
compileJava.source sourceSets.generated.java, sourceSets.main.java

It seem dependencies is not apply to generated class

If I do instead this:

compileJava{
    dependsOn hbm2dao
    source sourceSets.generated.output
}

Less error …

src/main/java/fr/cea/ig/wildfly/App.java:35: error: package fr.cea.ig.database.pkgdb.dao does not exist
import fr.cea.ig.database.pkgdb.dao.GenomicObjectHome;
                                   ^
src/main/java/fr/cea/ig/wildfly/App.java:40: error: cannot find symbol
    GenomicObjectHome genomicObjectHome;
    ^
  symbol:   class GenomicObjectHome
  location: class App
2 errors
1 warning
:compileJava FAILED

While the class exists:

$ ls build/generated/fr/cea/ig/database/pkgdb/dao/
GenomicObjectHome.java  GoProcessHome.java  GoReactionHome.java  OrganismHome.java

thanks for your help

Some debug information:

when I do :

compileGeneratedJava.dependsOn(hbm2dao)
compileJava.dependsOn(compileGeneratedJava) 

javax symbol are not found …

Compiler arguments: -source 1.7 -target 1.7 -d /build/classes/generated -g /build/generated/foo/bar/dao/GoReactionHome.java /build/generated/foo/bar/dao/OrganismHome.java /build/generated/foo/bar/dao/GenomicObjectHome.java /build/generated/foo/bar/dao/GoProcessHome.java /build/generated/foo/bar/model/GoProcess.java /build/generated/foo/bar/model/Organism.java /build/generated/foo/bar/model/GoReaction.java /build/generated/foo/bar/model/GenomicObject.java /build/generated/foo/bar/model/GoReactionId.java /build/generated/foo/bar/model/GoProcessId.java

how to add dependencies to the generated files ? how to set the claspath without circular dependencies?

thanks for your help

I finally fix mine problem but with the need to put some extra information from user side (arf …)

user need to put this piece of code:

sourceSets {
   generated{
        java.srcDir "${buildDir}/generated/src/java/"
    }
}

compileGeneratedJava{
    dependsOn(hbm2dao)
    classpath = configurations.compile
}
compileJava{
    dependsOn(compileGeneratedJava)
    source    += sourceSets.generated.java
}

I do not know how to override user sourceset once my plugin is applied to avoid to put any extra information user side.
That could to be really cool to send automatically the sourcesets when the user call the target compileJava. They are any documentation about this or easy way to do it …

So in this current state I publish a first stable release of mine plugin: https://github.com/institut-de-genomique/hibernatetools-gradle-plugin/releases

Not sure what you are asking here.

I would like to add a sourceset to the target compileJava, from my plugin. When a user apply the plugin and run the target compileJava, needed sourceset is added automatically.

WIll this help?

// In your plugin apply code ...
void addGeneratedToSource(Project project) {

    project.sourceSets.matching { it.name == "main" } .all {
        it.java.srcDir '/path/to/generated/code'
    }
}

Thanks Schalk_Cronje,
that works fine