Maven 2 如何让m2eclipse(Maven)指向;persistence.xml“;路径
我正在使用EclipseHelios、M2EclipseMaven插件和Glassfish插件。 我编辑了“pom.xml”文件,以便获得DerbyClient和JPA持久性类Maven 2 如何让m2eclipse(Maven)指向;persistence.xml“;路径,maven-2,persistence,m2eclipse,Maven 2,Persistence,M2eclipse,我正在使用EclipseHelios、M2EclipseMaven插件和Glassfish插件。 我编辑了“pom.xml”文件,以便获得DerbyClient和JPA持久性类 <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
<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&nf=1&file=/rt/eclipselink/maven.repo</url>
</repository>
</repositories>
</project>
主要功能如下:
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控制台和“更新依赖项”时,我得到了以下(错误)信息:
因此,我认为他试图在某个地方创建“资源”目录,但在构建之后无法获得它们
非常感谢;)
关于如果您使用的是m2eclipse 0.10.0,那么未识别资源是一个已知问题(请参阅m2eclipse用户列表)。这里介绍了一种变通方法: 使用maven resources插件版本2.4.2或2.4.3也可能比2.4更好。如果不告诉maven“指向”文件,则将文件放在最终位于类路径的目录中。对于资源,应该使用
src/main/resources
换句话说,只需将persistence.xml
放在src/main/resources/META-INF
中
我使用的是M2Eclipse0.10.0,我没有使用任何特殊的解决方法,它只适用于我工作区中至少3个jar、ejb和war类型的项目 根据JPA标准,persistence.xml应该位于META-INF文件夹的根目录中。因此,将其放入
src/main/resources/META-INF
应该可以工作顺便说一句,您不需要依赖maven资源插件。请删除它。感谢Pascal在24小时内的第三次访问,您可以在这里看到:“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,它是现成的。再次感谢;)
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();
}
}
}
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