Where to put persistence.xml in library jar using maven?

前端 未结 4 1310
无人及你
无人及你 2020-12-24 11:45

At work we have an entity library which is used by several clients for the library (several servlets, a desktop application, etc.). The entity library consists of JPA-Annota

相关标签:
4条回答
  • 2020-12-24 11:57

    I have only gotten persistence.xml to work in my WAR when placed at WEB-INF/classes/META-INF, just as the documentation advises:

    http://docs.oracle.com/cd/E19159-01/819-3669/bnbrj/index.html states the following:

    If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.

    In my Eclipse project props, I added this to the build path:

    src\main\resources

    My Maven project has persistence.xml at the following location:

    src\main\resources\META-INF\persistence.xml

    After running Maven clean install, a WAR is built with:

    WEB-INF\classes\META-INF\persistence.xml

    When I drop this WAR into Tomcat 7's webapps folder, it is deployed properly and JPA works.

    0 讨论(0)
  • 2020-12-24 11:59

    In the maven pom.xml file you have to add the resource directory, so you don't have to add it to the build-path in eclipse manually.

    <project>
      ...
      <build>
        ...
        <resources>
          <resource>
            <directory>src/resources</directory>
          </resource>
        </resources>
        ...
      </build>
      ...
    </project>
    

    see: http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html

    0 讨论(0)
  • 2020-12-24 12:13

    XML configuration files almost always belong in src/main/resources, in a package hierarchy (just like Java source files). Those files will be copied into the artifact in the same hierarchy.

    0 讨论(0)
  • 2020-12-24 12:18

    As @Dave mentions above src/main/resources is the place. When it comes to persistence.xml it should be placed in the META-INF folder. So to conclude: src/main/resources/META-INF/persistence.xml.

    0 讨论(0)
提交回复
热议问题