Java 在 Eclipse Maven 项目中找不到persistence.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20907912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
persistence.xml not found in Eclipse Maven Project
提问by JuanBruno2013
I cannot fix the persistence.xml file not found
eclipse problem, this is a simple test project (Maven Nature) for a very basic EJB testing, the file is indeed in src/main/resources/META-INF/... this is the pom.xml contents. Tried adding the folder to the project's build path, updating maven project. No luck so far, any help would be greatly appreciated, thanks!
我无法修复persistence.xml file not found
eclipse 问题,这是一个用于非常基本的 EJB 测试的简单测试项目(Maven Nature),该文件确实在 src/main/resources/META-INF/...这是 pom.xml 的内容。尝试将文件夹添加到项目的构建路径,更新 Maven 项目。到目前为止没有运气,任何帮助将不胜感激,谢谢!
<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>
<groupId>LibEE</groupId>
<artifactId>LibEE</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<mainClass>main.resources.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.ow2.spec.ee</groupId>
<artifactId>ow2-ejb-3.0-spec</artifactId>
<version>1.0.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
And this is the persistence.xml:
这是persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="LibEE" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/LibEE</jta-data-source> <!-- Refers to data source in Enterprise server -->
<class>main.java.entities.Book</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
回答by Sinto
Solution 1. In your pom.xml replace <sourceDirectory>src</sourceDirectory>
with <sourceDirectory>src/main/resources</sourceDirectory>
解决方案 1. 在您的 pom.xml 中替换<sourceDirectory>src</sourceDirectory>
为<sourceDirectory>src/main/resources</sourceDirectory>
Solution 2. Add this under build tag
解决方案 2. 在 build 标签下添加这个
<resources>
<resource>
<directory>src</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
Solution 3.
In your pom.xml remove <sourceDirectory>src</sourceDirectory>
then maven will look for default layout
解决方案 3. 在您的 pom.xml 中删除<sourceDirectory>src</sourceDirectory>
然后 maven 将寻找默认布局
回答by Shekh Akther
persistence.xml file MUST be added to the classpath to resovle this issue.
必须将 persistence.xml 文件添加到类路径中以解决此问题。
Try as following:
尝试如下:
Create/Update a directory src/main/resources/META-INF, place persistence.xml file here
Run Maven->Update project configuration
Verify that src/main/resources was added to source folders (Java Resources) by Right click on your project in Eclipse IDE > Properties > Java Build Path > Source tab. You will found /src/main/resources here.
Select Maven->Update project configuration or Project->Clean
If you build your applicaiton, check for persistence.xml file under target//WEB-INF/classes/META-INF/persistence.xml (or how you have configured your classpath).
That's it!
创建/更新目录 src/main/resources/META-INF,将 persistence.xml 文件放在这里
运行Maven->更新项目配置
通过在 Eclipse IDE > 属性 > Java 构建路径 > 源选项卡中右键单击您的项目,验证 src/main/resources 是否已添加到源文件夹(Java 资源)。您将在此处找到 /src/main/resources。
选择 Maven->Update project configuration 或 Project->Clean
如果您构建应用程序,请检查 target//WEB-INF/classes/META-INF/persistence.xml 下的 persistence.xml 文件(或您如何配置类路径)。
就是这样!
回答by slartidan
Add this code to your pom.xml (in section "build")
<resources> <resource> <directory>src</directory> <includes> <include>**</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </resource>
Put you persistence.xml into src/main/resources/META-INF
- Go to Project/Properties/Java Build Path/Source, select all folders, click "Remove", "OK"
- Rightclick project/Maven/Update Project..., "OK"
将此代码添加到您的 pom.xml(在“构建”部分)
<resources> <resource> <directory>src</directory> <includes> <include>**</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </resource>
把你的 persistence.xml 放入 src/main/resources/META-INF
- 转到 Project/Properties/Java Build Path/Source,选择所有文件夹,点击“Remove”、“OK”
- 右键单击项目/Maven/更新项目...,“确定”
The problem seems to be, that the m2e-plugin adds "Excluded: **" to the src/main/resources folder (see Project/Properties/Java Build Path/Source).
问题似乎是,m2e 插件将“排除:**”添加到 src/main/resources 文件夹(请参阅 Project/Properties/Java Build Path/Source)。
回答by Diogo Kollross
I had a similar problem (but using JDeveloper 11) where src/META-INF/persistence.xml
was not included into my project's JAR (at /META-INF/persistence.xml
).
我有一个类似的问题(但使用 JDeveloper 11),其中src/META-INF/persistence.xml
没有包含在我的项目的 JAR (at /META-INF/persistence.xml
) 中。
My fix was adding the following to the pom.xml
(inside <build>
):
我的修复是将以下内容添加到pom.xml
(内部<build>
):
<resources>
<resource>
<directory>src</directory>
<includes>
<include>META-INF/persistence.xml</include>
</includes>
</resource>
</resources>
回答by Harrison Ssamanya
All the answers seem to be correct. The problem, no one explains whats going on. For more understanding, I followed a resource provided by maven here.
所有的答案似乎都是正确的。问题,没有人解释这是怎么回事。为了更多的理解,我关注了maven提供的资源here。
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>[your directory]</directory>
<includes>
<include>[resource file #1]</include>
<include>[resource file #2]</include>
<include>[resource file #3]</include>
...
<include>[resource file #n]</include>
</includes>
</resource>
...
</resources>
...
</build>
...
</project>
回答by Andrew
The top answer is correct; however this persistently (pun intended) failed to work no matter what I tried, until I included the hibernate entitymanager in my dependencies in Maven.
最佳答案是正确的;然而,无论我尝试什么,这始终(双关语)都无法正常工作,直到我在 Maven 的依赖项中包含了休眠实体管理器。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
After that it worked just fine.
之后它工作得很好。
回答by Arun Raaj
create a META-INF folder (if doesn't exist) under src->main->java-> and place your persistence.xml file.
在 src->main->java-> 下创建一个 META-INF 文件夹(如果不存在)并放置您的 persistence.xml 文件。
hibernate-entitymanager.jar is not required if hibernate-core.jar of latest hibernate version is added.
如果添加了最新的hibernate 版本的hibernate-core.jar,则不需要hibernate-entitymanager.jar。