How is package-info.java shared between main and test in a maven project?

717 Views Asked by At

In a maven project, I created a simple Unit test which loads all classes within a given root package, iterates over them and processes some information about their packages (namely whether a specific annotation is provided for a given package).

Given the following files:

/src/main/com/andy/Foo.java
/src/main/com/andy/package-info.java
/src/main/com/andy/inner/Bar.java

src/test/com/andy/inner/SomeTest.java
src/test/com/andy/inner/package-info.java

when I loaded the Class<?> for Bar.java, and tried to obtain annotation of its package (using the Class#getPackage method), to my surpise I found out that the package somehow shared the annotations from the src/test directory, where the package-info.java lies next to the SomeTest.java file (otherwise the packages are the same).

I have also found out it works the other way around as well, i.e. a class without package-info.java in src/test seems to share a package-info.java from the same package under the src/main directory.

Is this specific to a maven project, or is this a regular Java behaviour? Is there a way to not load the related package from the other directory?

0

There are 0 best solutions below