Search code examples
javamaven-2jettymaven-jetty-plugin

How to prevent mvn jetty:run from executing test phase?


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>
     <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?


Solution

  • 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>