我有一个相当大的Maven项目,其中包含许多子项目,我的竹子实例使用“ mvn clean deploy”运行连续构建。此外,还有一个单独的每晚“ mvn clean sonar:sonar”失败,并显示以下错误。

MediaServiceType是正在编译的子项目的主要Java部分中的Enum,它实现了另一个项目的接口。 AbstractToolTest与MediaServiceType位于同一软件包中,因此它没有显示完整的软件包名称。问题所在的行只是从接口类型转换为MediaSerivceType。

我假设我的pom出了点问题,但是看着-X的构建,Java编译器实际上注销了类路径,并且其中包含具有MediaServiceType.class的文件夹。我也不确定该怎么做的错误是“无法访问MediaServiceType”,未找到类。

是什么引起这种问题?

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


打开详细日志记录后,我得到以下信息

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);


并且类文件存在意味着丢失的不是MediaServiceType而是其他。

现在,我发现,如果我删除了声纳构建的运行,则应归咎于MediaServiceType中使用的javax.annotations.Nonnull。同样运行mvn测试也可以,但是如果我运行声纳,则测试编译会失败,因此,我很确定现在是声纳,而不是我的pom文件。

在--debug日志上对测试与声纳构建进行差异测试,我可以看到,当声纳运行时,以下额外的jar会放在编译类路径中:


cobertura-1.9.4.1.jar
ant-1.7.0.jar
ant-launcher-1.7.0.jar


也没有使用目标/类作为主要类,而是使用了生成类/ cobertura。

目前,我通过从cobertura配置中忽略MediaServiceType来解决此问题

            <configuration>
                <instrumentation>
                    <excludes>
                        <exclude>**/MediaServiceType.class</exclude>
                        <exclude>**/*Parameter.class</exclude>
                    </excludes>
                </instrumentation>
            </configuration>


参数枚举似乎有相同的问题,它们也是使用@Nonnull的枚举。

最佳答案

您是否检查了包含MediaServiceType和AbstractToolTest的依赖项在测试范围内。 <scope>test</scope>。该测试找不到该类,因此它可能不在范围内。

09-10 13:52