【问题标题】:How to prevent mvn jetty:run from executing test phase?如何防止 mvn jetty:run 执行测试阶段?
【发布时间】:2010-06-15 11:56:52
【问题描述】:

我们在生产环境中使用 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 文件复制任务。我希望它使用部署版本运行。我该如何解决这个问题?

【问题讨论】:

    标签: java maven-2 jetty maven-jetty-plugin


    【解决方案1】:

    尝试添加参数-Dmaven.test.skip=true or -DskipTests=true in the command line。例如

    mvn -DskipTests=true jetty:run ...
    

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

    更多关于跳过测试的信息is available in the Surefire plugin documentation

    【讨论】:

    • 如果忘了说我已经测试过了,但它没有帮助。
    【解决方案2】:

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

    您需要做的是将copy-test-persistence 执行绑定到test-compile 之后但test 之前的生命周期阶段。而且没有十几个候选人,只有一个:process-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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2016-03-08
      相关资源
      最近更新 更多