更新时间:2022-06-12 00:38:30
以下内容适用于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>