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?
Problem description
I had a similar issue and would like to share my solution which is based on the previous post. Following problems were solved:
Step 1: Process persistence.xml
Firstly, the placeholder @datasourceName@ should be replaced with an actual value. This is achieved through following snipped in your build.gradle file:
Following code shows the (simplified) src/main/resources/META-INF/persistence.xml file:
It's important to mention, that the flag exclude-unlisted-classes must be set to false in order to make the EclipseLink Weaver discovering the annotated entity classes automatically. Make also sure, that the property "eclipselink.weaving" is set to "static" which tells your application runtime, that the entity classes are already woven and do not need to be enhanced dynamically. If you work with OSGi, keep in mind to import all necessary EclipseLink packages into the bundle which contains the woven classes.
After the processResources task has been executed, the generated file build/resources/main/META-INF/persistence.xml looks like this (note the persistence-unit name):
Step 2: Copy the generated persistence.xml
Next, we add a new Copy task which copies the processed resources into the output directory of the compileJava task. This is necessary to have all necessary artifacts on the Weavers classpath (works around bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=295031). Add following task to your build.gradle file:
Step 3: Weave your entity classes
In order to enhance your classes, it's necessary to configure your build appropriately. Add following snipped to your build.gradle. A separate classpath configuration has been declared because having the EclipseLink classes in the ordinary compile classpath is not desired.
That's it! If you run now any Jar task, your entity classes will be enhanced before they are packed into the Jar file.
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.