Upgrade application using OpenJPA to Java 8

2.5k Views Asked by At

I am trying to upgrade my application to Java 8, but it uses OpenJPA with build time enhancement through the openjpa-maven-plugin 2.3.0, which seems to be the last version.

When I build my application, I get an IllegalArgumentException because that version of the plugin is using a PCEnhancer that depends on org.apache.xbean.asm4.ClassReader, that is not compatible with Java 8. I found this ticket: https://issues.apache.org/jira/browse/OPENJPA-2386, but still it is not solved.

Do you know any other way to implement the build enhancement for openjpa without using the openjpa-maven-plugin?

1

There are 1 best solutions below

1
On

As you pointed out, Java 8 with its additional syntax is not compatible with OpenJPA 2.3.0 class enhancement. However, if you absolutely need to use code with OpenJPA 2.3.0 which is written for the Java 8 standard library, you can still compile your code using a JDK for Java 8 which outputs Java version <8 bytecode. For example, I use this workaround in Maven:

POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow.examples</groupId>
    <artifactId>openjpa-2.3.0-jdk8</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>openjpa-2.3.0-jdk8</name>
    <url>http://stackoverflow.com/a/29343171/1391325</url>

    <properties>
        <!-- NOTE: As of OpenJPA version 2.3.0, Java 8 is still unsupported -->
        <javac.version>1.7</javac.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <openjpa.version>2.3.0</openjpa.version>
        <openjpa.entityDir>com/stackoverflow/examples/persistence/entities</openjpa.entityDir>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <version>${openjpa.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>${javac.version}</source>
                    <target>${javac.version}</target>
                    <!-- NOTE: The variable "JAVA_8_HOME" must be set either in the Maven 
                        settings.xml file or as an environment variable! -->
                    <executable>${JAVA_8_HOME}/bin/javac</executable>
                    <fork>true</fork>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <id>enforce-property</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireProperty>
                                    <property>JAVA_8_HOME</property>
                                    <message>Property "JAVA_8_HOME" not set.</message>
                                </requireProperty>
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>${openjpa-maven-plugin.version}</version>
                <configuration>
                    <includes>${openjpa.entityDir}/*.class</includes>
                </configuration>
                <executions>
                    <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.openjpa</groupId>
                        <artifactId>openjpa</artifactId>
                        <!-- set the version to be the same as the level in your runtime -->
                        <version>${openjpa.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

settings.xml:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                  http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>java-8-home</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <JAVA_8_HOME>/usr/lib/jvm/java-8-oracle/</JAVA_8_HOME>
            </properties>
        </profile>
    </profiles>
</settings>