如何恢复四叶草插件完成后的Maven project.build变量?(How to revert

2019-10-21 01:30发布

我试图集成测试添加到一个大的Maven项目。 这里是事件所需的顺序:

  1. 清洁现有的工件。
  2. 解决依赖性和构建项目。
  3. 通过Surefire插件运行单元测试。
  4. 叉生命周期四叶草插件。
  5. ---采用四叶草插件仪器来源。
  6. ---修改project.build.directory和project.build.finalName三叶草。
  7. ---建立三叶草仪表项目在新目录中。
  8. ---运行四叶草通过Surefire插件仪表单元测试。
  9. ---从三叶草仪表单元测试,检查代码覆盖率。
  10. --- 复位project.build.directory和project.build.finalName原始值。
  11. ---结束分叉生命周期(三叶草仅通过“测试”相叉)。
  12. 包项目作为WAR文件。
  13. 通过Tomcat7插件本地主机项目WAR文件。
  14. 通过神火插件对Tomcat7实例运行集成测试。

所有这一切都按预期工作,除了步骤#9(因此这个问题)。 相反,build目录仍设置为三叶草版本,项目名称有“-clover”追加到它。 我还发现,当“MyProject的-clover.war”由Tomcat7托管按预期它不工作(在浏览器返回404错误)。

即使没有工作,我们并不需要/想在我们反对,因为集成测试不碰任何的生产代码测试WAR文件三叶草仪表(这是在src / test / java下所有硒UI的东西,其与本地托管的网页,而不是生产代码本身)进行交互。

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

下面是四叶草插件的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>

下面是对战争插件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全新安装-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全新安装”(注意通过Maven的战争插件和Maven的源插件警告信息中列出的路径当前的(不需要的)行为:

[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

我能做些什么,以确保该{project.build.directory}和{} project.build.finalName值被重置为初始值三叶草已经执行完毕的maven-clover2-插件后:期间的测试阶段,检查的目标分叉的生命周期?

我已经试过了浏览联机手册, 三叶草 ,在WAR插件,并Tomcat7 。 我看没有提到我可以用它来恢复由三叶草改变了生成变量的任何设置。 我总是可以硬编码在我的POM文件的路径,但我更喜欢一个不太脆弱的解决方案。

Answer 1:

[INFO] ---行家 - 源插件:2.2.1:罐(默认)@ MyProject的--- [警告]不添加源与分类伪像的Maven仅支持每个伪像一个分类。 当前神器[com.mycompany:MyProject的:战争:三叶草:ParentProject-快照]有[三叶草]分类。

这是Maven的的限制。 它不支持多分类文物。 作为一个分叉生命周期中的神器具有“三叶草”分类已经,不能在同一时间的“源”分类。 这就是为什么这个警告是存在的。 如果你需要调用一个maven-源插件工具:您可以考虑使用clover2:设置 ,而不是clover2。

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

有其派生一个构建两个三叶草目标:

  • clover2:仪器 - 叉构建直到“安装”阶段

  • clover2:仪器测试 - 叉构建直到“测试”阶段

你可能有兴趣在后者。 您还可能有兴趣在使用useCloverClassifier = false选项 - 这将在分叉构建禁用“三叶草”分类器的使用。

我还发现,当“MyProject的-clover.war”由Tomcat7托管按预期它不工作(在浏览器返回404错误)。

我的第一个猜测是,你有clover.jar(com.atlassian.clover:三叶草)在运行时失踪。 你必须可以复制clover.jar到Tomcat的/ lib目录或捆绑在你的WAR。 见https://confluence.atlassian.com/display/CLOVER/Using+Clover+for+web+applications



Answer 2:

事实证明,三叶草将永远改变这些变量,除非clover2:用于设定目标。 但是,如果您仍然希望到餐桌三叶草生命周期(IE:clover2:仪器或clover2:仪器测试),那么这些变量将永远被改变。

我们希望继续使用分叉三叶草生命周期:仪器测试,所以我想出了一个解决方法。 而不是使用project.build.finalName和project.build.directory变量,我使用的被复制并保存在与他们四叶草可以胡来之前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>

然后我告诉所有的后续插件来使用这些变量来代替它三叶草已经改变了项目变量:

<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变量。 这两种解决方案可能比硬编码在所有插件的路径更好。



文章来源: How to revert Maven project.build variables after Clover plugin finishes?