EJB转Maven项目Maven Install后Jar无persistence.xml,求POM代码作用
persistence.xml Great question! Let’s walk through why your persistence.xml was missing from the generated JAR and exactly what that pom.xml snippet does.
First, Why Was persistence.xml Missing?
By default, Maven follows a standard project structure: it only looks for resource files (like configs, XMLs, properties) in the src/main/resources directory. When you converted your Eclipse EJB project to Maven, your persistence.xml was likely still in the traditional EJB location—usually ejbModule/META-INF—which isn’t part of Maven’s default resource scanning path. So when you ran mvn install, Maven completely ignored this file and didn’t package it into your JAR.
What Does the Pom.xml Snippet Do?
Assuming the code you added looks something like this (the standard fix for this scenario):
<build> <resources> <resource> <directory>src/main/ejbModule</directory> <includes> <include>META-INF/persistence.xml</include> </includes> <targetPath>META-INF</targetPath> </resource> </resources> </build>
Here’s a breakdown of each part:
<directory>: Tells Maven to scan an additional directory for resource files. In this case, it’s pointing to your original EJB module directory wherepersistence.xmllives.<includes>: Narrows down which files from that directory to include. This ensures you only package thepersistence.xmlfile (and not any other unrelated files in the ejbModule directory) to keep your JAR clean.<targetPath>: Specifies where the file should be placed inside the final JAR. JPA requirespersistence.xmlto live in theMETA-INFdirectory of the JAR, so this setting ensures it ends up in the correct location for your EJB/JPA container to find and load it.
A Quick Alternative
If you want to avoid this custom configuration long-term, you can just move your persistence.xml into src/main/resources/META-INF. Maven will automatically pick up files in this default resource directory and package them correctly without needing extra build rules.
内容的提问来源于stack exchange,提问作者rjc

