如何使用 exec maven 插件创建一个独立的 JPA(使用 OpenJPA 实现)Maven 项目?

How to make a standalone JPA (with OpenJPA implementation) Maven project start with exec maven plugin?

我有一个非常简单的 JPA 项目。这是我的目录布局..

/src
  /main
    /java
      /biz
        /tugay
          /jpaExamples
            App.java
            /model
              Person.java

  /resources
    /META-INF
      persistence.xml

所以这是非常标准的 maven 目录布局。这是我的 pom.xml..

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>biz.tugay</groupId>
    <artifactId>jpa-examples</artifactId>
    <packaging>jar</packaging>

    <version>1.0-SNAPSHOT</version>

    <name>jpa-examples</name>
    <url>http://www.tugay.biz</url>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <version>2.4.2</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa-persistence</artifactId>
            <version>2.4.2</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.192</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>bare-maven-project</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.jpaExamples.App</mainClass>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

persistence.xml

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd” 版本="2.0">

<persistence-unit name="jpaExamples" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>biz.tugay.jpaExamples.model.Person</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/sampleh2db;TRACE_LEVEL_FILE=0"/>
        <property name="javax.persistence.jdbc.user" value="test"/>
        <property name="javax.persistence.jdbc.password" value="test"/>
        <property name="openjpa.Log" value="DefaultLevel=ERROR, Tool=ERROR"/>
    </properties>
</persistence-unit>

最后,App.java

package biz.tugay.jpaExamples;

import biz.tugay.jpaExamples.model.Person;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.sql.*;

public class App {

    private static final String PERSISTENCE_UNIT_NAME = "jpaExamples";
    private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
        final Person person = entityManager.find(Person.class, 1L);
        System.out.println(person);
        entityManager.close();
        ENTITY_MANAGER_FACTORY.close();
    }
}

当我从 IntelliJ 执行 App.java 时,一切正常。我会看到输出:

Person{id=1, firstname='Koray', lastname='Tugay', address='Yıldız Cd. 43 / 30', city='İstanbul'}

这是 IntelliJ 生成的,当 运行 main in App.java

"C:\Program Files\Java\jdk1.8.0_121\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\Users\Koray Tugay\Desktop\jpa-examples\target\classes;C:\Users\Koray Tugay\.m2\repository\org\eclipse\persistence\javax.persistence.1.1\javax.persistence-2.1.1.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\openjpa\openjpa.4.2\openjpa-2.4.2.jar;C:\Users\Koray Tugay\.m2\repository\commons-lang\commons-lang.6\commons-lang-2.6.jar;C:\Users\Koray Tugay\.m2\repository\commons-collections\commons-collections.2.2\commons-collections-3.2.2.jar;C:\Users\Koray Tugay\.m2\repository\net\sourceforge\serp\serp.15.1\serp-1.15.1.jar;C:\Users\Koray Tugay\.m2\repository\junit\junit.8.1\junit-3.8.1.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\geronimo\specs\geronimo-jms_1.1_spec.1.1\geronimo-jms_1.1_spec-1.1.1.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\geronimo\specs\geronimo-jta_1.1_spec.1.1\geronimo-jta_1.1_spec-1.1.1.jar;C:\Users\Koray Tugay\.m2\repository\commons-pool\commons-pool.6\commons-pool-1.6.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\xbean\xbean-asm5-shaded.17\xbean-asm5-shaded-3.17.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\geronimo\specs\geronimo-jpa_2.0_spec.1\geronimo-jpa_2.0_spec-1.1.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\openjpa\openjpa-persistence.4.2\openjpa-persistence-2.4.2.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\openjpa\openjpa-kernel.4.2\openjpa-kernel-2.4.2.jar;C:\Users\Koray Tugay\.m2\repository\org\apache\openjpa\openjpa-lib.4.2\openjpa-lib-2.4.2.jar;C:\Users\Koray Tugay\.m2\repository\com\h2database\h2.4.192\h2-1.4.192.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain biz.tugay.jpaExamples.App

所以我很确定与我的 persistence.xml 配置和依赖项相关的一切都很好..

当我尝试 mvn exec:java(在 mvn clean install 之后)时,我得到:

[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:745)
Caused by: <openjpa-2.4.2-r422266:1777108 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "
biz.tugay.jpaExamples.model.Person".
    at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:115)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:311)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:235)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:211)
    at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:154)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:226)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:153)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:59)
    at biz.tugay.jpaExamples.App.main(App.java:16)
    ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.240 s
[INFO] Finished at: 2017-05-24T22:32:46+03:00
[INFO] Final Memory: 11M/139M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project jpa-examples: An exception occured while executing the Java class. null: InvocationTargetException: This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "
[ERROR] biz.tugay.jpaExamples.model.Person".

我该如何解决这个问题?我认为 OpenJPA 无法以某种方式找到 persistence.xml,但我可以验证它是否在 target/classes.

下找到

将此添加到 pom.xml 有效:

<plugin>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa-maven-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
        <includes>**/model/*.class</includes>
        <excludes>**/model/XML*.class</excludes>
        <addDefaultConstructor>true</addDefaultConstructor>
        <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
    </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>2.4.2</version>
        </dependency>
    </dependencies>
</plugin>