4

我们在生产环境中使用 MySQL,在单元测试中使用 Derby。我们的 pom.xml 在测试之前复制 Derby 版本的 persistence.xml,并在准备包阶段将其替换为 MySQL 版本:

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

问题是,如果我执行 mvn jetty:run 它将在启动 jetty 之前执行 test persistence.xml 文件复制任务。我希望它使用部署版本运行。我怎样才能解决这个问题?

4

2 回答 2

6

尝试在命令行中添加参数-Dmaven.test.skip=true 或 -DskipTests=true。例如

mvn -DskipTests=true jetty:run ...

不过,不确定这是否会跳过 process-test-resources 阶段。

有关跳过测试的更多信息,请参阅 Surefire 插件文档

于 2010-06-15T12:00:13.640 回答
2

jetty:run目标在执行自身之前调用生命周期阶段test-compile的执行。所以跳过测试执行不会改变任何事情。

您需要做的是将执行绑定到 .之后但之前copy-test-persistence的生命周期阶段。而且没有十几个候选人,只有一个:。test-compiletestprocess-test-classes

这在概念上可能并不理想,但它是最不糟糕的选择,它会起作用:

 <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>
于 2010-06-15T12:57:54.497 回答