title: Maven Compilation failure cannot access Enum with @Nonnull annotation when sonar:sonar plugin runs
body: <p>I have a fairly large maven project with a bunch of subprojects, my bamboo instance runs continuous builds with "mvn clean deploy". Also there is a separate nightly "mvn clean sonar:sonar" that is failing with the error below. </p>
  
  <p>MediaServiceType is an Enum in the main java part of the subproject being compiled, it implements an interface from another project. AbstractToolTest is in the same package as MediaServiceType hence it doesn't show the full package name. The line in question is just casting from the interface type to MediaSerivceType.</p>
  
  <p>I assume there is something wrong with my pom, but looking at the build with -X the java compiler actually logs out the classpath and that includes a folder that has MediaServiceType.class. I'm also not sure what to make of the error being "cannot access MediaServiceType", not class not found.</p>
  
  <p>What could cause this sort of problem? </p>
  
  <pre><code>21-Mar-2013 10:45:46    [INFO] ------------------------------------------------------------------------
  21-Mar-2013 10:45:46    [ERROR] BUILD ERROR
  21-Mar-2013 10:45:46    [INFO] ------------------------------------------------------------------------
  21-Mar-2013 10:45:46    [INFO] Can not execute Sonar
  21-Mar-2013 10:45:46    
  21-Mar-2013 10:45:46    Embedded error: Unable to execute maven plugin
  21-Mar-2013 10:45:46    Compilation failure
  21-Mar-2013 10:45:46    /data/bamboo/xml-data/build-dir/PRO-PROTRUNKSONAR-JOB1/processors/processor-tool/src/test/java/company/prod/processor/AbstractToolTest.java:[20,52] error: cannot access MediaServiceType
  21-Mar-2013 10:45:46    
  21-Mar-2013 10:45:46    
  21-Mar-2013 10:45:46    [INFO] ------------------------------------------------------------------------
  21-Mar-2013 10:45:46    [DEBUG] Trace
  21-Mar-2013 10:45:46    org.apache.maven.lifecycle.LifecycleExecutionException: Can not execute Sonar
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:284)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
  21-Mar-2013 10:45:46        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
  21-Mar-2013 10:45:46        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
  21-Mar-2013 10:45:46        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
  21-Mar-2013 10:45:46        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
  21-Mar-2013 10:45:46        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  21-Mar-2013 10:45:46        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  21-Mar-2013 10:45:46        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  21-Mar-2013 10:45:46        at java.lang.reflect.Method.invoke(Method.java:601)
  21-Mar-2013 10:45:46        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
  21-Mar-2013 10:45:46        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
  21-Mar-2013 10:45:46        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
  21-Mar-2013 10:45:46        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
  21-Mar-2013 10:45:46    Caused by: org.apache.maven.plugin.MojoExecutionException: Can not execute Sonar
  21-Mar-2013 10:45:46        at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:103)
  21-Mar-2013 10:45:46        at org.codehaus.mojo.sonar.Bootstraper.start(Bootstraper.java:79)
  21-Mar-2013 10:45:46        at org.codehaus.mojo.sonar.SonarMojo.execute(SonarMojo.java:88)
  21-Mar-2013 10:45:46        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
  21-Mar-2013 10:45:46        ... 17 more
  21-Mar-2013 10:45:46    Caused by: org.sonar.api.utils.SonarException: Unable to execute maven plugin
  21-Mar-2013 10:45:46        at org.sonar.batch.AbstractMavenPluginExecutor.execute(AbstractMavenPluginExecutor.java:51)
  21-Mar-2013 10:45:46        at org.sonar.batch.AbstractMavenPluginExecutor.execute(AbstractMavenPluginExecutor.java:38)
  21-Mar-2013 10:45:46        at org.sonar.batch.phases.InitializersExecutor.executeMavenPlugin(InitializersExecutor.java:73)
  21-Mar-2013 10:45:46        at org.sonar.batch.phases.InitializersExecutor.execute(InitializersExecutor.java:60)
  21-Mar-2013 10:45:46        at org.sonar.batch.phases.Phases.execute(Phases.java:90)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.ProjectModule.doStart(ProjectModule.java:143)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.Module.start(Module.java:83)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.BatchModule.analyze(BatchModule.java:111)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.BatchModule.analyze(BatchModule.java:106)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.BatchModule.analyze(BatchModule.java:106)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.BatchModule.doStart(BatchModule.java:101)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.Module.start(Module.java:83)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.BootstrapModule.doStart(BootstrapModule.java:102)
  21-Mar-2013 10:45:46        at org.sonar.batch.bootstrap.Module.start(Module.java:83)
  21-Mar-2013 10:45:46        at org.sonar.batch.Batch.execute(Batch.java:100)
  21-Mar-2013 10:45:46        at org.sonar.maven.SonarMojo.executeBatch(SonarMojo.java:152)
  21-Mar-2013 10:45:46        at org.sonar.maven.SonarMojo.execute(SonarMojo.java:142)
  21-Mar-2013 10:45:46        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
  21-Mar-2013 10:45:46        at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:98)
  21-Mar-2013 10:45:46        ... 21 more
  21-Mar-2013 10:45:46    Caused by: org.apache.maven.BuildFailureException: Compilation failure
  21-Mar-2013 10:45:46    /data/bamboo/xml-data/build-dir/PRO-PROTRUNKSONAR-JOB1/processors/processor-tool/src/test/java/company/prod/processor/AbstractToolTest.java:[20,52] error: cannot access MediaServiceType
  21-Mar-2013 10:45:46    
  21-Mar-2013 10:45:46    
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:715)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkProjectLifecycle(DefaultLifecycleExecutor.java:1205)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.forkLifecycle(DefaultLifecycleExecutor.java:1038)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:643)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
  21-Mar-2013 10:45:46        at org.sonar.maven.Maven2PluginExecutor.concreteExecute(Maven2PluginExecutor.java:52)
  21-Mar-2013 10:45:46        at org.sonar.batch.AbstractMavenPluginExecutor.execute(AbstractMavenPluginExecutor.java:49)
  21-Mar-2013 10:45:46        ... 39 more
  21-Mar-2013 10:45:46    Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
  21-Mar-2013 10:45:46    /data/bamboo/xml-data/build-dir/PRO-PROTRUNKSONAR-JOB1/processors/processor-tool/src/test/java/company/prod/processor/AbstractToolTest.java:[20,52] error: cannot access MediaServiceType
  21-Mar-2013 10:45:46    
  21-Mar-2013 10:45:46    
  21-Mar-2013 10:45:46        at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:516)
  21-Mar-2013 10:45:46        at org.apache.maven.plugin.TestCompilerMojo.execute(TestCompilerMojo.java:102)
  21-Mar-2013 10:45:46        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
  21-Mar-2013 10:45:46        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
  21-Mar-2013 10:45:46        ... 50 more
  </code></pre>
  
  <p>After turning on verbose logging I get the following</p>
  
  <pre><code>21-Mar-2013 17:55:07    [loading RegularFileObject[/data/bamboo/xml-data/build-dir/PRO-PROTRUNKSONAR-JOB1/processors/processor-tool/target/generated-classes/cobertura/company/prod/processor/MediaServiceType.class]]
  21-Mar-2013 17:55:07    /data/bamboo/xml-data/build-dir/PRO-PROTRUNKSONAR-JOB1/processors/processor-tool/src/test/java/company/prod/processor/AbstractToolTest.java:20: error: cannot access MediaServiceType
  21-Mar-2013 17:55:07            return MediaServiceFactory.getMediaService((MediaServiceType) serviceType, ppt2TextPath, pdfwmkPath);
  </code></pre>
  
  <p>And the class file exists meaning it's not MediaServiceType that is missing but something else.</p>
  
  <p>I've now found that the blame has something to do with javax.annotations.Nonnull being used in MediaServiceType, if I remove that the sonar build runs. Also running mvn test works fine, but if I run sonar the test compile fails, so I'm pretty sure now the blame is with sonar, not my pom file.</p>
  
  <p>Doing a diff on the --debug logs for the test vs the sonar build I can see that when sonar is running, the following extra jars are put on the compilation classpath:</p>
  
  <ul>
  <li>cobertura-1.9.4.1.jar </li>
  <li>ant-1.7.0.jar </li>
  <li>ant-launcher-1.7.0.jar</li>
  </ul>
  
  <p>also instead of using target/classes for the main classes it uses generated-classes/cobertura. </p>
  
  <p>At the moment I've worked around this problem by ignoring MediaServiceType from the cobertura configuration</p>
  
  <pre><code>            &lt;configuration&gt;
                  &lt;instrumentation&gt;
                      &lt;excludes&gt;
                          &lt;exclude&gt;**/MediaServiceType.class&lt;/exclude&gt;
                          &lt;exclude&gt;**/*Parameter.class&lt;/exclude&gt;
                      &lt;/excludes&gt;
                  &lt;/instrumentation&gt;
              &lt;/configuration&gt;
  </code></pre>
  
  <p>Parameter enums seem to have the same problem, they are also enums that use @Nonnull.</p>