7

我正在尝试调整 maven 插件执行将在 maven-2 中运行的阶段。

我的具体问题是尝试运行cobertura:instrument绑定到生命周期阶段的步骤process-test-classes,这样它就不会与使用 aspectj 的其他插件冲突(并删除检测代码,从而生成 0% 的覆盖率报告)。但我的问题更笼统。

在默认生命周期内,我设法通过在插件声明中添加执行部分来做到这一点:

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>instrument-late</id>
                    <phase>process-test-classes</phase>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        ...

这样,当我运行mvn test一切正常时,cobertura:instrument 在我想要的阶段运行,类被检测,测试使用检测类运行,等等。这是总结输出:

[INFO] [clean:clean {execution: default-clean}]
[INFO] [buildnumber:create {execution: default}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-utf8}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-8859_1}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] [compiler:compile {execution: default-compile}]
[INFO] [jar:jar {execution: lib}]
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Preparing hibernate3:hbm2ddl
[WARNING] Removing: hbm2ddl from forked lifecycle, to prevent recursive invocation.
[INFO] [buildnumber:create {execution: default}]
[INFO] Change the default 'svn' provider implementation to 'javasvn'.
[INFO] Checking for local modifications: skipped.
[INFO] Updating project files from SCM: skipped.
[INFO] Storing buildNumber: 2082 at timestamp: 1299861835678
[INFO] Storing buildScmBranch: trunk
[INFO] [native2ascii:native2ascii {execution: native2ascii-utf8}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-8859_1}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] [hibernate3:hbm2ddl {execution: default}]
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] [jar:test-jar {execution: tests}]
[INFO] [dbunit:operation {execution: test-compile}]
[INFO] [cobertura:instrument {execution: instrument-late}]
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/carles/dev/ism4web/portasigma/portasigma-web/target/surefire-reports

...

Results :

Tests run: 62, Failures: 0, Errors: 0, Skipped: 0

Flushing results...
Flushing results done
Cobertura: Loaded information on 74 classes.
Cobertura: Saved information on 74 classes.
[INFO] [dbunit:operation {execution: test}]

但是,当我这样做时,mvn site我似乎无法控制插件的执行阶段。我的报告部分包含:

<reporting>
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
        </plugin>

mvn site(总结)的输出说:

[INFO] [clean:clean {execution: default-clean}]
[INFO] [buildnumber:create {execution: default}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-utf8}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-8859_1}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] [compiler:compile {execution: default-compile}]
[INFO] [jar:jar {execution: lib}]
[INFO] [cobertura:instrument {execution: default-instrument}]
[INFO] [hibernate3:hbm2ddl {execution: default}]
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] [jar:test-jar {execution: tests}]
[INFO] [dbunit:operation {execution: test-compile}]
[INFO] [cobertura:instrument {execution: instrument-late}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Unable to prepare instrumentation directory.
Embedded error: source and destination are the same directory.

仪器被调用了两次,一次在插件定义的(我认为)默认阶段,另一次在我重新定义的阶段。我猜这来自作为站点生命周期的一部分运行的插件。

问题:我还没有找到如何在报告部分/站点生命周期内调整插件执行。有什么提示吗?

4

2 回答 2

1

我在这里猜测,但我认为正在发生的事情是因为 cobertura 目标在其自己的生命周期中运行,检测步骤被调用两次,一次来自 maven 生命周期,一次来自 cobertura 生命周期。

于 2012-02-17T09:54:10.673 回答
0

在多次尝试 Cobertura 之后,我切换到JaCoCo - 即时检测和恢复检测类的选项。

虽然我还没有尝试过你提出的 cobertura-it

除了添加插件之外只需要配置:

<executions>
  <execution>
    <id>jacoco-initialize</id>
    <phase>initialize</phase> <!-- m2e complains if you skip, though mine complains on this either, had to mark it as ignored by m2e, feel free to omit the phase, defaults are fine -->
    <goals>
      <goal>prepare-agent</goal>
    </goals>
  </execution>
  <execution>
    <id>jacoco-site</id>
    <phase>package</phase>
    <goals>
      <goal>report</goal>
    </goals>
  </execution>
</executions>

对我来说额外的好处是我不再有 Java 1.7 的问题。

编辑:更改了 jacoco-initialize 的阶段,以前的版本是错误的:在mvn clean它不再会创建 jacoco.exec 文件之后导致没有覆盖率报告。

于 2013-08-15T09:11:23.233 回答