1

我正在尝试将集成测试添加到一个大型 Maven 项目中。这是所需的事件顺序:

  1. 清理现有的工件。
  2. 解决依赖关系并构建项目。
  3. 通过 Surefire 插件运行单元测试。
  4. Clover 插件的分叉生命周期。
  5. --- 使用 Clover 插件的仪器源。
  6. --- 修改 Clover 的 project.build.directory 和 project.build.finalName。
  7. --- 在新目录中构建 Clover 检测项目。
  8. --- 通过 Surefire 插件运行 Clover 检测单元测试。
  9. --- 从 Clover 检测单元测试中检查代码覆盖率。
  10. ---将 project.build.directory 和 project.build.finalName 重置为原始值。
  11. --- 结束分叉生命周期(Clover 仅通过“测试”阶段分叉)。
  12. 将项目打包为 WAR 文件。
  13. 通过 Tomcat7 插件在本地托管项目 WAR 文件。
  14. 通过 Surefire 插件对 Tomcat7 实例运行集成测试。

除了第 9 步(因此提出问题)之外,所有这些都按预期工作。相反,构建目录仍然设置为 Clover 版本,并且项目名称附加了“-clover”。我还发现,当“MyProject-clover.war”由 Tomcat7 托管时,它无法按预期运行(在浏览器中返回 404 错误)。

即使它确实有效,我们也不需要/想要我们正在测试的 WAR 文件中的 Clover 检测,因为集成测试不会触及任何生产代码(它是 src/test/java 下的所有 Selenium UI 内容)与本地托管的页面而不是生产代码本身进行交互)。

如前所述,这是一个包含数百个依赖项和数十个插件的大型项目。我相信以下内容与我的问题有关,但如有必要,我可以挖掘更多内容(发布整个 pom 文件似乎不合理)。

这是 Clover 插件的 pom 配置:

