Maven + Java包声明 [英] Maven + Java package declaration

查看:113
本文介绍了Maven + Java包声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个带有 Java 文件的 Maven 项目(打包为JAR)。最近在此项目中添加了一个新的 Java 源文件。添加Java文件的路径与其包声明不匹配。正如所料,Eclipse在类中显示错误的错误。但是,Maven很好地构建了这个项目。在生成的JAR文件中,.class文件存在于包声明指示的路径中。我们尝试将Java源文件移动到其他不正确的文件夹(即与包声明不同),但每次Maven构建项目都很好。

We have a Maven project (packaged as JAR) with Java files. A new Java source file was recently added to this project. The path in which the Java file was added, does not match its package declaration. As expected, Eclipse shows an error in the class for the mismatch. However, Maven builds the project just fine. In the generated JAR file, the .class file is present in the path indicated by the package declaration. We tried moving the Java source file to other incorrect folders (i.e. different from the package declaration), but every time Maven builds the project fine.

那么,Maven是否会忽略.java文件所在的实际目录?是否只考虑包声明?

So, does Maven ignore the actual directory in which the .java file is present? Does it only consider package declaration?

推荐答案

Maven编译器插件在内部组装一个命令行调用,其中包含传递给javac的参数,一个每个源文件的参数(使用 sourcefiles javac电话的版本)。它确实传递了实际的源文件夹,也用于注释处理之类的元功能,但是当单个源文件传递给javac时,这些优先级并且编译器没有有效的方法来找出哪个文件属于哪个源文件夹,因此可以'验证包结构。

The Maven Compiler plugin internally assembles a command line call containing the arguments passed to javac, one argument per source file (using the sourcefiles version of the javac call). It does pass the actual source folders also for meta-capabilities like annotation processing, but when individual source files are passed to javac, these take precedence and the compiler has no efficient way to find out which file belongs to which source folder and hence can't validate the package structure.

Maven编译器插件内部使用 Plexus编译器API ,您可以在这两个类中找到相关代码:

The Maven Compiler Plugin internally uses the Plexus Compiler API, and you can find the relevant code in these two classes:

  • plexus-compiler-api: AbstractCompiler
  • plexus-compiler-javac: JavacCompiler

这篇关于Maven + Java包声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