Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, May 26, 2016

Tomcat7 Maven Plugin and JaCoCo

Tomcat7 Maven Plugin and JaCoCo


Is there any way to get code coverage using JaCoCo with the tomcat7-maven-plugin embedded instance?

The jacoco-maven-plugin is configured in my WAR's POM to instrument my unit tests, but I'm not sure how to attach the jacoco agent to the embedded Tomcat instance to instrument my integration tests that run against Tomcat. Given that the Tomcat instance is embedded, I'm not sure if this approach is possible. Is there any other way to accomplish this? I can probably switch from using the Tomcat Maven Plugin to using Cargo to get coverage, but I'd prefer to stick with the Tomcat plugin if possible.

Here are a few relevant snippets from my POM:

      org.jacoco      jacoco-maven-plugin      0.6.2.201302030002                                                prepare-agent                                        maven-failsafe-plugin      2.14                              integration-tests                                integration-test                  verify                                        org.apache.tomcat.maven      tomcat7-maven-plugin      2.1                                            -javaagent:${project.build.directory}/${jacoco.jar}=destfile=${project.build.directory}/jacoco.exec,append=true                                              tomcat-startup                                run-war-only                            pre-integration-test                                true                                                tomcat-shutdown                                shutdown                            post-integration-test                    

Versions: Maven 3.0.4, Tomcat Maven Plugin 2.1, Jacoco 0.6.2.201302030002, Java 7

Answer by Andre Piantino for Tomcat7 Maven Plugin and JaCoCo


You don't need pass JAVA_OPTS to tomcat embedded if you use maven-failsafe-plugin (or maven-surefire-plugin) to run yours integration test. It is because tomcat embedded run in the same process of maven-failsafe-plugin.

So when jacoco-maven-plugin execute prepare-agent it sets argLine that maven-failsafe-plugin uses too.

I created a project to test this, below part of pom:

                              org.jacoco              jacoco-maven-plugin              0.6.2.201302030002                                                                                prepare-agent                                                                                                    my.project.package.only.*                                                                                                                  org.apache.tomcat.maven              tomcat7-maven-plugin              2.1                                                      tomcat-startup                                                run-war-only                                            pre-integration-test                                                true                                                                                tomcat-shutdown                                                shutdown                                            post-integration-test                                                                  org.apache.maven.plugins              maven-failsafe-plugin                                                      integration-tests                      integration-test                                                integration-test                                                                                        org.codehaus.mojo              selenium-maven-plugin              2.3                                                      start                      pre-integration-test                                                start-server                                                                      true                          true                          true                                                                                stop                      post-integration-test                                                stop-server                                                                         

Answer by ronapelbaum for Tomcat7 Maven Plugin and JaCoCo


you can try to take a look at a workaround at this post: http://dougonjava.blogspot.co.il/2013/07/integration-testing-using-maven-tomcat.html

Answer by Ondrej Burkert for Tomcat7 Maven Plugin and JaCoCo


I resolved problems when setting up JaCoCo agent with embedded Tomcat by instrumenting classes offline and then just placing JaCoCo agent on Tomcat classpath (plugin dependency) and adding file jacoco-agent.properties.

I put the working configuration on my blog:

http://burkond.blogspot.de/2014/05/selenium-in-sonar-code-coverage-metrics.html

Answer by Koder for Tomcat7 Maven Plugin and JaCoCo


I know its been awhile since the question was posted but I don't feel the answer really addressed the root of the problem. Code coverage may work with failsafe or surefire if you are running tests within those plugins. However, if you just want to monitor tomcat with jacoco to get a coverage report current information doesn't provide that. I found that the tomcat7-maven-plugin doesn't allow you to inject the -javaagent for jacoco by simply providing JAVA_OPTS. Switching to cargo I was able to do that like so.

      org.jacoco      jacoco-maven-plugin      0.7.2.201409121644                ${sonar.jacoco.reportPath}          ${sonar.jacoco.reportPath}          ${project.reporting.outputDirectory}/jacoco-it          ${project.reporting.outputDirectory}/jacoco-it/classes          ${skipITs}                                    jacoco-agent              pre-integration-test                                prepare-agent                                              ${sonar.jacoco.reportPath}                  jacoco.agent.itArgLine                                                jacoco-report              post-integration-test                                dump                  report                                            org.codehaus.cargo      cargo-maven2-plugin      1.4.11                ${skipITs}                        tomcat7x                                http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip                                    ${project.build.directory}/downloads                  ${project.build.directory}/extracts                                                                    ojdbc                      ojdbc6                                                                  ${project.build.directory}/catalina-base                                ${jacoco.agent.itArgLine},output=tcpserver,port=6300 -Drunmode=TEST                  9090                                                                    ${basedir}/src/test/conf/context.xml                      conf/Catalina/localhost/                      context.xml.default                                                                              start-tomcat              pre-integration-test                                start                                                stop-tomcat              post-integration-test                                stop                                  

