title: How to make SonarQube module analyze the project only once when sonar analysis is bound to maven lifecycle in a multi-module project?
body: <p>What I am trying to achieve is integrate SonarQube analysis into the build process, so that whenever <code>mvn clean install</code> is run, the code is analyzed with SonarQube. We want to use it for local analysis and also for build on Jenkins. If new issues are found, than the build should fail (we want to use build breaker plugin for that). This way the developer would know that by his code his is going to introduce new issues, and will have to fix them for the build to work. </p>
  
  <p>When I run <code>mvn sonar:sonar</code>, the analysis takes 30 seconds, which is OK. </p>
  
  <p>However, the problem occurs when I am trying to bind <code>sonar</code> goal to maven build phases. I bind <code>sonar</code> to <code>verify</code> phase. The build takes <strong>5 minutes</strong> now, which is too long. It should take about <strong>1 minute</strong>. The build itself, without SonarQube analysis takes 30 seconds.</p>
  
  <p><strong>Note</strong> (may help to figure out what the problem is): the project on which the build is run has multiple modules in it, and I guess that is the problem. It looks like <code>sonar:sonar</code> goal is executed multiple times, once for each submodule, and the <strong>whole</strong> project is analyzed multiple times (not only the submodules). So, we have 4 submodules, and the report is generated 5 times during the build.</p>
  
  <p>Instead, we want to analyze the whole project only once, not 5 times. It's also important for this 1 analysis to be run at the end of the build, after the <strong>cobertura</strong> reports are generated for all modules.</p>
  
  <p>So, how do I integrate SonarQube analysis into the build, so that it analyzes my multi-module project only once, in the end, after cobertura reports are generated for all the submodules?</p>
  
  <p><strong>SonarQube plugin properties in parent pom:</strong></p>
  
  <pre><code>&lt;!-- Sonar plugin properties --&gt;
  &lt;sonar.jdbc.url&gt;jdbc:url&lt;/sonar.jdbc.url&gt;
  &lt;sonar.analysis.mode&gt;preview&lt;/sonar.analysis.mode&gt;
  &lt;sonar.issuesReport.html.enable&gt;true&lt;/sonar.issuesReport.html.enable&gt;       
  &lt;sonar.issuesReport.console.enable&gt;true&lt;/sonar.issuesReport.console.enable&gt;
  &lt;sonar.host.url&gt;sonar.host:9000&lt;/sonar.host.url&gt;
  &lt;sonar.language&gt;java&lt;/sonar.language&gt;
  &lt;sonar.buildbreaker.skip&gt;false&lt;/sonar.buildbreaker.skip&gt;
  &lt;sonar.qualitygate&gt;Sonar%20way%20with%20Findbugs&lt;/sonar.qualitygate&gt;
  &lt;sonar.preview.includePlugins&gt;buildbreaker&lt;/sonar.preview.includePlugins&gt;
  &lt;sonar.exclusions&gt;file:**/target/**&lt;/sonar.exclusions&gt;
  &lt;branch&gt;development&lt;/branch&gt;
  </code></pre>
  
  <p><strong>Plugins configuration in the project pom:</strong></p>
  
  <pre><code>                &lt;!-- Run cobertura analysis during package phase --&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                      &lt;artifactId&gt;cobertura-maven-plugin&lt;/artifactId&gt;
                      &lt;executions&gt;
                          &lt;execution&gt;
                             &lt;phase&gt;package&lt;/phase&gt;
                                &lt;goals&gt;
                                   &lt;goal&gt;cobertura&lt;/goal&gt;
                                 &lt;/goals&gt;
                          &lt;/execution&gt;
                    &lt;/executions&gt;
                  &lt;/plugin&gt;
  
                  &lt;!-- Run sonar analysis (preview mode) during verify phase. Cobertura reports need to be generated already --&gt;
                  &lt;plugin&gt;
                     &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                     &lt;artifactId&gt;sonar-maven-plugin&lt;/artifactId&gt;
                     &lt;version&gt;2.5&lt;/version&gt;
                     &lt;executions&gt;
                          &lt;execution&gt;
                             &lt;phase&gt;verify&lt;/phase&gt;
                                &lt;goals&gt;
                                   &lt;goal&gt;sonar&lt;/goal&gt;
                                 &lt;/goals&gt;
                          &lt;/execution&gt;
                    &lt;/executions&gt;
                  &lt;/plugin&gt;
  </code></pre>