Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven + Java package declaration

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.

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

like image 487
Vasan Avatar asked Nov 16 '12 10:11

Vasan


1 Answers

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.

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
like image 51
Sean Patrick Floyd Avatar answered Nov 03 '22 15:11

Sean Patrick Floyd