2

我有一个相当大的 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 test 也可以正常工作,但是如果我运行 sonar 测试编译失败,所以我很确定现在应该归咎于声纳,而不是我的 pom 文件。

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

  • cobertura-1.9.4.1.jar
  • 蚂蚁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 的枚举。

4

2 回答 2

2

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

于 2013-03-21T12:44:17.930 回答
0

确保您对旧版本没有问题(您最近添加了 Enum 吗?)。

您可以尝试暂时移走竹子主文件夹中的所有内容~/.m2/repository,看看是否有帮助。我担心您可能出于某种原因在本地存储库中使用捆绑包而不是实际模块。如果这确实有效,您必须找出原因;)

于 2013-03-22T10:27:11.557 回答