持久性单元被覆盖

persistence unit gets overwritten

我正在编写一个独立的 java 应用程序,从 Maven 项目构建它并通过调用 jar 文件执行。

在我的应用程序中,我有一个使用持久性单元的实体管理器,在我的 persistence.xml 中定义为独立的(事务类型="RESOURCE_LOCAL")

注释 类 的实际实体来自另一个项目,因此作为依赖项添加到 pom.xml 文件中。

问题是包含实体 类 的项目的 persistence.xml 在构建时覆盖了 jar 目标文件夹中的实际独立应用程序 persistence.xml。

Maven 构建过程成功完成,但是当 运行 jar 我得到一个异常,如:

Exception in thread "main" javax.persistence.PersistenceException: No persistence providers available for "PersistenceUnitXXX" after trying the following discovered implementations: org.eclipse.persistence.jpa.PersistenceProvider

此外,包含实体 类 的项目的持久性单元用于部署在应用程序服务器上,因此我认为不可能将 persistence.xml 重用于独立应用程序.

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>

    <parent>
        <groupId>com.my</groupId>
        <artifactId>projects</artifactId>
        <version>1.0</version>
        <relativePath>../..</relativePath>
    </parent>

    <groupId>com.my.projects</groupId>
    <artifactId>mystandaloneapp</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.oracle.weblogic</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.3-0-0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openejb</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0-5</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>1.4.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>1.4.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.my</groupId>
            <artifactId>my_entities</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.my.StandAloneApp</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>create-my-bundle</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>eksport-assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

文件结构为

-projects       
    -mystandaloneapp    
     -src
      -main
        -java
          -com
            -my
              -db
                -EntityConsumer
        -resources
           -WEB-INF
             -persistence.xml
  -my_entities    
    -src
      -main
        -java
          -com
            -my
              -model
                -db
                  -MyEntity
        -resources
           -WEB-INF
             -persistence.xml

好的,这个问题是这个问题的重复,它提供了一个可行的解决方案:

Maven overwrite resource file in dependency