vendredi 12 janvier 2024

How to resolve JAR conflicts with maven-shade-plugin

I have two projects: jartest and jarprj. jartest depends on poi 3.17, whereas jarprj depends on poi 5.10. jartest encounters an error when dynamically loading jarprj. The error message is as follows:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.example.App.main(App.java:27)
Caused by: java.lang.NoSuchMethodError: org.apache.poi.xwpf.usermodel.XWPFDocument.getDocComments()Lorg/apache/poi/xwpf/usermodel/XWPFComments;
    at org.example.JarClass.test(JarClass.java:10)
    ... 5 more

The issue may stem from a conflict between the versions of poi. I attempted to rename poi using the maven-shade-plugin, but the error persists. The error message is as follows:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.example.App.main(App.java:27)
Caused by: java.lang.NoSuchFieldError: Factory
    at shade.org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentCreate(XWPFDocument.java:304)
    at shade.org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:149)
    at org.example.JarClass.test(JarClass.java:9)
    ... 5 more

jartest cannot upgrade its poi version, while jarprj requires poi 5.1.0. What methods are there to solve this? How can we resolve it using maven-shade-plugin?





Aucun commentaire:

Enregistrer un commentaire