persistence.xml not found in Eclipse Maven Project
Asked Answered
P

7

7

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!

<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:

    <?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>
Piscator answered 3/1, 2014 at 16:10 Comment(3)
src/main/resources should be in the build path by default, but make sure that the Maven plugin hasn't put exclusions on it by default. It does for me :/ And exactly what is giving you the error? Eclipse itself, a unit test, or when deploying to a server?Aboriginal
This is being shown in Eclipse, however modifying pom.xml seems to bring up new errors...I'm guessing it is maven relatedPiscator
did this every get resolved? if so please provide your solution so others with the same problem can benefit.Billposter
P
12

persistence.xml file MUST be added to the classpath to resovle this issue.

Try as following:

  1. Create/Update a directory src/main/resources/META-INF, place persistence.xml file here

  2. Run Maven->Update project configuration

  3. 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.

  4. Select Maven->Update project configuration or Project->Clean

  5. 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).

  6. That's it!

Prosopopoeia answered 31/7, 2014 at 13:42 Comment(0)
R
3

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).

My fix was adding the following to the pom.xml (inside <build>):

<resources>
    <resource>
        <directory>src</directory>
        <includes>
            <include>META-INF/persistence.xml</include>
        </includes>
    </resource>
</resources>
Radiolarian answered 19/6, 2015 at 16:18 Comment(0)
B
2

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.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.1.Final</version>
</dependency>

After that it worked just fine.

Bernicebernie answered 16/8, 2016 at 21:26 Comment(0)
A
0

Solution 1. In your pom.xml replace <sourceDirectory>src</sourceDirectory> with <sourceDirectory>src/main/resources</sourceDirectory>

Solution 2. Add this under build tag

<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

Aquavit answered 3/1, 2014 at 16:56 Comment(0)
S
0
  1. 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>
    

  2. Put you persistence.xml into src/main/resources/META-INF

  3. Go to Project/Properties/Java Build Path/Source, select all folders, click "Remove", "OK"
  4. Rightclick project/Maven/Update Project..., "OK"

The problem seems to be, that the m2e-plugin adds "Excluded: **" to the src/main/resources folder (see Project/Properties/Java Build Path/Source).

Stylize answered 8/4, 2015 at 14:50 Comment(0)
E
0

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.

<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>
Elery answered 15/4, 2016 at 10:37 Comment(0)
R
-1

create a META-INF folder (if doesn't exist) under src->main->java-> and place your persistence.xml file.

hibernate-entitymanager.jar is not required if hibernate-core.jar of latest hibernate version is added.

Rosemonde answered 15/3, 2018 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.