Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Eclipselink's static weaving from Gradle

I'd like to enable Eclipselink's static weaving for my JPA classes from Gradle. The Eclipselink docs explain how to do this in an Ant task:

<target name="define.task" description="New task definition for EclipseLink static weaving"/>

<taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask"/>
</target>
<target name="weaving" description="perform weaving" depends="define.task">
    <weave  source="c:\myjar.jar"
            target="c:\wovenmyjar.jar"
            persistenceinfo="c:\myjar-containing-persistenceinfo.jar">
        <classpath>
            <pathelement path="c:\myjar-dependent.jar"/>
        </classpath>

    </weave>
</target>

Now I've got 2 questions:

1. How do I 'translate' this into a Gradle approach? I've tried this (based on the docs at http://www.gradle.org/docs/current/userguide/ant.html#N1143F):

task eclipseWeave << {
    ant.taskdef(name: "weave",
                classname: "org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask",
                classpath: configurations.compile.asPath)

    ant.weave(source: relativePath(compileJava.destinationDir),
              target: relativePath(compileJava.destinationDir),
              persistenceinfo: relativePath(processResources.destinationDir) {
    }
}

but the problem is that the classpath doesn't seem to work within ant.weave(..). The weaving process is aborted after a bit with the message:

Execution failed for task ':eclipseWeave'.
> java.lang.NoClassDefFoundError: some/class/from/my/dependencies

The classpath setting works for ant.taskdef(..) since the StaticWeaveAntTask is found in my dependencies. How can I make it apply to ant.weave(..) itself?

2. How do I integrate this into my build, so it is executed automatically after each compileJava step?

like image 296
Martin S. Avatar asked Jan 10 '23 19:01

Martin S.


1 Answers

I know this is an old question, but based on the OP's comment for the "gradle" way to do it, I thought I'd share our approach. We are using the JavaExec task and the various available configuration objects.

Since the weaving is done in the classes directory (before the JAR is built) you end up only having to build one jar, not two. Since our jar is quite large, that was important to us.

task performJPAWeaving(type: JavaExec, dependsOn: "compileJava"){
  inputs.dir compileJava.destinationDir
  outputs.dir compileJava.destinationDir
  main "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
  args  "-persistenceinfo",
   "src/main/resources",
   compileJava.destinationDir.getAbsolutePath()
  classpath = configurations.compileClasspath
}

tasks.withType(Jar){
  dependsOn "performJPAWeaving"
}
like image 152
Terence Avatar answered Jan 18 '23 11:01

Terence