2

我有一些单元测试(虽然它们是 Android 测试,但我使用的是Robolectric,所以它们在 上运行JVM)。他们在没有覆盖的情况下快乐地奔跑。

当我尝试覆盖时,我从 emma-maven 收到此错误:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:emma-maven-plugin:1.0-alpha-3:instrument (default-cli) on project android: Execution default-cli of goal org.codehaus.mojo:emma-maven-plugin:1.0-alpha-3:instrument failed: class [com.larvalabs.svgandroid.ParserHelper] appears to be instrumented already

重要的是class .... appears to be instrumented already

很难找到合适的文档,但这是我从各种来源拼凑起来的配置:

<plugin>
    <!-- This doesn't work, see below for a working configuration -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>emma-maven-plugin</artifactId>
    <version>1.0-alpha-3</version>
    <inherited>true</inherited>                          
    <executions>
            <execution>
                    <phase>process-classes</phase>                               
                    <configuration>
                            <filters>
                                    <filter>-com.viewpagerindicator.*</filter>
                                    <filter>-com.actionbarsherlock.*</filter>
                                    <filter>-com.larvalabs.svgandroid.*</filter>
                            </filters>
                    </configuration>
                    <goals> 
                            <goal>instrument</goal>
                    </goals>
            </execution>
    </executions>
</plugin>

问题是,排除了它抱怨的那些包(我认为问题是这些是 Android 库项目无意中在某些路径列表上结束了两次),它现在抱怨我自己的包。


一位同事错误地建议上面的 <plugin> 部分应该放在 <project><build><pluginManagement> 中。

事实证明 <configuration> 应该直接在 <plugin> 并且应该删除剩余的 <executions> 位,请参阅答案。

4

3 回答 3

5

我找到了一个解决方案——简单得令人尴尬。

忘记 pluginManagement 的东西:只有通过忽略 /this/ pom 的过滤器才能“起作用”,但将它们应用于任何子 pom:http ://maven.apache.org/pom.html#Plugin_Management

只需将配置元素移出执行块,然后删除执行块。http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Generic_Configuration

使用插件元素内的配置部分,使用 'mvn -X emma:emma' 运行会显示列出的过滤器。由于每个排除行都改变了我看到的错误,我推断它是排除的。(我认为包括在内,您添加带有 + 前缀的过滤器以覆盖上述 - 前缀的部分。)

<project>
 <build>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>emma-maven-plugin</artifactId>
    <version>1.0-alpha-3</version>
    <!-- inherited>true</inherited -->
    <configuration>
            <filters>
                    <filter>-com.viewpagerindicator.*</filter>
                    <filter>-com.actionbarsherlock.*</filter>
                    <filter>-com.larvalabs.svgandroid.*</filter>
            </filters>
            <!-- verbose>true</verbose -->
   </configuration>
  </plugin>

就问题的隐含 Android 部分而言(这实际上使过滤对我来说毫无意义),因为 Android 需要一个单独的 APK 来保存测试并运行主应用程序 APK,所以您根本不运行 Android 集成测试同一个项目:android 插件希望你从一个单独的项目中制作集成测试 APK - 通常是同一个父 pom 文件下的兄弟。隐藏在 android 插件页面 - http://code.google.com/p/maven-android-plugin/wiki/Samples的 zip 中的示例/示例项目- 您应该在其中搜索“覆盖”并更改它至:

<coverage>true</coverage>

到配置 - 几个样本有它,但被注释掉了。我分心了,所以不记得这是否有效,但显然应该有效。

由于我们不想在项目的现有源代码控制中插入目录级别,因此我们创建了名为“父”和“集成”的目录作为应用程序 pom.xml 的对等目录,使用“父”来保存父 pom,并且“ integration' 用于集成测试,本节告诉 app/pom.xml 使用 app/parent/pom.xml:

<project>
    <parent>
            <groupId>our.group.id</groupId>
            <artifactId>base</artifactId>
            <relativePath>parent</relativePath>
            <version>3.9.0</version>
    </parent>

这在 app/integration/pom.xml 中:

<project>
  <parent>
    <groupId>our.group.id</groupId>
    <artifactId>base</artifactId>
    <relativePath>../parent</relativePath>
    <version>3.9.0</version>
  </parent>
于 2012-03-04T07:21:28.493 回答
2

据我了解,只有 cobertura 需要检测类来生成报告。如果您在目标/类中创建它们,它们将覆盖原始类文件。

如果因此需要 jar 中的检测文件,则可以配置maven-jar-plugin 以从 target/generated-classes 目录中获取文件,而不是标准 ${build.项目.输出目录}。

编辑

看看 maven-jar-plugin 描述。要仅使用目标/生成类,POM 中的以下添加应该可以工作 - 尝试并根据您的需要进行修改:

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3</version> <!-- replace with correct version nbr! -->
        <configuration>
          <includes>
            <include>${project.build.directory}/generated-classes/**/*.class</include>
          </includes>
          <excludes>
            <exclude>${project.build.directory}/classes/**/*.class</include>
          </excludes>

        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

${project.build.directory}指向您的目标文件夹,${project.build.ouputDirectory}指向目标/类。我不知道你是否可以简单地设置${project.build.ouputDirectory}一个新值-看看maven book的这一章,也许你会找到一些提示

参考 :

或者,您可以在coberture:instrument完成后使用 maven 将文件从目标/生成类复制到目标/类。这个问题有一个示例 POM(片段)的答案,您只需要确定正确的阶段(流程资源对于您的情况来说绝对为时过早)

参考这个

希望能帮助到你。!

于 2012-03-04T07:49:06.273 回答
0

请使用模式=“覆盖”。它会工作得很好。

于 2017-07-03T09:11:03.417 回答