Maven 构建失败 "package com.fasterxml.jackson does not exist"

Maven build fails with "package com.fasterxml.jackson does not exist"

我需要在我的 maven 项目中解析一个 json 文件。 为此,我开始在我的 java 文件中进行简单导入 (App.java)

package com.mycompany.app;
import com.fasterxml.jackson.*;// <-------- HERE
public class App
{
    public static void main( String[] args )
    {
    }
}

然后,我尝试使用mvn package编译项目,但出现错误:

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ my-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /tmp/my-app/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /tmp/my-app/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /tmp/my-app/src/main/java/com/mycompany/app/App.java:[2,1] package com.fasterxml.jackson does not exist
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.628 s
[INFO] Finished at: 2022-05-10T15:31:39+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project my-app: Compilation failure
[ERROR] /tmp/my-app/src/main/java/com/mycompany/app/App.java:[2,1] package com.fasterxml.jackson does not exist
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

以下是我构建 maven 项目的方式:

  1. 我创建了我的 maven 项目:
mvn -B archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
  1. 我修改了文件 App.java(由前面的命令创建)以匹配我的文件。
  2. 我修改了pom.xml以添加依赖:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.13.2</version>
</dependency>

你知道如何解决这个错误吗?

我最近 a similar question: the problem is that com.fasterxml.jackson is not a package, there is a folder com/fasterxml/jackson (see the source),但不是包裹。在Java中,package是一个命名空间,至少包含一个class。 Asterix import 导入 non-recursively 包中的所有 classes。所以,比方说,如果你需要使用包 com.fasterxml.jackson.core 中的 classes JsonParser.javaJacksonException.java,你可以像这样添加一个星号导入:com.fasterxml.jackson.core.*