gpt4 book ai didi

java - 使用 OpenJPA 将应用程序升级到 Java 8

转载 作者:行者123 更新时间:2023-12-01 14:35:31 24 4
gpt4 key购买 nike

我正在尝试将我的应用程序升级到 Java 8,但它使用 OpenJPA,通过 openjpa-maven-plugin 2.3.0 增强了构建时间,这似乎是最后一个版本。

当我构建我的应用程序时,我得到一个 IllegalArgumentException,因为该版本的插件正在使用依赖于 org.apache.xbean.asm4.ClassReader 的 PCEnhancer,它与 Java 8 不兼容。我找到了这张票:https://issues.apache.org/jira/browse/OPENJPA-2386 , 但仍未解决。

您知道在不使用 openjpa-maven-plugin 的情况下实现 openjpa 构建增强的其他方法吗?

最佳答案

正如您所指出的,Java 8 及其附加语法与 OpenJPA 2.3.0 类增强不兼容。但是,如果您确实需要使用为 Java 8 标准库 编写的 OpenJPA 2.3.0 代码,您仍然可以使用 JDK for Java 8 编译代码,它输出 Java 版本 <8 字节码.例如,我在 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>

设置.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>

关于java - 使用 OpenJPA 将应用程序升级到 Java 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28476227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com