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 and not as the EclipseLink JPA Extensions wiki page says.

             <java classname=""
                   classpathref="maven.runtime.classpath" fork="true">
               <arg line="-loglevel FINE -persistenceinfo src/main/resources target/classes target/classes"/>

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
    [exec] [java] at
    [exec] [java] at
    [exec] [java] at
    [exec] [java] Caused by: java.lang.ClassNotFoundException: javax.persistence.spi.PersistenceUnitInfo
    [exec] [java] at$
    [exec] [java] at Method)
    [exec] [java] at
    [exec] [java] at java.lang.ClassLoader.loadClass(
    [exec] [java] at sun.misc.Launcher$AppClassLoader.loadClass(
    [exec] [java] at java.lang.ClassLoader.loadClass(
    [exec] [java] at java.lang.ClassLoader.loadClassInternal(
    [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!

  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.