Search code examples
gradleaptquerydsl

Generating JPA2 Metamodel from a Gradle build script


I'm trying to set up a Gradle build script for a new project. That project will use JPA 2 along with Querydsl.

On the following page of Querydsl's reference documentation, they explain how to set up their JPAAnnotationProcessor (apt) for Maven and Ant.

I would like to do the same with Gradle, but I don't know how and my beloved friend did not help me much on this one. I need to find a way to invoke Javac (preferably without any additional dependencies) with arguments to be able to specify the processor that apt should use (?)


Solution

  • I did not test it but this should work:

    repositories {
        mavenCentral()
    }
    apply plugin: 'java'
    dependencies {
       compile(group: 'com.mysema.querydsl', name: 'querydsl-apt', version: '1.8.4')
       compile(group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '1.8.4')
       compile(group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1')
    }
    
    compileJava {
        doFirst {
            Map otherArgs = [
                includeAntRuntime: false,
                destdir: destinationDir,
                classpath: configurations.compile.asPath,
                sourcepath: '',
                target: targetCompatibility,
                source: sourceCompatibility
            ]
            options.compilerArgs = [
                '-processor', 'com.mysema.query.apt.jpa.JPAAnnotationProcessor',
                '-s', "${destinationDir.absolutePath}".toString()
            ]
            Map antOptions = otherArgs + options.optionMap()
            ant.javac(antOptions) {
                source.addToAntBuilder(ant, 'src', FileCollection.AntType.MatchingTask)
                options.compilerArgs.each {value ->
                    compilerarg(value: value)
                }
            }
        }
    }
    

    Hope it helps.