简体   繁体   English

为什么 vscode 认为声明的 package 与预期的 package 不匹配?

[英]Why does vscode think the declared package doesn't match the expected package?

Situation情况

I did the following with a project from GitHub我对来自 GitHub 的项目进行了以下操作

  1. cloned the project: git clone https://github.com/antlr/antlr4.git克隆项目:git clone https://github.com/antlr/antlr4.git
  2. opened the project with: code antlr4打开项目:代码antlr4
  3. opened a file in the project: Antlr4MojoTest.java在项目中打开一个文件: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该文件在项目的以下路径下找到: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" VSCode 告诉我:声明的 package“org.antlr.mojo.antlr4”与预期的 package“java.org.antlr.mojo.antlr4”不匹配

Question问题

Why does vscode think the expected package is java.org.antlr.mojo.antlr4 ?为什么 vscode 认为预期的 package 是java.org.antlr.mojo.antlr4

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 . VSCode 期望 class 位于 package java.org.antlr.mojo中,因为插件 pom 设置为包含src/test ,正如@Sweeper所指出的。 Here's the line from the module pom:这是模块 pom 中的行:

 <testSourceDirectory>src/test</testSourceDirectory>

VSCode correctly understands everything under this to be a package for the included classes. VSCode 正确地将其下的所有内容理解为包含类的 package。 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.补救措施是更改目录结构以匹配 pom(例如,将java目录的内容向上移动一级)或更改 pom 以匹配目录结构(例如,将“/java”添加到 testSourceDirectory 标记的末尾)。

IMO changing the pom is the better solution. IMO 更改 pom 是更好的解决方案。 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).特别是 testSourceDirectory 标签应该被整个删除,因为默认的 testSourceDirectory 已经是'src/test/java'作为 super-pom 版本 3.8.6 (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).不需要配置不需要配置的东西,因为默认会做(在这种情况下是正确的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM