本文介绍了如何配置 JPA 以在 Maven 中进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Maven 项目中设置第二个persistence.xml 文件,以便它用于测试而不是用于部署的普通文件?

Is there a way to set up a second persistence.xml file in a Maven project such that it is used for testing instead of the normal one that is used for deployment?

我尝试将persistence.xml 放入src/test/resources/META-INF,它被复制到target/test-classes/META-INF,但它似乎是target/classes/META-INF(来自src/main/resources) 成为首选,尽管 mvn -X test 以正确的顺序列出了类路径条目:

I tried putting a persistence.xml into src/test/resources/META-INF, which gets copied into target/test-classes/META-INF, but it seems target/classes/META-INF (the copy from the src/main/resources) gets preferred, despite mvn -X test listing the classpath entries in the right order:

[DEBUG] Test Classpath :
[DEBUG]   /home/uqpbecke/dev/NetBeansProjects/UserManager/target/test-classes
[DEBUG]   /home/uqpbecke/dev/NetBeansProjects/UserManager/target/classes
[DEBUG]   /home/uqpbecke/.m2/repository/junit/junit/4.5/junit-4.5.jar
...

我希望能够针对简单的 hsqldb 配置运行测试,而无需更改 JPA 配置的部署版本,理想情况下,在项目签出后无需任何本地调整即可直接运行.

I would like to be able to run tests against a simple hsqldb configuration without having to change the deployment version of the JPA configuration, ideally straight after project checkout without any need for local tweaking.

推荐答案

以下内容适用于 Maven 2.1+(在此之前,测试和包之间没有可以绑定执行的阶段).

The following will work for Maven 2.1+ (prior to that there wasn't a phase between test and package that you could bind an execution to).

>

在测试期间,您可以使用maven-antrun-plugin将persistence.xml替换为测试版本,然后在项目打包前恢复正确的版本.

You can use the maven-antrun-plugin to replace the persistence.xml with the test version for the duration of the tests, then restore the proper version before the project is packaged.

此示例假设生产版本为 src/main/resources/META-INF/persistence.xml,测试版本为 src/test/resources/META-INF/persistence.xml,因此它们将被复制到 target/分别为 classes/META-INF 和 target/test-classes/META-INF.

This example assumes the production version is src/main/resources/META-INF/persistence.xml and the test version is src/test/resources/META-INF/persistence.xml, so they will be copied to target/classes/META-INF and target/test-classes/META-INF respectively.

把它封装成一个 mojo 会更优雅,但因为你只是复制一个文件,这似乎有点矫枉过正.

It would be more elegant to encapsulate this into a mojo, but as you're only copying a file, it seems like overkill.

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>copy-test-persistence</id>
      <phase>process-test-resources</phase>
      <configuration>
        <tasks>
          <!--backup the "proper" persistence.xml-->
          <copy file="${project.build.outputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml.proper"/>
          <!--replace the "proper" persistence.xml with the "test" version-->
          <copy file="${project.build.testOutputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
    <execution>
      <id>restore-persistence</id>
      <phase>prepare-package</phase>
      <configuration>
        <tasks>
          <!--restore the "proper" persistence.xml-->
          <copy file="${project.build.outputDirectory}/META-INF/persistence.xml.proper" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

这篇关于如何配置 JPA 以在 Maven 中进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:48