本文介绍了Clover插件完成后如何还原Maven project.build变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to add integration tests to a large Maven project. Here is the desired order of events:

  1. 清理现有工件.
  2. 解决依赖关系并构建项目.
  3. 通过Surefire插件运行单元测试.
  4. 三叶草插件的叉子生命周期.
  5. ---使用三叶草插件的乐器来源.
  6. ---修改三叶草的project.build.directory和project.build.finalName.
  7. ---在新目录中构建Clover检测项目.
  8. ---通过Surefire插件运行Clover仪器化的单元测试.
  9. ---从四叶草检测到的单元测试中检查代码覆盖率.
  10. --- 将project.build.directory和project.build.finalName重置为原始值.
  11. ---结束分叉的生命周期(三叶草只能在测试"阶段进行分叉).
  12. 将项目打包为WAR文件.
  13. 通过Tomcat7插件在本地托管项目WAR文件.
  14. 通过Surefire插件针对Tomcat7实例运行集成测试.
  1. Clean existing artifacts.
  2. Resolve dependencies and build project.
  3. Run unit tests via Surefire plugin.
  4. Fork lifecycle for Clover plugin.
  5. --- Instrument sources using Clover plugin.
  6. --- Modify project.build.directory and project.build.finalName for Clover.
  7. --- Build Clover instrumented project in a new directory.
  8. --- Run Clover instrumented unit tests via Surefire plugin.
  9. --- Check code coverage from Clover instrumented unit tests.
  10. --- Reset project.build.directory and project.build.finalName to original values.
  11. --- End forked lifecycle (Clover only forks through 'test' phase).
  12. Package project as a WAR file.
  13. Locally host project WAR file via Tomcat7 plugin.
  14. Run integration tests against Tomcat7 instance via Surefire plugin.

除步骤9(因此,出现问题)外,所有其他操作均按预期进行.相反,构建目录仍设置为Clover版本,并且项目名称后附加了"-clover".我还发现,当Tomcat7托管"MyProject-clover.war"时,它无法按预期运行(在浏览器中返回404错误).

All of this works as expected except step #9 (hence the question). Instead, the build directory is still set to the Clover version, and the project name has "-clover" appended to it. I've also found that when "MyProject-clover.war" is hosted by Tomcat7 that it does not function as expected (returns a 404 error in the browser).

即使它确实起作用,我们也不需要在我们正在测试的WAR文件中使用/不需要Clover工具,因为集成测试不会涉及任何生产代码(全部src/test/java下的Selenium UI东西,它与本地托管的页面(而不是生产代码本身)进行交互.)

Even if it did work, We do not need/want Clover instrumentation in the WAR file which we're testing against because the integration tests don't touch any of the production code (it's all Selenium UI stuff under src/test/java which interacts with the locally hosted pages instead of the production code itself).

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

As mentioned, this is a large project containing hundreds of dependencies and dozens of plugins. I believe the following are relevant to my issue, though I can dig up more if necessary (posting the entire pom file seems unreasonable).

这是三叶草插件的pom配置:

Here's the pom configuration for the Clover plugin:

<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配置:

Here's the pom configuration for the WAR plugin:

<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配置:

Here's the pom configuration for the Tomcat7 plugin:

<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"时的当前(所需)行为:

Here's the current (desired) behavior when I run "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的警告消息:

Here's the current (undesired) behavior when I run "mvn clean install" (note the paths listed by maven-war-plugin and warning message from 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}值重置为其原始值?分叉生命周期测试阶段的目标?

我已经尝试浏览的在线手册Clover WAR 插件,以及 Tomcat7 .我没有提到可用于还原由Clover更改的构建变量的任何设置.我总是可以在pom文件中对路径进行硬编码,但我希望使用一种不那么脆弱的解决方案.

I've already tried browsing the online manuals for Clover, the WAR plugin, and Tomcat7. I see no mention of any settings I can use to revert the build variables that are altered by Clover. I can always hard-code the paths in my pom file, but I'd prefer a less brittle solution.

推荐答案

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

It turns out that Clover will always alter these variables unless the clover2:setup target is used. However, if you still wish to fork the lifecycle for Clover (IE: clover2:instrument or clover2:instrument-test) then these variables will always be altered.

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

We wish to continue forking the lifecycle using clover:instrument-test, so I've come up with a workaround. Instead of using the project.build.finalName and project.build.directory variables, I'm using my own variables that get copied and saved on Maven execution before Clover can mess with them:

<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更改过的项目变量:

I then tell all of the subsequent plugins to use these variables instead of the project variables which Clover has altered:

<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阶段中创建或利用其他插件来恢复project.build在Clover完成执行后的变量.这些解决方案中的任何一个都可能比对所有插件中的路径进行硬编码更好.

Alternative solutions might include creating or utilizing an additional plugin in a subsequent Maven phase to revert the project.build variables after Clover has finished executing. Either of these solutions is probably better than hard-coding the paths in all of your plugins.

这篇关于Clover插件完成后如何还原Maven project.build变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 20:56