-->

How to apply Static Weaving Ant Task with Eclipse-

2020-08-20 04:34发布

问题:

I am using Netbeans 7.1.1 and a Glassfish 3.1.2 server. As JPA provider I decided to use eclipse-link 2.3.0 which is shipped with Netbeans. Since I want to use lazy fetching and my project consists of roughly 45 JPA Entity classes which do have lots of relations among them, I decided to use static weaving. Moreover, the @Entity classes are in a separate Class Lib Project, whereas the persistence.xml resides in the EJB project.

I already followed the instructions here: http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving

I put the ant-task into the build-impl.xml file (below ). Is that correct when using Netbeans ? Here's the snippet from build-impl.xml:

<target name="-post-jar" 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="-post-jar">
    <weave  source="${dist.jar}\MyProject.jar"
            target="${dist.jar}\MyProject_Weaved.jar"
            persistenceinfo="${dist.jar}..\..\MyProjectEE\MyProject-ejb\build\classes\META-INF\persistence.xml"
            loglevel="FINER">
        <classpath>
            <pathelement path="${dist.jar}\lib\eclipselink-2.3.0.jar"/>
            <pathelement path="${dist.jar}\lib\javaee-api-6.0.jar"/>
            <pathelement path="${dist.jar}\lib\jasypt-1.9.0.jar"/>
            <pathelement path="${dist.jar}\lib\javax.persistence-2.0.jar"/>
            <pathelement path="${dist.jar}\lib\org.eclipse.persistence.jpa.jpql_1.0.0.jar"/>
            <pathelement path="${dist.jar}\lib\joda-time-2.1.jar"/>
            <pathelement path="${dist.jar}\lib\jms.jar"/>
        </classpath>
    </weave>
</target>

All jar-files in the pathelement paths are also included as libraries in the Netbeans IDE. When hitting "Clean & Build", I now get the following error:

D:\workspace\MyProject\nbproject\build-impl.xml:703: taskdef class org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask cannot be found
using the classloader AntClassLoader[]

What do I make wrong here ?

回答1:

You need to add the eclipselink.jar and javax.persistence.jar to the Ant classpath.

In Netbeans go to Tools / Options / Miscellaneous / Ant, and edit the classpath there.



回答2:

I am also using Netbeans 8.0.1 for developing java ee projects with Eclipselink 2.4.2 in TomEE 1.7.0 and I just put the following into my build.xml (I never modify build-impl.xml because Netbeans can overwrite it when something is modified in the configuration, building):

<target name="--weaving-def" description="New task definition for EclipseLink static weaving" depends="dist">
    <taskdef name="weave" classname="org.eclipse.persistence.tools.weaving.jpa.StaticWeaveAntTask"
             classpath="${j2ee.platform.classpath}">
    </taskdef>
</target>
<target name="weaving" description="perform weaving" depends="--weaving-def">
    <weave source="${build.classes.dir}"
           target="${build.classes.dir}"
           loglevel="INFO">
        <classpath path="${j2ee.platform.classpath}:${javac.classpath}"/>  
    </weave>
</target>

You can see that I have used only the commonly used variables in all Netbeans Java EE Ant projects. I have not defined any jar directly just the variables which defined in the project already.

In my implementation I am using exploded directory structure and my persistence.xml is under the

${build.classes.dir}/META-INF/persistence.xml

The most important thing was to define the classpath correctly.

Now if I run

ant weaving 

the static weaving will be done. It takes approx 15 seconds more to build so I am building this way only on the testing server and the production server and not in my development environment.

If I just simple run (or use the Netbeans build menu)

ant dist

the building will be resulted normal without weaving.

Of course I have a weaving definition in my persistence.xml like this:

<persistence-unit name="MY-PU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>mydata</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/>
        <property name="eclipselink.logging.level" value="INFO"/>
        <property name="eclipselink.weaving" value="static"/>
    </properties>
</persistence-unit>


回答3:

I also had to include org.eclipse.persistence.jpa.jpql_1.0.1.jar and org.eclipse.persistence.jpa.modelgen.processor-2.3.2.jar in order to bring weaving to live. Hope this helps.