We use MySQL in production, and Derby for unit tests. Our pom.xml copies Derby version of persistence.xml before tests, and replaces it with the MySQL version in prepare-package phase:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>copy-test-persistence</id>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<!--replace the "proper" persistence.xml with the "test" version-->
<copy
file="${project.build.testOutputDirectory}/META-INF/persistence.xml.test"
tofile="${project.build.outputDirectory}/META-INF/persistence.xml"
overwrite="true" verbose="true" failonerror="true" />
</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.production"
tofile="${project.build.outputDirectory}/META-INF/persistence.xml"
overwrite="true" verbose="true" failonerror="true" />
</tasks>
</configuration>
<goals>
&开发者_开发百科lt;goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
The problem is, that if I execute mvn jetty:run it will execute the test persistence.xml file copy task before starting jetty. I want it to be run using the deployment version. How can I fix this?
Try adding the arguments -Dmaven.test.skip=true or -DskipTests=true in the command line. For example
mvn -DskipTests=true jetty:run ...
Not sure if this skips the process-test-resources phase, though.
More info about skipping tests is available in the Surefire plugin documentation.
The jetty:run
goal invokes the execution of the lifecycle phase test-compile
prior to executing itself. So skipping tests execution won't change anything.
What you need to do is to bind the copy-test-persistence
execution to a lifecycle phase posterior to test-compile
but prior to test
. And there aren't dozen of candidates but only one: process-test-classes
.
That's conceptually maybe not ideal, but it's the least worse option, and it will work:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>copy-test-persistence</id>
<phase>process-test-classes</phase>
<configuration>
<tasks>
<!--replace the "proper" persistence.xml with the "test" version-->
<copy
file="${project.build.testOutputDirectory}/META-INF/persistence.xml.test"
tofile="${project.build.outputDirectory}/META-INF/persistence.xml"
overwrite="true" verbose="true" failonerror="true" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
...
</executions>
</plugin>
精彩评论