为什么我在maven中添加了一个依赖项而导致模块丢失错误,我需要解决它?

时间:2019-02-20 15:03:07

标签: java maven intellij-idea gson

Error:(10, 18) java: package com.google.gson is not visible (package com.google.gson is declared in module gson, but module AnimalShelter fails to read it)

在向我的Maven添加 gson 依赖项后出现此错误,我完全不知道如何解决它,我在Google上四处搜寻,建议我放置一个必需的在我的 module-info.java 中。做到了,它修复了IDE错误,但是当我尝试运行时

java.lang.module.FindException: Module gson not found, required by AnimalShelter

老实说,我根本不了解模块的工作方式,因此我可能会忽略一些简单的事情,但是在我的maven pom.xml中添加依赖项后,它不应该工作吗?

如下所示。

pom.xml :(已导入)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>AnimalShelterYori</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/../mods</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>
<dependencies>



        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>


    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
        </dependency>


        <!-- ... -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
        <!-- ... -->
    </dependencies>
</project>

2 个答案:

答案 0 :(得分:1)

您可以尝试以下选项:

  1. 提供了Gson依赖项的范围,因此请确保在运行时由任何容器提供
  2. 更新maven项目并执行全新安装
  3. 尝试使用最新版本的Gson

    <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> <scope>provided</scope> </dependency>

答案 1 :(得分:0)

使用Maven的provided范围意味着不需要将依赖关系与您的应用程序打包在一起(即,您正在部署到将提供依赖关系的Tomcat之类的应用服务器上)。尝试一起删除范围,或更改为默认的 compile