解决Java+Scala项目构建时MyClass_类源文件无法识别警告
解决scala-maven-plugin报"Could not determine source for class"警告的方法
针对你遇到的混合Java/Scala项目构建警告问题,核心原因是Hibernate JPAMetaModelEntityProcessor生成的元模型类(如MyClass_)存储在target/scoverage-classes目录,scala-maven-plugin无法关联到对应的源文件。以下是几种合理的解决方式:
方法一:直接让scala-maven-plugin忽略生成的元模型类
这是最快捷的方案,通过配置插件排除所有JPAMetaModel生成的*_格式类,避免插件尝试查找它们的源文件:
<plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>你的插件版本</version> <executions> <!-- 保留你原有的execution配置 --> </executions> <configuration> <excludes> <!-- 排除所有下划线结尾的类文件和源文件 --> <exclude>com/mycompany/*_.java</exclude> <exclude>com/mycompany/*_.class</exclude> </excludes> </configuration> </plugin>
方法二:调整JPAMetaModel生成目录到Maven认可的源码路径
把元模型生成的源文件放到Maven标准的生成源码目录,让scala-maven-plugin能自动识别到:
- 配置maven-compiler-plugin,指定注解处理器的生成目录:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <annotationProcessors> <annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor> </annotationProcessors> <!-- 指定生成目录为Maven标准的生成源码路径 --> <generatedSourcesDirectory>${project.build.directory}/generated-sources/java</generatedSourcesDirectory> </configuration> </plugin>
- 将生成目录添加到项目源码路径中:
<build> <sources> <source>${project.build.directory}/generated-sources/java</source> </sources> </build>
这样scala-maven-plugin在扫描源码时就能找到MyClass_.java,不会再报警告。
方法三:配置scoverage插件排除元模型类
如果scoverage插件参与了类文件的处理,让它跳过元模型类的扫描,避免干扰scala-maven-plugin的源文件关联:
<plugin> <groupId>org.scoverage</groupId> <artifactId>scoverage-maven-plugin</artifactId> <version>你的插件版本</version> <configuration> <excludes> <exclude>com/mycompany/*_.java</exclude> </excludes> </configuration> </plugin>
内容的提问来源于stack exchange,提问作者kc2001
相关产品推荐
相关产品推荐

