Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating JPA2 Metamodel from a Gradle build script

Tags:

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 (?)

like image 967
dSebastien Avatar asked Jun 21 '11 19:06

dSebastien


People also ask

What is metamodel in JPA?

JPA (Java persistence API) metamodel classes are classes that describe the structure of JPA entity classes (classes used to store database state as Java objects). They enable you to write Java code for creating database queries in a typesafe way.

What is static metamodel?

A static metamodel is a series of classes that "mirror" the entities and embeddables in the domain model and provide static access to the metadata about the mirrored class's attributes.

What is metamodel in hibernate?

Hibernate Static Metamodel Generator is an annotation processor based on JSR_269 with the task of creating JPA 2 static metamodel classes. The following example shows two JPA 2 entities Order and Item , together with the metamodel class Order_ and a typesafe query.


1 Answers

While I have no problem with the use gradle makes of Ant, I agree with the original poster that it is undesirable in this case. I found a github project by Tom Anderson here that describes what I believe is a better approach. I modified it a small amount to fit my needs (output to src/main/generated) so that it looks like:

sourceSets {      generated }  sourceSets.generated.java.srcDirs = ['src/main/generated']  configurations {      querydslapt }  dependencies {          compile 'mine go here'     querydslapt 'com.mysema.querydsl:querydsl-apt:2.7.1' }  task generateQueryDSL(type: Compile, group: 'build', description: 'Generates the QueryDSL query types') {          source = sourceSets.main.java          classpath = configurations.compile + configurations.querydslapt          options.compilerArgs = [                 "-proc:only",                 "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"          ]          destinationDir = sourceSets.generated.java.srcDirs.iterator().next() } compileJava.dependsOn generateQueryDSL 

This approach makes a lot more sense to me than the other, if it does to you too, then you have another option for querydsl generation.

like image 57
JoeG Avatar answered Oct 02 '22 19:10

JoeG