Issue
Situation
I did the following with a project from GitHub
- cloned the project: git clone https://github.com/antlr/antlr4.git
- opened the project with: code antlr4
- opened a file in the project: Antlr4MojoTest.java
The file is found under the following path in the project: antlr4/antlr4-maven-plugin/src/test/java/org/antlr/mojo/antlr4/Antlr4MojoTest.java
VSCode tells me: The declared package "org.antlr.mojo.antlr4" does not match the expected package "java.org.antlr.mojo.antlr4"
Question
Why does vscode think the expected package is java.org.antlr.mojo.antlr4
?
Solution
An Explanation
VSCode expects the class to be in the package java.org.antlr.mojo
because the plugin pom is set up to include src/test
as was pointed out by @Sweeper. Here's the line from the module pom:
<testSourceDirectory>src/test</testSourceDirectory>
VSCode correctly understands everything under this to be a package for the included classes.
The remedy is to either change the directory structure to match the pom (eg move the contents of the java
dir up one level) or change the pom to match the directory structure (eg add '/java' to the end of the testSourceDirectory tag.
IMO changing the pom is the better solution. Specifically the testSourceDirectory tag should be deleted in its entirety since the default testSourceDirectory already is 'src/test/java' as of version 3.8.6 of the super-pom (org.apache.maven:maven-model-builder:3.8.6:super-pom). No need to configure something that doesn't need configuring because the default will do (and in this case is correct).
Answered By - Chris
Answer Checked By - David Marino (JavaFixing Volunteer)