You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法在Maven项目中使用org.ehcache的问题求助

问题:Ehcache依赖无法识别,报错“Cannot resolve symbol 'ehcache'”

在项目中尝试使用Ehcache,始终无法成功,编译时报错Cannot resolve symbol 'ehcache'。开发环境为IntelliJ IDEA,已安装Maven 3.9.4,执行过mvn clean install和mvn dependency:resolve命令,耗时6小时仍未解决。

相关文件内容

Main.java

package org.example;

import org.ehcache.*;
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

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>org.example</groupId>
    <artifactId>demo3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>3.1.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

mvn clean install执行输出

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< org.example:demo3 >--------------------------
[INFO] Building demo3 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ demo3 ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo3 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo3 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/asad-raheem/Documents/workspace/demo3/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/asad-raheem/Documents/workspace/demo3/src/main/java/org/example/Main.java:[3,1] package org.ehcache does not exist
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.634 s
[INFO] Finished at: 2023-09-22T02:23:00+05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project demo3: Compilation failure
[ERROR] /Users/asad-raheem/Documents/workspace/demo3/src/main/java/org/example/Main.java:[3,1] package org.ehcache 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

Process finished with exit code 1

解决方案

问题出在pom.xml的配置上:你把Ehcache的依赖放在了<dependencyManagement>节点下,这个节点仅用于统一管理依赖版本,不会将依赖实际引入到项目中。需要把依赖移到独立的<dependencies>节点下。

修改后的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>org.example</groupId>
    <artifactId>demo3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!-- 可选:保留dependencyManagement用于统一版本管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>3.1.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 新增dependencies节点,实际引入Ehcache依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        </dependency>
    </dependencies>
</project>

修改完成后执行以下操作:

  1. 在IntelliJ IDEA中点击右侧Maven面板的刷新按钮(🔄),重新导入依赖
  2. 再次执行mvn clean install命令

若仍有问题,可尝试:

  • 清理IDEA缓存:File -> Invalidate Caches... -> Invalidate and Restart
  • 检查本地Maven仓库(默认路径~/.m2/repository),若不存在ehcache相关jar包,手动删除org/ehcache目录后重新拉取依赖

内容的提问来源于stack exchange,提问作者tehmas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 08:10:32