Jar着色与动态加载类与Maven



在将jar上传到AWS lambda之前,我使用maven插件来遮蔽和最小化jar。然而,我得到一个运行时异常,因为缺少一个类。据我所知,这是由于一些"动态"类加载或其他东西,但我不确定是否A)有一个解决方案或B)它可能是什么,超出我已经做出的努力。我读了一些关于罐子阴影的好[文章][1],我想我理解了一般的想法,但我找不到任何地方记录的我的特殊问题的例子。

[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,解压缩并查看里面,

➜  geotools git:(dev-S62039_geojson-to-shapefile_ro007848) ✗ pwd                                                                                                                                                                              
/home/ro007848/Workspace/geometry-service/target/jar-shaded-simple/jar-contents/org/geotools                                                                                                                                                  
➜  geotools git:(dev-S62039_geojson-to-shapefile_ro007848) ✗ 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"函数…如果我的例外不是讨论这个,它讨论的是哪个?我如何调试这个问题?在jar-shading插件配置中设置<include>过滤器是否有"焦土"方法,以便使其工作?

有人建议我尝试在阴影配置中显式添加这些类,如下所示:

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

但这要么不起作用,要么我没有做对。

更多信息:

当我在本地运行此代码的集成测试时,执行没有问题。需要的功能都找到了。然而,当我对部署在AWS中的资源(由这个jar支持的这个lambda)执行相同的集成测试时,当我得到失败时,就是。我发现很难在调试器中发现我到底需要包含什么,因为代码似乎是"动态地"做事情,我很难密切关注它。

我已经无计可施了!

编辑:我看了一下@gerold- browser发布的电子邮件线程,但我不确定它是否适用,因为根据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>
``` and 
```
<dg-geotools.version>25.1</dg-geotools.version>  <!-- com.digitalglobe.p2020.common:common-dependencies:3.47.139813, line 218 -->
```

[1]: https://medium.com/@akhaku/java-class-shadowing-and-shading-9439b0eacb13

正如GeoTools FAQ中所讨论的那样,GeoTools依赖于应用程序使用的每个GeoTools模块的META-INF/services文件。除非您特别指示Shade插件合并这些文件,否则您将以maven看到的第一个或最后一个文件结束(我忘记了哪个)。

所以把这个添加到你的诗歌的build部分:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- This bit merges the various GeoTools META-INF/services files         -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

如果你想检查它是否工作,那么你需要检查META-INF/services/org.opengis.filter.expression.Function包含org.geotools.filter.LengthFunction

相关内容

  • 没有找到相关文章

最新更新