【问题标题】:How to make m2eclipse (Maven) point to the "persistence.xml" path?如何使 m2eclipse (Maven) 指向“persistence.xml”路径?
【发布时间】:2011-03-08 20:03:01
【问题描述】:

我使用 Eclipse Helios、m2eclipse Maven 插件和 Glassfish 插件。 我编辑了“pom.xml”文件,以便获得 DerbyClient 和 JPA Persistence 类。

<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>com.apress.javaee6</groupId>
  <artifactId>chapter02</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>chapter02</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.6.1.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.3</version>
        <type>maven-plugin</type>
    </dependency>
  </dependencies>


  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>EclipseLink Repo</id>
      <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
    </repository>    
  </repositories>

</project>

然后,我创建了“src/main/resrouce”目录并放置了一个“persistence.xml”文件,其中包含:

<xml version="1.0" encoding="UTF-8">
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <class>com.apress.javaee6.chapter02.Book</class>
            <properties>
                <property name="eclipselink.target-datababase" value="DERBY"/>
                <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
                <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
                <property name="eclipselink.jdbc.user" value="APP"/>
                <property name="eclipselink.jdbc.password" value="APP"/>
                <property name="eclipselink.jdbc.ddl-generation" value="create-tables"/>
                <property name="eclipselink.jdbc.logging-level" value="INFO"/>
            </properties>
        </persistence-unit> 
    </persistence>
</xml>

当然,德比服务器运行完美。

我已经创建了以下图书类:

package com.apress.javaee6.chapter02; 

import javax.persistence.*;
@Entity
@NamedQuery(name="findAllBooks", query="SELECT b from Book b") 
public class Book {
    @Id @GeneratedValue
    private Long id;
    @Column(nullable = false)
    private String  title;
    private float   price;
    @Column(length = 1000)
    private String  description;
    private String  isbn;
    private Integer nbOfPage;
    private Boolean illustrations;

    public Book() {
        super();
    }
    public Book(Long id, String title, float price, String description,
            String isbn, Integer nbOfPage, Boolean illustrations) {
        super();
        this.id = id;
        this.title = title;
        this.price = price;
        this.description = description;
        this.isbn = isbn;
        this.nbOfPage = nbOfPage;
        this.illustrations = illustrations;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    public Integer getNbOfPage() {
        return nbOfPage;
    }
    public void setNbOfPage(Integer nbOfPage) {
        this.nbOfPage = nbOfPage;
    }
    public Boolean getIllustrations() {
        return illustrations;
    }
    public void setIllustrations(Boolean illustrations) {
        this.illustrations = illustrations;
    }


}

主要功能在这里:

package com.apress.javaee6.chapter02;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // Create an instance of book
        Book book = new Book();
        book.setTitle("The Hitchhiker's guide to the Galaxy");
        book.setPrice(12.5F);
        book.setDescription("Science fiction comedy book");
        book.setIsbn("1-84023-742-2");
        book.setIllustrations(false);

        // Gets an entity manager and a transaction
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter02PU");
        EntityManager em = emf.createEntityManager();

        // Persists the book to the database
        EntityTransaction tx = em.getTransaction();

        try {
            tx.begin();
            em.persist(book);
            tx.commit();

            em.close();
            emf.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

问题是“persistence.xml”无法识别。 如何让 Maven 指向“persistence.xml”文件?

当我激活 Maven 控制台和“更新依赖项”时,我得到了以下(错误的)信息:

28/06/10 15:20:36 CEST: [INFO] Using 'UTF-8' encoding to copy filtered resources.
28/06/10 15:20:36 CEST: [INFO] skip non existing resourceDirectory /home/zakaria/workspace/chapter02/src/main/resources
28/06/10 15:20:36 CEST: [INFO] Using 'UTF-8' encoding to copy filtered resources.
28/06/10 15:20:36 CEST: [INFO] skip non existing resourceDirectory /home/zakaria/workspace/chapter02/src/test/resources

所以我认为他尝试在某个地方创建“资源”目录,但在构建后无法获取它们。

非常感谢;) 问候

【问题讨论】:

  • 顺便说一句,您不需要对 maven-resources-plugin 的这种依赖。只需将其删除。

标签: maven-2 persistence m2eclipse


【解决方案1】:

如果您使用的是 m2eclipse 0.10.0,则无法识别资源是一个已知问题(请参阅 m2eclipse 用户列表)。此处描述了一种解决方法: https://docs.sonatype.org/pages/viewpage.action?pageId=2949459

使用 maven-resources-plugin 版本 2.4.2 或 2.4.3 也可能比 2.4 更好。

【讨论】:

    【解决方案2】:

    您不会告诉 maven “指向”文件,而是将文件放在最终位于类路径上的目录中。对于资源,您应该使用src/main/resources

    换句话说,只需将persistence.xml 放入src/main/resources/META-INF

    我使用的是 m2eclipse 0.10.0,我没有使用任何特殊的解决方法,它只适用于我的工作区中的 jar、ejb 和 war 类型的至少 3 个项目。

    【讨论】:

    • 感谢 Pascal 在 24 小时内第三次 lool 你可以在这里看到:yfrog.com/f/j3projectgp 创建了“src/main/resources”并且“persistence.xml”在那里。不知道怎么回事?再次感谢。
    • @Zakaria:确实,我可以看到(这在问题中并不明显)。你能确认在 Eclipse 中构建时persistence.xml 没有被复制到target/classes/META-INF 吗?在命令行上构建时行为是否不同?如果是,那么也许您正面临@Bruno 描述的问题。我不能说太多,我自己没有遇到这个问题。
    • 是的。我确认没有创建“resources”文件夹,顺便说一下,“resources/META-INFO”目录和“resources/META-INF/persistence.xml”没有创建。我在帖子中添加了一些打印在 Maven 控制台中的“信息”。对于命令行构建方法,今晚晚些时候我会尝试,当我找不到解决方案时,因为我的目标是拥有一个“紧凑”的开发环境。我已经尝试过使用 Netbeans,它开箱即用。再次感谢;)
    【解决方案3】:

    根据 JPA 标准,persistence.xml 应位于 META-INF 文件夹的根目录中。所以输入src/main/resources/META-INF 应该可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多