使用Gradle时如何执行OpenJPA增强功能?

时间:2015-03-05 21:45:01

标签: ant gradle openjpa

我已经尝试过这个gradle插件https://github.com/schmutterer/gradle-openjpa,但它抱怨它无法找到某些库,也不支持提供的Compile,这使得我无法使用它。

我也尝试过调用ANT任务,下面我的最新尝试是:

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

这是一长串尝试中的最新版本,可能完全是垃圾,因为我只是绝望地撕掉了所有东西: - (

<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>

1 个答案:

答案 0 :(得分:4)

试试这个安德鲁 - 我在S.O.上提供的很好的Enhancer脚本上基于这个gradle。由另一个成员(对于DataNucleus增强器)。

请注意,您需要修改实体文件(包含/排除)以指向您的特定“要成为/不成为”增强型Java源文件。此外,此方法假定类路径派生自您的父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)
        }
    }
}

我希望这会有所帮助,编写第一个gradle脚本的人并不介意我们将它(从DataNucleus)重新定位到OpenJPA。