I'm in the process of trying to migrate a large code base from Java 8 to modular (JPMS) Java 11, and I'm encountering significant pain and finding consistent advice on project structure and how to use module-info files for actual production projects is hard.
The project in question follows the conventional gradle structure for source and test files:
src/main/java/org/abc/...
src/test/java/org/abc/...
I have a module-info.java
file in src/main/java/module-info.java
, is this the right location? The quick-start for modular java and this seems to contradict this; however other resources do it the way I did.
When trying to run a unit test that looks like this:
@RunWith(MockitoJUnitRunner.class)
public class ABCTest {
@Mock
public SomeClass mock;
...
}
I get:
Unable to make field public SomeClass mock accessible: module org.abc does not "exports org.abc"
to module org.mockito
which indicates that I need to add exports org.abc to org.mockito
in my module-info.java
file but this seems clunky and verbose having to do this for all sub packages in my test tree which aren't even part of the module, and it seems like the wrong place to do it since the tests shouldn't be exported with the release jar.
The seemingly most up to date guidance I could find suggests adding a module-info.[test|java]
file to my test tree. What is the difference between these two files (.java
and .test
)? I couldn't find any hits on Google explaining this. Also I'm supposedly going to have to copy content from the main/java/module-info.java
to the test/java/module-info.test
and keep them in sync? This seems tedious.
I also understand that gradle doesn't have native support for modular java yet and one must rely on plugins for this, what are the "defacto" plugins that one should use?
I'm confused by the information available, it seems contradicting, very low level or not applicable when using gradle. Could some one please provide an example project with conventional source layout using mockito, junit, gradle and modular java (version 9+, preferably 11)?