OpenClover-与AspectJ一起使用 [英] OpenClover - Getting to work with AspectJ

查看:344
本文介绍了OpenClover-与AspectJ一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Openclover与使用AspectJ并将项目纳入其代码的项目一起使用.

I'm trying to use Openclover with a project that uses AspectJ and that instruments aspects into its code.

pom.xml具有与AspectJ相关的依赖性:

The pom.xml has these dependencies with relation to AspectJ:

...
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.9</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.9</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
...

这些插件:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.openclover</groupId>
            <artifactId>clover-aspectj-compiler</artifactId>
            <version>1.0.0</version>
        </plugin>

        <plugin>
            <groupId>org.openclover</groupId>
            <artifactId>clover-maven-plugin</artifactId>
            <version>4.2.0</version>
            <executions>
                <execution>
                    <id>clover</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>instrument</goal>
                        <goal>clover</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

我正在使用两个插件来执行此操作:clover-maven-plugin是代码覆盖率工具,而clover-aspectj-compiler是AspectJ编译器的包装,该包装允许使用OpenClover进行代码检测.

I'm using two plugins to do this: clover-maven-plugin which is a code coverage tool and clover-aspectj-compiler, a wrapper for AspectJ compiler which allows code instrumentation using OpenClover.

我得到的错误如下:

[ERROR] QueryAspect.java:48:0::0 The type QueryAspect is already defined
[ERROR] LogAspect.java:35:0::0 The type LogAspect is already defined

关于此的文档太少了(或者更好,没有),而且我似乎无法使AspectJ与OpenClover一起使用,并且网络上没有太多帮助.

The documentation about this is just too little (or better, none) and I can't seem to make AspectJ work with OpenClover, and there isn't much help on the web.

谢谢

推荐答案

如我们的注释中所述,您可以只使用 AspectJ Maven 而不是 Clover AspectJ .您只需要采取一些预防措施即可使其正常工作:

As discussed in our comments, you can just use AspectJ Maven instead of Clover AspectJ. You just need to take a few precautions in order to get it to work:

  • 我喜欢将AspectJ Maven执行放入process-sources阶段,以确保AspectJ编译器在Maven Compiler插件启动的普通Java编译器之前启动.您也可以停用Maven编译器,因为Ajc可以完全替代Javac.实际上,该阶段曾经是较早的插件版本中的默认阶段,但已答案中也提到了.

  • I like to put AspectJ Maven executions into the process-sources phase in order to make sure the AspectJ compiler kicks in before the normal Java compiler kicked off by Maven Compiler plugin. You could also deactivate Maven Compiler instead as Ajc is a full replacement for Javac. Actually that phase used to be the default in older plugin versions, but it has been changed long ago, which is also mentioned in an answer on SO.

Maven编译器中存在一个问题,即开关useIncrementalCompilation似乎具有相反的逻辑.这就是为什么需要将其设置为false使其起作用的原因.否则,它将尝试重新编译AspectJ已编译的内容,从而破坏Aspect编织.参见 MCOMPILER-209

There is a problem in Maven Compiler, namely the switch useIncrementalCompilation seems to have reversed logic. This is why you need to set it to false in order to make it work. Otherwise it tries to recompile stuff already compiled by AspectJ, breaking aspect weaving. See MCOMPILER-209 and MCOMPILER-194, I explained the problem and its solution there in my posts.

现在唯一真正与OpenClover(OC)有关的问题:AspectJ(AJ)对OC向每种方法添加源代码以实现代码覆盖方面一无所知.不幸的是,OC还不了解AJ,并且还在注释样式的切入点中添加了代码,这些切入点定义为具有@Pointcut注释的空方法.由于OC需要在AJ编译之前尽其所能,AJ编译器会抱怨切入点中发现了意外的代码,并因错误而停止了编译.至少有两种方法可以避免这种情况:

Now the only issue actually related to OpenClover (OC): AspectJ (AJ) does not know anything about OC adding source code to each method in order to enable code coverage. Unfortunately OC also does not know about AJ and also adds code to annotation-style pointcuts defined as empty methods with a @Pointcut annotation. As OC needs to do its magic before AJ compiles, the AJ compiler complains about unexpected code found in the pointcut and stops compilation with an error. There are at least two ways to avoid that:

  • 您可以将所有切入点内联到相应的@Before@After@Around等中.使用这些建议通常是有效的,但在需要使用参数绑定的情况下,并非始终是一种选择切入点,以实现像execution(pointcutA()) && cflow(execution(pointcutB(myArgument)))这样的虫洞模式.

  • You can either inline all pointcuts into the respective @Before, @After, @Around etc. advices using them, which usually works, but is not always an option in cases where you need argument binding in pointcuts in order to implement a wormhole pattern like execution(pointcutA()) && cflow(execution(pointcutB(myArgument))).

或者您可以从OC工具中排除所有方面,如果它们驻留在一个不需要其他Java类的包中,这是最简单的.然后,您可以使用简单的排除方式,例如您的情况<exclude>codeaspects/**</exclude>.这就是我在修复时在拉请求中所做的事情您的项目.

Or you can exclude all aspects from OC instrumentation, which is easiest if they reside in one package where there are no other Java classes which need to be instrumented. Then you can use a simple exclusion like in your case <exclude>codeaspects/**</exclude>. This is what I was doing in my pull request when fixing your project.

最简单的方法是将所有方面从*.java重命名为*.aj,这是无论如何命名它们的规范方法.我刚刚在您的项目中尝试过,并且效果很好. AspectJ Maven仍然会寻找那些文件,但是OC会忽略它们,甚至不会为缺少覆盖范围而计算它们的代码行.您也可以摆脱上面提到的<exclude>,请参见此提交.

The easiest way is to just rename all aspects from *.java to *.aj, which is the canonical way of naming them anyway. I just tried in your project, and it works beautifully. AspectJ Maven looks for those files anyway, but OC will ignore them, not even calculating their lines of code for missing coverage. You can also get rid of the <exclude> mentioned above, see this commit.

也许这一切都是四叶草AspectJ自动处理的,我从未尝试过.也许那个编译器包装的作者应该在文档中实际解释它的功能以及它的工作方式,尤其是如何在Maven中使用它.否则,使用它就没有太大意义.

Maybe all of this is automatically taken care of by Clover AspectJ, I never tried. Maybe the author of that compiler wrapper should actually explain what it does and how it works in the documentation, especially how to use it with Maven. Otherwise it does not make much sense to use it.

这篇关于OpenClover-与AspectJ一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