<plugin>
    <groupId>com.atlassian.maven.plugins</groupId>
    <artifactId>maven-clover2-plugin</artifactId>
    <version>3.1.11</version>
    <configuration>
        <generateHtml>true</generateHtml>
        <generateXml>true</generateXml>
        <licenseLocation>${basedir}/src/test/resources/clover.license</licenseLocation>
        <targetPercentage>92%</targetPercentage>
      <excludes>
        <exclude>**/mock/*.java</exclude>
        <exclude>**/com/mycompany/somestuff/*.java</exclude>
      </excludes>
    </configuration>
    <executions>
        <execution>
            <id>generate-clover-report</id>
            <phase>test</phase>
            <goals>
                <goal>instrument</goal>
                <goal>clover</goal>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这是 WAR 插件的 pom 配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
            <manifestEntries>
                <!--Implementation-Build>${buildNumber}_${timestamp}</Implementation-Build -->
                <Build-Time>${timestamp}</Build-Time>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

这是 Tomcat7 插件的 pom 配置:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <ajpPort>8009</ajpPort>
        <backgroundProcessorDelay>2</backgroundProcessorDelay>
        <configurationDir>${project.build.directory}/tomcat7_plugin</configurationDir>
        <contextFile>${CATALINA_HOME}/conf/context.xml</contextFile>
        <contextReloadable>false</contextReloadable>
        <fork>true</fork>
        <hostName>localhost</hostName>
        <httpsPort>8443</httpsPort>
        <ignorePackaging>false</ignorePackaging>
        <jarScanAllDirectories>false</jarScanAllDirectories>
        <path>/contentmain</path>
        <port>8080</port>
        <serverXml>${CATALINA_HOME}/conf/server.xml</serverXml>
        <tomcatUsers>${CATALINA_HOME}/conf/tomcat-users.xml</tomcatUsers>
        <tomcatWebXml>${CATALINA_HOME}/conf/web.xml</tomcatWebXml>
        <useNaming>true</useNaming>
        <useTestClasspath>true</useTestClasspath>
        <update>true</update>
        <warDirectory>${project.build.directory}/${project.build.finalName}</warDirectory>
    </configuration>
    <executions>
        <execution>
            <id>start-tomcat</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>run-war-only</goal>
            </goals>
            <configuration>
                <fork>true</fork>
            </configuration>
        </execution>
        <execution>
            <id>stop-tomcat</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>shutdown</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这是我运行“mvn clean install -Dmaven.clover.skip”时的当前(期望)行为:

[INFO] --- maven-clover2-plugin:3.1.11:check (generate-clover-report) @ MyProject ---
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:unpack (unpack) @ MyProject ---
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes css/*.css and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes scripts/*/*.* and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes images/*.* and excludes:null
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ MyProject ---
[INFO] Packaging webapp
[INFO] Assembling webapp [MyProject] in [mydir/MyProject/target/MyProject]
[INFO] Processing war project
[INFO] Copying webapp resources [mydir/MyProject/src/main/webapp]
[INFO] Webapp assembled in [2019 msecs]
[INFO] Building war: /mydir/MyProject/target/MyProject.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (default) @ MyProject >>>
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:copy (default) @ MyProject ---
[INFO] 
[INFO] --- buildnumber-maven-plugin:1.1:create-timestamp (default) @ MyProject ---
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (default) @ MyProject <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ MyProject ---
[INFO] Building jar: /mydir/MyProject/target/MyProject-sources.jar
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run-war-only (start-tomcat) @ MyProject ---
[INFO] Running war on http://localhost:8080/contentmain

这是我运行“mvn clean install”时的当前(不希望的)行为(注意 maven-war-plugin 列出的路径和来自 maven-source-plugin 的警告消息:

[INFO] --- maven-clover2-plugin:3.1.11:check (generate-clover-report) @ MyProject ---
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:unpack (unpack) @ MyProject ---
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Configured Artifact: com.mycompany:somedependency:?:jar
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes css/*.css and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes scripts/*/*.* and excludes:null
[INFO] Unpacking /mydir/.m2/myrepo/mycompany/somedir/somedependency.jar to mydir/MyProject/target/MyProject with includes images/*.* and excludes:null
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ MyProject ---
[INFO] Packaging webapp
[INFO] Assembling webapp [MyProject] in [mydir/MyProject/target/clover/MyProject-clover]
[INFO] Processing war project
[INFO] Copying webapp resources [mydir/MyProject/src/main/webapp]
[INFO] Webapp assembled in [1770 msecs]
[INFO] Building war: /mydir/MyProject/target/clover/MyProject-clover.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (default) @ MyProject >>>
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:copy (default) @ MyProject ---
[INFO] 
[INFO] --- buildnumber-maven-plugin:1.1:create-timestamp (default) @ MyProject ---
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (default) @ MyProject <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (default) @ MyProject ---
[WARNING] NOT adding sources to artifacts with classifier as Maven only supports one classifier per artifact. Current artifact [com.mycompany:MyProject:war:clover:ParentProject-SNAPSHOT] has a [clover] classifier.
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run-war-only (start-tomcat) @ MyProject ---
[INFO] Running war on http://localhost:8080/contentmain

在 Clover 的测试阶段完成执行 maven-clover2-plugin:check 目标后,我该怎么做才能确保 {project.build.directory} 和 {project.build.finalName} 值重置为其原始值分叉的生命周期?

我已经尝试浏览CloverWAR插件和Tomcat7的在线手册。我没有看到任何可以用来恢复由 Clover 更改的构建变量的设置。我总是可以对我的 pom 文件中的路径进行硬编码,但我更喜欢不那么脆弱的解决方案。

4

2 回答 2

0

事实证明,除非使用 clover2:setup 目标,否则 Clover 将始终更改这些变量。但是,如果您仍然希望分叉 Clover 的生命周期(即:clover2:instrument 或 clover2:instrument-test),那么这些变量将始终被更改。

我们希望继续使用 clover:instrument-test 分叉生命周期,所以我想出了一个解决方法。我没有使用 project.build.finalName 和 project.build.directory 变量,而是使用我自己的变量,这些变量在 Clover 弄乱它们之前被复制并保存在 Maven 执行中:

<properties>
    <!-- Saving these variables now before Clover alters them -->
    <original.build.finalName>${project.build.finalName}</original.build.finalName>
    <original.build.directory>${project.build.directory}</original.build.directory>
</properties>

然后我告诉所有后续插件使用这些变量而不是 Clover 更改的项目变量:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <cacheFile>${original.build.directory}/war/work/webapp-cache.xml</cacheFile>
            <outputDirectory>${original.build.directory}</outputDirectory>
            <warName>${original.build.finalName}</warName>
            <webappDirectory>${original.build.directory}/${original.build.finalName}</webappDirectory>
            <workDirectory>${original.build.directory}</workDirectory>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <configurationDir>${original.build.directory}/tomcat7_plugin</configurationDir>
            <warDirectory>${original.build.directory}/${original.build.finalName}</warDirectory>
        </configuration>
    </plugin>
</plugins>

替代解决方案可能包括在后续 Maven 阶段创建或使用附加插件,以在 Clover 完成执行后恢复 project.build 变量。这些解决方案中的任何一个都可能比在所有插件中硬编码路径更好。

于 2014-09-23T18:49:37.950 回答
0

[INFO] --- maven-source-plugin:2.2.1:jar (默认)@ MyProject --- [警告] 不要将源添加到带有分类器的工件中,因为 Maven 仅支持每个工件一个分类器。当前工件 [com.mycompany:MyProject:war:clover:ParentProject-SNAPSHOT] 有一个 [clover] 分类器。

这是 Maven 的限制。它不支持具有多个分类器的工件。由于分叉生命周期中的工件已经具有“三叶草”分类器,因此它不能同时具有“源”分类器。这就是存在此警告的原因。如果您需要调用 maven-source-plugin,您可以考虑使用clover2:setup而不是clover2:instrument 。

4. Fork lifecycle for Clover plugin.
--- End forked lifecycle (Clover only forks through 'test' phase).

有两个 Clover 目标可以分叉构建:

  • clover2:instrument - 在“安装”阶段分叉构建

  • clover2:instrument-test - 将构建分叉到“测试”阶段

您可能对后者感兴趣。您可能还对使用 useCloverClassifier=false 选项感兴趣 - 这将禁用分叉构建中“clover”分类器的使用。

我还发现,当“MyProject-clover.war”由 Tomcat7 托管时,它无法按预期运行(在浏览器中返回 404 错误)。

我的第一个猜测是您在运行时缺少 clover.jar (com.atlassian.clover:clover)。您必须将 clover.jar 复制到 Tomcat 的 /lib 目录或将其捆绑到您的 WAR 中。见https://confluence.atlassian.com/display/CLOVER/Using+Clover+for+web+applications

于 2014-10-07T20:50:23.987 回答