How do I get Emma or Cobertura, with Maven, to report coverage on source code in other modules?

前端 未结 5 824
孤城傲影
孤城傲影 2021-02-01 08:57

I have a multi-module Maven setup with Java code.

My unit tests, in one of the modules, exercise code in multiple modules. Naturally, the modules have inter-dependenc

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 09:55

    Never tried, but this may be a way to accomplish it:

    • In each module, just before the install phase, let cobertura instrument the jar files and install the instrumented jar files (!) into the local Maven repository
    • In the tests-module, Maven will use the artifact dependencies from the local Maven repository to run the tests. These instrumented classes should now appear in the datafile, e.g. cobertura.ser
    • Run the cobertura-report generation as usual from within the tests-module of your project, e.g. mvn site

    See cobertura documentation on how to manually invoke cobertura to instrument external JAR files in-place:

    ... You can also pass in jar files to be instrumented using standard ant filesets. Cobertura will extract each class from the jar and instrument it. If 'todir' was not specified then the original jar will be overwritten with an instrumented version ...

    The pom.xml's build plugins may look like this - you may want to add a profile or use classifiers to distinguish between the final jar file and the instrumented jar file if you don't want to overwrite them in your local repo. Then, in the tests module, you just need to define the dependencies to your other modules using the classifiers.

            
                org.apache.maven.plugins
                maven-antrun-plugin
                
                    
                        cobertura-inplace-instrumentation
                        package
                        
                            
                                
                                
                                    
                                        
                                    
                                
                            
                        
                        
                            run
                        
                    
                
                
                    
                        net.sourceforge.cobertura
                        cobertura
                        1.9.4.1
                    
                
            
    

提交回复
热议问题