项目中集成自己写的jar包时发现了一些问题:

1、集成本地jar包本身比较简单,但是考虑到团队合作的问题,如果我只在maven本地私服导入jar包的话,其他团队成员都要做相关操作,成本略高。

2、用依赖本地jar包+maven-install-plugin的方式集成,项目本地运行、打包都没有问题,但是部署在服务器上,访问到相关方法时,会有org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class的报错。

当前的pom如下:

        <dependency>
            <groupId>*.*</groupId>
            <artifactId>*</artifactId>
            <version>*</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/taskqueue-1.0-SNAPSHOT.jar</systemPath>
        </dependency>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
                <executions>
                    <execution>
                        <id>install-external</id>
                        <phase>clean</phase>
                        <configuration>
                            <file>${basedir}/src/main/resources/lib/taskqueue-1.0-SNAPSHOT.jar</file>
                            <repositoryLayout>default</repositoryLayout>
                            <groupId>*.*</groupId>
                            <artifactId>*</artifactId>
                            <version>*</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

报错信息:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.*.taskqueue.utils.RedisUt
ils    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1053)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)

 可以看到报错是NoClassDefFoundError,大概可以判断可能是由于两种问题导致

1、没有引入相应的jar包
2、两个jar包中都有这个class,无法确认是引用的哪一个,就是jar冲突

打开编译完成的jar包

 

 发现确实有两个,手动删除一个再发布运行,错误没变,然而又确实存在jar包,包的大小也一样,心态有点炸。

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

回来了

小问题处理了一天,时间都花在maven打包上了,后来才发现是因为集成的jar包里面的配置文件没有读到,错误又被catch了所以启动没有停止,日志太多没注意看,中间的报错信息如下:

2020-03-19 15:54:00.405 * [async-service-1] ERROR o.s.a.i.SimpleAsyncUncaughtExceptionHandler - Unexpected exception occurred invoking async method: public void com.*.framework.task.AsyncServiceImpl.executeAsync()
java.lang.ExceptionInInitializerError: null
    at com.*.taskqueue.plan.RedisDelayPlan.<init>(RedisDelayPlan.java:19)
    at com.*.framework.task.AsyncServiceImpl.executeAsync(AsyncServiceImpl.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: null
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at com.*.taskqueue.utils.RedisUtils.<clinit>(RedisUtils.java:27)
    ... 14 common frames omitted

处理方向转移为配置的读取问题,当前的情况是配置文件在jar包里都有,本地执行正常,部署的jar包执行不正常。

为了节省宝贵的时间,将配置读取方式改为调用时传入,再初始化连接,使用双检锁方式确保单例,参考

 

 https://blog.csdn.net/absolute_chen/article/details/93380566

问题解决!!!

 

这是一篇很无聊的博客!!!