Statically weaving JPA entities for EclipseLink using Maven

EclipseLink provides advanced JPA features such as lazy-loading, change tracking and fetch groups using bytecode weaving. To use bytecode weaving you can either dynamically instrument your entity classes at runtime (via a jvm agent) or use a tool to statically process the .class files after compilation. In this post we will present how to use EclipseLink’s static weaver in a Maven project.

To enable static weaving in a Maven based project we have to add the Eclipselink weaver in the process-classes phase of the Maven’s build life cycle. The process-classes phase happens after the compile phase and allows the post-processing of files generated in compile phase. In our case, the EclipseLink weaver will post-process the .class files produced by the compiler to add extra bytecodes that implement the desired JPA functionality (lazy-loading, etc).

In the below code we use the Maven AntRun plugin to call (via the java ANT task) the command line version of the EclipseLink’s static weaver. Please note that the class name of the weaver is org.eclipse.persistence.tools.weaving.jpa.StaticWeave and not org.eclipse.persistence.tools.weaving.StaticWeave as the EclipseLink JPA Extensions wiki page says.

<build>
 <plugins>
   <plugin>
     <artifactId>maven-antrun-plugin</artifactId>
     <executions>
       <execution>
         <phase>process-classes</phase>
         <configuration>
           <tasks>
             <java classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
                   classpathref="maven.runtime.classpath" fork="true">
               <arg line="-loglevel FINE -persistenceinfo src/main/resources target/classes target/classes"/>
             </java>
           </tasks>
         </configuration>
         <goals>
           <goal>run</goal>
         </goals>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>

In addition to the pom.xml changes in order to use static weaving in EclipseLink you have to set the eclipselink.weaving property to static in the META-INF/peristence.xml file.

<property name="eclipselink.weaving" value="static" />

For more information about EclipseLink’s static weaving see the How to Configure Static Weaving for JPA Entities section of the Using EclipseLink JPA Extensions wiki page.

9 thoughts on “Statically weaving JPA entities for EclipseLink using Maven

  1. What are your dependencies?

    I get

    [exec] [INFO] [antrun:run {execution: default}]
    [exec] [INFO] Executing tasks
    [exec] [java] Exception in thread “main” java.lang.NoClassDefFoundError: javax/persistence/spi/PersistenceUnitInfo
    [exec] [java] at org.eclipse.persistence.tools.weaving.jpa.StaticWeaveProcessor.process(StaticWeaveProcessor.java:233)
    [exec] [java] at org.eclipse.persistence.tools.weaving.jpa.StaticWeaveProcessor.performWeaving(StaticWeaveProcessor.java:159)
    [exec] [java] at org.eclipse.persistence.tools.weaving.jpa.StaticWeave.start(StaticWeave.java:110)
    [exec] [java] at org.eclipse.persistence.tools.weaving.jpa.StaticWeave.main(StaticWeave.java:82)
    [exec] [java] Caused by: java.lang.ClassNotFoundException: javax.persistence.spi.PersistenceUnitInfo
    [exec] [java] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    [exec] [java] at java.security.AccessController.doPrivileged(Native Method)
    [exec] [java] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    [exec] [java] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    [exec] [java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    [exec] [java] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    [exec] [java] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    [exec] [java] … 4 more
    [exec] [java] Java Result: 1

    the dependency is there, but it seems that it can’t be picked up by antrun?

  2. Thanks for the info. Here’s the alternative Ant Task call (insert in place of the section above). Note that you need to use ${basedir} prefix for the directories, as shown below. Also note that I have to use maven.test.classpath, like Magnus above, to pick up my provided-scope javax.persistence classes.

    Also strange that my persistence.xml is located under src/main/resources/META-INF, but if I list it like that, it won’t be found. When I leave off the META-INF, it finds it. Go figure…

    Thanks again!
    Ari

  3. if xmp tag doesn’t work, i give up — sorry for all the posts — i don’t know how to post my XML in this blog comment

Comments are closed.