the important parts are:

  1. org.jacoco ...jacoco.agent.itArgLine
  2. ${jacoco.agent.itArgLine},output=tcpserver,port=6300

When report target is run on the jacoco plugin it will create a directory in ${projectbase}/target/site/jacoco-it/index.html with your coverage report. I use this with the soapui-maven-plugin but it could be used with selenium-maven-plugin also.

Answer by dokaspar for Tomcat7 Maven Plugin and JaCoCo


I had the exact same problem and the only solution I found was to set the MAVEN_OPTS previously to the Maven build (for example on the command line or in the configuration of a Jenkins job):

export MAVEN_OPTS=-javaagent:~/.m2/repository/org/jacoco/org.jacoco.agent/0.7.4.201502262128/org.jacoco.agent-0.7.4.201502262128-runtime.jar=destfile=./target/jacoco.exec,append=true  

This will attach the jacoco agent to the embedded tomcat instance, which will report back the coverage results into the given destfile.

First, it is important that the path to the jacoco runtime JAR is correct. You can manually download and refer to it or use another Maven command to download it into your local .m2 repository:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.jacoco:org.jacoco.agent:0.7.4.201502262128:jar:runtime  

Second, make sure that the path to the jacoco.exec file is correct. In my case, I already have an existing jacoco.exec file in the target folder, which contains unit test results. The append=true makes sure that unit and integration tests are combined.

Answer by Oliver Kuster for Tomcat7 Maven Plugin and JaCoCo


I managed to do it and it involves some tinkering with finicky stuff:

  • server/container needs to be on a separate jvm that can receive arguments (jacoco-agent). Cargo using embedded containers did not seem to work and was a pain to debug...
  • jvm with jacoco-it needs to stop before the jacoco analysis (duh!) but registering container-stop and jacoco-report on post-integration-test does not guarantee this... (the tcpdump, etc option in a previous answer had this problem)
  • defining random ports for the server/container makes this easy to integrate with continuous integration
  • phantomjs is an extra ;)
  • jacoco should be used as prepare-agent-integration and report-integration for integration-test (does not really make a difference)

Should be run as 'mvn clean verify'

Pom:

      org.jacoco      jacoco-maven-plugin      0.7.4.201502262128                                        jacoco-pre-unit-test                                prepare-agent                                              ${project.build.directory}/coverage-reports/jacoco-ut.exec                  jacoco.ut.argLine                                                jacoco-post-unit-test              test                                report                                              ${project.build.directory}/coverage-reports/jacoco-ut.exec                  ${project.reporting.outputDirectory}/jacoco-ut                                                            jacoco-pre-integration-test              pre-integration-test                                prepare-agent-integration                                              ${project.build.directory}/coverage-reports/jacoco-it.exec                  jacoco.it.argLine                                                  jacoco-post-integration-test              post-integration-test                                 report-integration                                              ${project.build.directory}/coverage-reports/jacoco-it.exec                  ${project.reporting.outputDirectory}/jacoco-it                                        org.codehaus.mojo      sonar-maven-plugin      2.6              com.github.klieber      phantomjs-maven-plugin      0.4                                            test                                install                                              1.9.7                    org.codehaus.mojo      build-helper-maven-plugin      1.6                              jetty.port              jetty.port.stop                                              reserve-port              test                                reserve-network-port                                            org.apache.maven.plugins      maven-surefire-plugin      2.18.1                ${jacoco.ut.argLine}          ${skip.unit.tests}          true                                                      org.apache.maven.plugins      maven-failsafe-plugin      2.17                                            ${phantomjs.binary}              ${jetty.port}                                              integration-test                                integration-test                  verify                                          org.codehaus.cargo      cargo-maven2-plugin      1.4.16                ${skipITs}                        tomcat8x                                                  http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.26/bin/apache-tomcat-8.0.26.zip                  ${project.build.directory}/downloads                  ${project.build.directory}/extracts                                                ${project.build.directory}/catalina-base                                ${jacoco.it.argLine}                  ${jetty.port}                                                                              cargo-start-tomcat              pre-integration-test                                start                                                cargo-stop-tomcat              integration-test                                stop                                  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.