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

Maven Shade打包含动态加载类的Jar后AWS Lambda找不到Geotools Length函数

Maven Shade打包GeoTools工具包部署AWS Lambda出现动态加载类缺失问题

问题背景

我使用maven-shade-plugin对Jar包进行shade和精简处理后上传至AWS Lambda,运行时抛出类缺失异常,根据现有信息判断大概率是动态类加载机制导致,目前未找到对应解决方案,也不清楚除已尝试操作外还可以如何排查处理。我已了解Jar Shade的基本原理,但未找到该特定问题的相关文档。

构建日志如下:

[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /home/ro007848/Workspace/geometry-service/target/geometry-service-1.0.0.jar with /home/ro007848/Workspace/geometry-service/target/geometry-service-1.0.0-shaded.jar

异常堆栈(Cloudwatch采集)

Caused by: java.lang.RuntimeException: Unable to find function Length
at org.geotools.filter.FunctionFinder.findFunction(FunctionFinder.java:208)
at org.geotools.filter.FunctionFinder.findFunction(FunctionFinder.java:152)
at org.geotools.filter.FunctionFinder.findFunction(FunctionFinder.java:129)
at org.geotools.filter.FilterFactoryImpl.function(FilterFactoryImpl.java:819)
at org.geotools.feature.FeatureTypes.createLengthRestriction(FeatureTypes.java:148)

现有pom配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <minimizeJar>true</minimizeJar>
                <filters>
                    <filter>....</filter>
                    <filter>
                        <artifact>org.geotools:*</artifact>
                        <includes>
                            <include>**</include>
                        </includes>
                    </filter>
..........
                    <filter>....</filter>
                    <filter>
                        <artifact>com.amazonaws:*</artifact>
                        <includes>
                            <include>**</include>
                        </includes>
                    </filter>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

包内容验证结果

我解压生成的Jar包后在org/geotools目录下执行grep -r "Length",结果如下:

................
Binary file ows/wms/CRSEnvelope.class matches
Binary file filter/FilterSAXParser.class matches
Binary file filter/FilterDOMParser.class matches
Binary file filter/LengthFunction.class matches
Binary file filter/function/StaticGeometry.class matches
Binary file filter/function/FilterFunction_strLength.class matches
Binary file filter/function/FilterFunction_strToLowerCase.class matches
Binary file filter/function/FilterFunction_strToUpperCase.class matches
Binary file filter/function/FilterFunction_geomLength.class matches
Binary file filter/ExpressionDOMParser.class matches
Binary file referencing/util/CRSUtilities.class matches
Binary file referencing/crs/DefaultProjectedCRS.class matches
................

Length相关的类已经被打进包中,不清楚异常提示的缺失内容具体指向什么,也不知道如何进一步调试该问题,同时希望了解是否有通用的maven-shade-plugin include配置可以解决这类动态类加载导致的类缺失问题。

已尝试的无效方案

有人建议在shade配置中显式引入指定类,配置如下,但执行后无效:

<filter>
    <artifact>org.geotools:gt-main</artifact>
    <includes>
        <include>org/geotools/filter/LengthFunction.class</include>
    </includes>
</filter>

补充信息

本地运行集成测试完全正常,所有所需函数都可以正常加载,只有部署到AWS Lambda后运行相同测试才会触发失败。由于涉及动态类加载逻辑,无法通过常规debug手段定位到需要引入的类。

编辑补充:
经mvn help:effective-pom -Dverbose命令验证,使用的GeoTools版本为25.1,不存在版本冲突问题:

<dependency>
    <groupId>org.geotools</groupId>  <!-- com.digitalglobe.p2020.common:common-dependencies:3.47.139813, line 922 -->
    <artifactId>gt-coverage</artifactId>  <!-- com.digitalglobe.p2020.common:common-dependencies:3.47.139813, line 923 -->
    <version>25.1</version>  <!-- com.digitalglobe.p2020.common:common-dependencies:3.47.139813, line 924 -->
</dependency>
<dg-geotools.version>25.1</dg-geotools.version>  <!-- com.digitalglobe.p2020.common:common-dependencies:3.47.139813, line 218 -->

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 04:24:01