Andrew Sumner
Andrew Sumner

Reputation: 793

How perform OpenJPA Enhancement when using Gradle?

I've tried this gradle plugin https://github.com/schmutterer/gradle-openjpa but it complains that it cannot find certain libraries and doesn't support providedCompile which makes this unusable for me anyway.

I've also tried calling ANT tasks, my latest attempt below is throwing:

Caused by: C:\Work_Java\workspace\PaxHoldRelease\jpa_enhance.xml:5: taskdef class org.apache.openjpa.ant.PCEnhancerTask cannot be found

build.gralde

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'ear'

// Java compilier compliance level
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenLocal()
    mavenCentral()
}    

ant.importBuild 'jpa_enhance.xml'
war.dependsOn enhance

dependencies {
    // Ensure ear plugin gets war file
    deploy files(war)

    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    compile 'javax.websocket:javax.websocket-api:1.1'  

    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.16'
    compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.5.1'
    compile 'org.glassfish:javax.json:1.0.4'

    providedCompile 'org.apache.openjpa:openjpa:2.2.2'

    providedCompile 'com.sybase:jconn3:6.05'
    providedCompile files('libs/sqljdbc4-3.0.jar')
} 

jpa_enhance.xml

This is the latest version in a long list of attempts and probably complete rubbish as I just ripped everything out in a fit of desperation :-(

<project>
    <target name="enhance">
        <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask"/>

        <!-- invoke enhancer on all .java files below the model directory -->
        <openjpac>
        </openjpac>

        <echo message="Enhancing complete!"/>
    </target>
</project>

Upvotes: 3

Views: 2041

Answers (1)

gto406
gto406

Reputation: 629

Try this Andrew - I loosely based this gradle on the nice Enhancer script provided on S.O. by another member (for the DataNucleus enhancer).

Note that you will need to modify the entity-files (include/exclude) to point to your specific 'to be/to not be' enhanced Java source files. Further, this approach assumes that classpath derives from your parent build.gradle.


task openJPAEnhance {
    description "Enhance JPA model classes using OpenJPA Enhancer"
    dependsOn compileJava

    doLast {
        // define the entity classes
        def entityFiles = fileTree(sourceSets.main.output.classesDir).matching {
            include 'org/foo/mypkg/entity/*.class'
            exclude 'org/foo/mypkg/entity/DoNotEnhance.class'
        }

        println "Enhancing with OpenJPA, the following files..."
        entityFiles.getFiles().each {
            println it
        }

        // define Ant task for Enhancer
        ant.taskdef(
            name : 'openjpac',
            classpath : sourceSets.main.runtimeClasspath.asPath,
            classname : 'org.apache.openjpa.ant.PCEnhancerTask'
        )

        // Run the OpenJPA Enhancer as an Ant task
        //   - see OpenJPA 'PCEnhancerTask' for supported arguments
        //   - this invocation of the enhancer adds support for a default-ctor
        //   - as well as ensuring JPA property use is valid.
        ant.openjpac(
            classpath: sourceSets.main.runtimeClasspath.asPath,
            addDefaultConstructor: true,
            enforcePropertyRestrictions: true) {
            entityFiles.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)
        }
    }
}

I hope this helps, and the individual who wrote that first gradle script did not mind that we re-purposed it (from DataNucleus) to OpenJPA.

Upvotes: 4

Related Questions