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

5

41

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-Annotated classes and most prominently a persistence.xml.

All projects are configured using maven.

Where should a persistence.xml file be put? It needs to be located inside the jar file of that entity library and I'm not sure how to configure this using maven.

(We are just splitting up a project into several smaller projects)

'''UPDATE''' To be clear about this, there is one Maven-Project A containing the persistence.xml and another one (B) which depends on that Project. I've places persistence.xml in src/main/resources/META-INF/persistence.xml in A, when trying to use an EntityManager inside A, no problem, insidie B: nothing works.

EclipseLink gives the following Warning:

[EL Warning]: The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element

I suspect the persistence.xml is not found, but it is present in the target jar.

Lifesaver answered 3/6, 2012 at 14:23 Comment(0)
F
88

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.

Funchal answered 4/6, 2012 at 8:6 Comment(3)
Worked perfectly for me. Had to create a folder META-INF and put the persistence.xml in it.Rugose
@Rugose Same here. I'm just learning Hibernate and Maven and was pulling my hair. Glad to know this is the recommended way to go. :-)Wolframite
Where is the src/main/resources in a NetBeans project?Ascomycete
D
15

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.

Domini answered 7/9, 2013 at 5:53 Comment(1)
This was helpful for my use case, but not for the question asked, which is around persistence.xml files in constituent JARs (possibly of a WAR), rather than directly in the WAR. The WEB-INF/classes/META-INF tip is rather helpful arcana, though!Skittish
M
5

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.

Montespan answered 3/6, 2012 at 14:26 Comment(2)
The note about package hierarchy is incorrect in the context of this question. JPA requires the persistence.xml to be stored in a specific location in the Maven project, as noted in the other answers.Marpet
@RobertDean It's still the right pace, though, and it'll still be copied to the correct location--that it's a "meta" package isn't terribly relevant, as also noted in the other answers. shrug Don't know why a ~7-yr old question/answer is getting attention lately.Montespan
S
3

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

Slate answered 18/12, 2013 at 0:9 Comment(1)
you should put it in src/main/resources instead, which is the maven default and is added automaticallyNovice
M
1

Just mentioning a similar issue I faced that kept bringing me to this question, so this might help others with not much familiarity with Maven as me.

I had a standalone, Java SE Maven project in Eclipse with the hibernate-hikaricp dependency and I had correctly added a persistence.xml file inside the <project_name>/src/main/resources/META-INF folder which is the Maven default resources folder for such JSE projects.

The project's pom.xml included a maven-resources-plugin plugin, but I'm not sure there is a relationship with the issue.

The point is, I was attempting to run the project from inside Eclipse as a Java Application (you know, from a main class with a public static main(String [] args) method) and the runtime log kept saying META-INF/persistence.xml could not be found even though it was in the mentioned, correct folder.

It turned out that I had to rebuild the project in order to resend the persistence.xml file to the correct target output folder. So a clean on the project (which I don't think was necessary at all, but you know, we try out things) and a build solved the issue for me. I called them from inside Eclipse, but I believe calling from the command line with mvn would have worked as well (I just would have had to probably refresh the Eclipse project tree afterwards to sync things up).

Magaretmagas answered 31/1, 2021 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.