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

Liferay 7.4模块添加外部依赖报错:导入包未解决

解决Liferay 7.4模块Ehcache/BigMemory依赖的未解析导入问题

你的模块出现Unresolved requirement错误,是因为OSGi容器找不到com.terracotta.entity.ehcache和指定版本的net.sf.ehcache包。以下是具体解决步骤:

1. 正确配置Gradle依赖

在模块的build.gradle中,先声明Ehcache和Terracotta相关依赖,注意版本匹配:

dependencies {
    // 引入指定版本的Ehcache
    implementation 'net.sf.ehcache:ehcache:2.10.0'
    // 引入与Ehcache 2.10兼容的Terracotta Entity依赖,版本按需调整
    implementation 'com.tc:terracotta-entity-ehcache:3.8.0'
}

2. 处理OSGi导入/导出规则

根据需求选择两种方式之一:

方式一:嵌入依赖到模块

如果Liferay系统未提供这些依赖包,将它们嵌入模块内部,避免OSGi尝试外部导入:

// 打包时将依赖嵌入模块jar
jar {
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    // 排除签名文件,避免OSGi加载冲突
    exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
}

// 配置OSGi元数据,排除嵌入的包,取消外部导入请求
osgi {
    importPackage = [
        '!com.terracotta.entity.ehcache',
        '!net.sf.ehcache',
        '*' // 保留模块其他必要的导入包
    ]
    exportPackage = [
        'com.your.module.package' // 替换为你的模块需要导出的包路径
    ]
}

方式二:依赖系统中的bundle

如果Liferay已经部署了兼容版本的Ehcache和Terracotta bundle:

  1. 通过Gogo Shell执行lb | grep ehcache,确认系统中已安装的bundle版本。
  2. 调整build.gradle的osgi配置,确保导入包的版本与系统匹配:
osgi {
    importPackage = [
        'com.terracotta.entity.ehcache',
        'net.sf.ehcache;version="[2.10.0,2.11.0)"',
        '*'
    ]
}

若系统中无对应bundle,需先通过Liferay模块管理界面或Gogo Shell安装缺失的bundle。

3. 配置BigMemory缓存

在模块的src/main/resources下创建ehcache.xml,配置BigMemory分布式存储:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <cacheManager name="bigMemoryCacheManager">
        <!-- 配置Terracotta服务器地址 -->
        <terracottaConfig url="your-terracotta-server:9510" />
        
        <cache name="businessDataCache"
               maxEntriesLocalHeap="1000"
               eternal="false"
               timeToIdleSeconds="3600"
               timeToLiveSeconds="7200">
            <!-- 指定使用BigMemory分布式持久化策略 -->
            <persistence strategy="distributed" />
        </cache>
    </cacheManager>
</ehcache>

4. 验证部署

执行gradle clean build清理构建缓存,检查生成的模块jar中META-INF/MANIFEST.MF的Import-Package项,确认未再出现未解析的包声明。之后部署模块到Liferay,观察是否还有启动错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 20:40:29