Dreamer
Dreamer

Reputation: 7549

Issue with Maven Eclipse plugin and openjpa enhancement

Our current project is integrating Spring 3/OpenJPA with Roo and maven on STS(Eclipse). The M2E plugin is a little problematic because it cannot handle entity enhancement.

So from programming side it is a little clumsy that we cannot build the whole project on Maven under Eclipse by one click, but have to let STS(Eclipse) to build the project first and then run Ant script to enhance the entities, then refresh the project, then refresh the server deployment. Not very effective.

I guess this is an issue for a while so create this thread to see if there is any updates.

There seems a tool available(OpenJPA Eclipse Tooling Builder) But it seems not supporting latest Eclipse(STS 2.9.1 is based on Eclipse 3.7, that tool only cover to 3.4).

So the question becomes: is there any way(or tools) we can integrate everything(build, compile with enhancement) together (possibly on Maven by only designing POM)?

Happy to learn any suggestions. Thank you.

Upvotes: 3

Views: 2738

Answers (2)

jfcorugedo
jfcorugedo

Reputation: 10051

I had the same problem with eclipse kepler and OpenJPA 2.2.1.

The solution is quite simple, but a little dirty.

First of all, you need to install OpenJPA m2e connector from this site:

http://openjpa-maven-connector.googlecode.com/svn/trunk/openjpa-connector-update-site

Next, you must modify your POM, and put, next to the elemento of the openjpa-maven-plugin plugin (inside build element), the next definition:

<pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>openjpa-maven-plugin</groupId>
                                    <artifactId>openjpa-maven-plugin</artifactId>
                                    <versionRange>[1.0,)</versionRange>
                                    <goals>
                                        <goal>enhance</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

So the build element should looks like this (with your own entity package):

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <verbose>true</verbose>
                <compilerVersion>1.6</compilerVersion>
                <source>1.6</source>
                <target>1.6</target>
                <debug>true</debug>
                <debuglevel>lines,vars,source</debuglevel>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-maven-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>mappingtool</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    com/xxxx/xxxx/*
                </includes>
            </configuration>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>
            <!--This plugin definition only affects eclipse, not the mvn command execution -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>openjpa-maven-plugin</groupId>
                                    <artifactId>openjpa-maven-plugin</artifactId>
                                    <versionRange>[1.0,)</versionRange>
                                    <goals>
                                        <goal>enhance</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Upvotes: 2

Natraj
Natraj

Reputation: 417

@Subarule I am not using Maven but only ANT for my project and the following ANT script helps to enhance my build.

    <target name="enhance" depends="compile">
    <echo message="Enhancing...."/>
    <!-- ***************************************************************************************************************** -->
    <!-- DO NOT DELETE FOLLOWING CODE. THIS IS ADDED FOR ENCHANCING THE CLASSES AT COMPILE TIME. IT IS REQUIRED BY OPENJPA -->
    <!-- Define the classpath to include the necessary files. -->
    <!-- ex. openjpa jars, persistence.xml, orm.xml, and target classes  -->
    <path id="jpa.enhancement.classpath">
        <!-- Assuming persistence.xml/orm.xml are in META-INF -->
        <pathelement location="META-INF" />

        <!-- Location of the .class files -->
        <pathelement location="${build.classes}" />

        <!-- Add the openjpa jars -->
        <fileset dir="OpenJPA_lib_jar">
                <include name="*.jar" />
        </fileset>
    </path>
    <!-- This is a bit of a hack, but I needed to copy the persistence.xml file from my src dir
         to the build dir when we run enhancement -->
    <!--<copy includeemptydirs="false" todir="bin">
        <fileset dir="src" excludes="**/*.launch, **/*.java"/>
    </copy>-->

    <!-- define the openjpac task; this can be done at the top of the -->
    <!-- build.xml file, so it will be available for all targets -->
    <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask" classpathref="jpa.enhancement.classpath" />

    <!-- invoke enhancer on all .class files below the model directory -->
    <openjpac>
        <classpath refid="jpa.enhancement.classpath" />
        <fileset dir=".">
            <include name="**/model/*.class" />
        </fileset>
        <config propertiesFile = "${basedir}/src/META-INF/persistence.xml"/>
    </openjpac>
    <echo message="Enhancement complete" />
    <!-- ***************************************************************************************************************** -->
</target>

Hope this helps you.

Upvotes: 0

Related Questions