【问题标题】:Integration testing Maven jar in a Jetty container在 Jetty 容器中集成测试 Maven jar
【发布时间】:2016-07-29 01:26:09
【问题描述】:

我有一个 Maven 项目,它生成一个用于 Web 服务的 jar 文件。它具有使用jetty-maven-plugin 运行的集成测试。

为了在编译后的 jar 文件上运行集成测试,我必须使用 <systemPath>${project.build.directory}/${project.build.finalName}.${project.packaging}</systemPath> 创建一个依赖项。集成测试如我所愿运行,使用编译后的 jar 文件并从 src/test 目录中正确创建 Web 应用程序。

所以就这个项目构建而言,这个设置非常有效。

问题是发布过程中部署的POM文件仍然有systemPath的依赖。这意味着使用 jar 的项目在构建期间报告错误。该错误表明 jar 文件“必须指定绝对路径”。这些构建不会失败,但日志混乱且具有误导性。

我希望从部署到我们的 Maven 存储库的 POM 中删除此 systemPath。我们该怎么做?

作为参考,这里是项目 POM 的相关部分。

<build>
  <plugins>
    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>9.0.7.v20131107</version>
      <configuration>
        <webAppSourceDirectory>${project.basedir}/src/test/webapp</webAppSourceDirectory>
        <classesDirectory>${project.build.testSourceDirectory}</classesDirectory>
        <useTestClasspath>true</useTestClasspath>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>${project.artifactId}</artifactId>
          <version>${project.version}</version>
          <scope>system</scope>
          <systemPath>${project.build.directory}/${project.build.finalName}.${project.packaging}</systemPath>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <id>start-jetty</id>
          <phase>pre-integration-test</phase>
          <goals>
            <goal>run</goal>
          </goals>
          <configuration>
            <scanIntervalSeconds>0</scanIntervalSeconds>
            <daemon>true</daemon>
          </configuration>
        </execution>
        <execution>
          <id>stop-jetty</id>
          <phase>post-integration-test</phase>
          <goals>
            <goal>stop</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

【问题讨论】:

    标签: maven maven-3 maven-jetty-plugin maven-deploy-plugin


    【解决方案1】:

    Jetty's documentation 关于&lt;classesDirectory&gt; 内容如下:

    为 webapp 编译的类的位置。 [...]

    所以,这应该是${project.build.testOutputDirectory} 而不是${project.build.testSourceDirectory},不是吗?

    &lt;useTestClasspath&gt; 未在Jetty's doc 中提及。

    是否可以install 依赖并使用&lt;scope&gt;provided?从那以后:

    [依赖项]仅在编译和测试类路径上可用,并且不可传递。

    【讨论】:

    • 感谢您的回复。集成测试作为 Maven verify 阶段的一部分运行,该阶段在生命周期中的 install 阶段之前运行。所以这对于使用 provided 是行不通的,对吧?
    • @DavidV 没错,但您可以将maven-install-plugin 绑定到之前的阶段,例如pre-integration-test。
    • 我改为使用${project.build.testOutputDirector} 并使用&lt;scope&gt;runtime&lt;/scope&gt;。然后我可以删除systemPath,一切正常。
    • @DavidV 很高兴你成功了!并感谢您的反馈(经过相当长的一段时间;)。您可以自行使最终解决方案成为答案。与答案相比,评论并不那么突出,并且通常不会被寻求者首先阅读。
    【解决方案2】:

    解决方案是对Gerold Broser's answer 稍作修改。

    以下是相关部分:

    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.1.5.v20140505</version>
        <configuration>
            <webAppSourceDirectory>${project.basedir}/src/test/webapp</webAppSourceDirectory>
            <classesDirectory>${project.build.testOutputDirectory}</classesDirectory>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>${project.artifactId}</artifactId>
                <version>${project.version}</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </plugin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2013-01-20
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      相关资源
      最近更新 更多