Maven无法识别persistence.xml

时间:2016-10-03 07:49:12

标签: java xml maven

最近认识到在我的项目mvn install命令中无法识别persistence.xml文件。

我将hodor标签写入persistence.xml。 IDE出错。同时我尝试使用mvn install命令编译项目,没有错误并且提供了构建成功。

<?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" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="myPU" transaction-type="JTA">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <jta-data-source>java:/OracleDS</jta-data-source>
      <hodor>hodorhodorhodor</hodor>
      <class>com.mycompany.MyClass</class>
   </persistence-unit>
</persistence>

是否存在像persistence.xml文件一样成功编译的设置或插件?

1 个答案:

答案 0 :(得分:0)

刚刚找到答案,但没有详尽无遗。它完成了mojohaus maven xml plugin

首先,我从xsi:schemaLocation标签下载了相关的.xsd文件。

<xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

将persistence_2_0.xsd移动到我项目的资源文件夹中。

enter image description here

将mojo的xml插件导入pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>xml-maven-plugin</artifactId>
   <version>1.0-beta-3</version>
   <executions>
      <execution>
         <goals>
            <goal>validate</goal>
         </goals>
      </execution>
   </executions>
   <configuration>
      <validationSets>
         <validationSet>
            <systemId>src/main/resources/persistence_2_0.xsd</systemId>
            <dir>src/main/resources/META-INF</dir>
         </validationSet>
      </validationSets>
   </configuration>
</plugin>

最后执行mvn xml:validate命令并给出结果;

[ERROR] Invalid content was found starting with element 'hodor'. One of '{"http://java.sun.com/xml/ns/persistence":non-jta-data-source, "http://java.sun.com/xml/ns/persistence":mapping-file, "http://java.sun.com/xml/ns/persistence":jar-file, "http://java.sun.com/xml/ns/persistence":class, "http://java.sun.com/xml/ns/persistence":exclude-unlisted-classes, "http://java.sun.com/xml/ns/persistence":shared-cache-mode, "http://java.sun.com/xml/ns/persistence":validation-mode, "http://java.sun.com/xml/ns/persistence":properties}' is expected.