maven-2 - maven cobertura 报告 0% 与 aspectj

标签 maven-2 maven-plugin aspectj cobertura

有人能够将 maven2 与 Aspectj 插件和 Cobertura 插件一起使用吗?当我应该得到一些东西时,我一直得到 0% 的覆盖率。 Cobertura 检测在 Aspectj 编织之前运行,我认为这会扰乱 Cobertura 检测。此外,Cobertura 正在就我的方面发出警告,看起来它正试图将它们解析为 java 文件。任何帮助将不胜感激。

C:\project>mvn cobertura:cobertura
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Project
[INFO]    task-segment: [cobertura:cobertura]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing cobertura:cobertura
[INFO] [aspectj:compile {execution: compile}]
[INFO] [resources:resources]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [cobertura:instrument]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 538 classes.
Instrumenting 538 files to C:\project\target\generated-classes\cobertura
Cobertura: Saved information on 538 classes.
Instrument time: 3938ms

[INFO] Instrumentation was successful.
[INFO] [aspectj:test-compile {execution: test-compile}]
[INFO] [resources:testResources]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] [compiler:testCompile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\project\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running project.AnnounceTypeActionTest

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.594 sec

Results :

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

[INFO] [cobertura:cobertura]
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 538 classes.
Javancss.(InputStream).e: net.sourceforge.cobertura.javancss.ParseException: Encountered "aspect" at line 5, column 8.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...

[cobertura] WARN  [main] net.sourceforge.cobertura.reporting.ComplexityCalculator - JavaNCSS got an error while parsing the java file C:\project\src\main\java\Project\logging\ApplicationLoggingAspect.aj
ParseException in STDIN
Last useful checkpoint: "project.logging."
Encountered "aspect" at line 5, column 8.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...


Javancss.(InputStream).e: net.sourceforge.cobertura.javancss.ParseException: Encountered "aspect" at line 4, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...

[cobertura] WARN  [main] net.sourceforge.cobertura.reporting.ComplexityCalculator - JavaNCSS got an error while parsing the java file C:\project\src\main\java\Project\logging\LoggingAspect.aj
ParseException in STDIN
Last useful checkpoint: "project.logging."
Encountered "aspect" at line 4, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...


Javancss.(InputStream).e: net.sourceforge.cobertura.javancss.ParseException: Encountered "aspect" at line 6, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...

[cobertura] WARN  [main] net.sourceforge.cobertura.reporting.ComplexityCalculator - JavaNCSS got an error while parsing the java file C:\project\src\main\java\Project\logging\TracingAspect.aj
ParseException in STDIN
Last useful checkpoint: "project.logging."
Encountered "aspect" at line 6, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...


Report time: 5891ms

[INFO] Cobertura Report generation was successful.
[INFO] Cobertura 1.9.2 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Cobertura: Loaded information on 538 classes.
Javancss.(InputStream).e: net.sourceforge.cobertura.javancss.ParseException: Encountered "aspect" at line 5, column 8.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...

[cobertura] WARN  [main] net.sourceforge.cobertura.reporting.ComplexityCalculator - JavaNCSS got an error while parsing the java file C:\project\src\main\java\Project\logging\ApplicationLoggingAspect.aj
ParseException in STDIN
Last useful checkpoint: "project.logging."
Encountered "aspect" at line 5, column 8.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...


Javancss.(InputStream).e: net.sourceforge.cobertura.javancss.ParseException: Encountered "aspect" at line 4, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...

[cobertura] WARN  [main] net.sourceforge.cobertura.reporting.ComplexityCalculator - JavaNCSS got an error while parsing the java file C:\project\src\main\java\Project\logging\LoggingAspect.aj
ParseException in STDIN
Last useful checkpoint: "project.logging."
Encountered "aspect" at line 4, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...


Javancss.(InputStream).e: net.sourceforge.cobertura.javancss.ParseException: Encountered "aspect" at line 6, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...

[cobertura] WARN  [main] net.sourceforge.cobertura.reporting.ComplexityCalculator - JavaNCSS got an error while parsing the java file C:\project\src\main\java\Project\logging\TracingAspect.aj
ParseException in STDIN
Last useful checkpoint: "project.logging."
Encountered "aspect" at line 6, column 17.
Was expecting one of:
    "@" ...
    "abstract" ...
    "final" ...
    "public" ...
    "synchronized" ...
    "strictfp" ...
    "class" ...
    "static" ...
    "protected" ...
    "private" ...
    "native" ...
    "transient" ...
    "volatile" ...
    "enum" ...
    "interface" ...


Report time: 3125ms

[INFO] Cobertura Report generation was successful.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30 seconds
[INFO] Finished at: Wed Sep 02 17:15:45 VET 2009
[INFO] Final Memory: 26M/254M
[INFO] ------------------------------------------------------------------------

最佳答案

我的项目使用 aspectj-maven-plugin 进行编译,并使用 cobertura-maven-plugin 进行代码覆盖。对于编译时编织,这在运行 mvn site 时工作正常,尽管测试被编译/运行两次以允许 Cobertura 编织。

使用以下配置运行 mvn site 会产生以下输出:

[INFO] Preparing surefire-report:report
[INFO] [aspectj:compile {execution: compile_with_aspectj}]
...
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
...
[INFO] [compiler:testCompile {execution: test-compile}]
[INFO] Compiling 8 source files to C:\test\aop-test
[INFO] [aspectj:test-compile {execution: test-compile_with_aspectj}]
...
[INFO] [surefire:test]
...
[INFO] Preparing cobertura:cobertura
[INFO] [aspectj:compile {execution: compile_with_aspectj}]
...
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [cobertura:instrument]
[INFO] Cobertura 1.8 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
Instrumenting 11 files to C:\test\aop-test\generated-classes\cobertura
Cobertura: Saved information on 11 classes.
Instrument time: 250ms

[INFO] Instrumentation was successful.
[INFO] [compiler:testCompile {execution: test-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [aspectj:test-compile {execution: test-compile_with_aspectj}]
...
[INFO] [surefire:test]
...
Cobertura: Coverage data file C:\test\aop-test\cobertura.ser either 
    does not exist or is not readable.  Creating a new data file.
Cobertura: Saved information on 8 classes.

我的配置包含在下面。

关于您方面的 Cobertura 警告,您可以尝试的一件事是将建议执行主体移动到 java 类型,而不是在方面中定义它。

例如,而不是这样做:

before(Throwable e, Object subject): handlers(e, subject) {
    Log logger = getTraceLog(subject.getClass());

    //code to trace exception handling
    ...
}

相反,您可以将实现委托(delegate)给 Java 类型,因此要处理的方面中的代码很少:

before(Throwable e, Object subject): handlers(e, subject) {
    Log logger = getTraceLog(subject.getClass());

    //get helper type and delegate processing to that type.
    getFormatterHelper().traceErrorHandling(logger, thisJoinPoint, e);
}

我对 aspectj 和 cobertura 插件的配置:

<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.2</version>
      <executions>
        <execution>
          <id>compile_with_aspectj</id>
          <goals>
            <goal>compile</goal>
          </goals>
          <configuration>
            <complianceLevel>1.5</complianceLevel>
          </configuration>
        </execution>
        <execution>
          <id>test-compile_with_aspectj</id>
          <goals>
            <goal>test-compile</goal>
          </goals>
          <configuration>
            <complianceLevel>1.5</complianceLevel>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>aspectj</groupId>
          <artifactId>aspectjtools</artifactId>
          <version>1.6.4</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>
...
<reporting>
  <plugins>
  ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <version>2.1</version>
      <configuration>
        <formats>
          <format>html</format>
          <format>xml</format>
        </formats>
        <outputDirectory>target/site/cobertura</outputDirectory>
      </configuration>
    </plugin>
  <plugins
</reporting>

关于maven-2 - maven cobertura 报告 0% 与 aspectj,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1370385/

相关文章:

java - Maven 使用 pom.xml 将 Maven 存储库从 {user}/.m2/repository 更改为 projectfolder

java - 具有简单类型的 AspectJ 连接点

java - Spring boot + Spring security with AspectJ 不工作

java - idea 应该指向什么 Maven 存储库?

jakarta-ee - 是否可以限制 Maven 构建的输出?

maven - 如何使用依赖库生成 JAR?

java - 重构方面的异常?

java - 使用 enunciate 为列表、数组、 map 等的消息正文生成示例 xml/json?

java - 无法执行目标 org.apache.maven.plugins :maven-surefire-plugin:2. 4.3:test (default-test) on project smrr: There are test failures

cxf - Maven CXF插件日志记录