Search code examples
gradledatanucleus

How can I run DataNucleus Enhancer from Gradle?


Is there a gradle plugin for running DataNucleus Enhancer? As I can see from documentation you can run it only from Maven or Ant: http://www.datanucleus.org/products/datanucleus/jpa/enhancer.html


Solution

  • I searched and found no plugin for running the DataNucleus Enhancer from Gradle. But there is a way of doing this by using the DataNucleus Enhancer Ant task.

    I added the following in my build.gradle.

    task datanucleusEnhance {
        description "Enhance JPA model classes using DataNucleus Enhancer"
        dependsOn compileJava
    
        doLast {    
            // define the entity classes
            def entityFiles = fileTree(sourceSets.main.output.classesDir).matching {
                include 'com/mycom/*.class', 'org/myorg/*.class'
            }
    
            println "Enhancing with DataNucleus the following files"
            entityFiles.getFiles().each {
                println it
            }
    
            // define Ant task for DataNucleus Enhancer
            ant.taskdef(
                name : 'datanucleusenhancer',
                classpath : sourceSets.main.runtimeClasspath.asPath,
                classname : 'org.datanucleus.enhancer.EnhancerTask'
                // the below is for DataNucleus Enhancer 3.1.1
                //classname : 'org.datanucleus.enhancer.tools.EnhancerTask'
            )
    
            // run the DataNucleus Enhancer as an Ant task
            ant.datanucleusenhancer(
                classpath: sourceSets.main.runtimeClasspath.asPath,
                verbose: true,
                api: "JPA") {
                entityFiles.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)
            }
        }
    }
    
    classes.dependsOn(datanucleusEnhance)
    

    In the entityFiles is where you configure your JPA entity annotated classes.

    Unfortunately you cannot see the enhancer output, as this task is using Ant logging. Unless you're running gradle with -i or -d option.

    Using: Java 8, org.eclipse.persistence:javax.persistence:2.1.0, org.datanucleus:datanucleus-accessplatform-jpa-rdbms:4.1.1.