Enhance OpenJPA entities with Gradle


OpenJPA has a Maven plugin but doesn’t provide a gradle plugin (yet?) but build-time enhancing is still a nice solution to ensure your entities will behave correctly whatever deployment you choose (in a plain TomEE the built-in javaagent does the work well but in embedded tests it is not guaranteed).

Is that a reason to abandon Gradle? Maybe not yet ;).

Asking quickly your friend Google you will likely find Gradle OpenJPA Plugin. The setup is not very hard:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'at.schmutterer.oss.gradle:gradle-openjpa:0.2.0'
    }
}

apply plugin: 'openjpa'

openjpa {
    // optional but makes the enhancing scanning easier/faster if your entities are in a single package
    files = fileTree(sourceSets.main.output.classesDir).matching {
        include '**/jpa/*.class' // I put the JPA model in a jpa package for easiness
    }
}

So you declare a plugin dependency, apply the plugin, configure it if needed, run it…and boom. This plugin relies on an OpenJPA version old enough to not work on java 8.

So what’s next? OpenJPA provides a main(String[]) for the enhancing, should you use that? Not exactly. The last versions of OpenJPA supports java 8 so you just have to override the plugin OpenJPA version. You simply do that adding OpenJPA as buildscript dependency before the plugin:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.apache.openjpa:openjpa:2.4.1' // HERE
        classpath 'at.schmutterer.oss.gradle:gradle-openjpa:0.2.0'
    }
}

// as before

Then run

gradle clean build

and your classes will be enhanced properly at build time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s