Code-Review-Maven编译(第三方jar包引用)

Code-Review-SpringBoot-Maven编译(第三方jar包引用)

 

  在使用maven编译项目时,有时候咱们可能会使用一些第三方的jar包依赖库,比如第三方支付类的接入,大多出于安全考虑,会单独给提供jar包,可是这些jar包依赖库又没有在共有的maven仓库。 一般只能下来放到本项目的lib目录下。如果在打包的时候不进行拷贝处理,会导致打包后的target.jar中不会有lib文件夹中的相关第三方jar包。 打包后没法运行起来,所以需要对第三方jar进行单独处理,让maven打包时可以把使用到外部jar打进去。主要就是在build中加resourcesapache。

 

  备注:pom.xml的配置很重要。

         观察完整的pom.xml配置文件,重点关注resources 节点包含了俩个  resource 。一个指定的第三方jar包的位置。一个指定了配置文件的位置。resource的配置至关重要。如果你没有配置

<resource>
    <directory>src/main/resources</directory>
    <targetPath>BOOT-INF/classes/</targetPath>
</resource>

的情况下:你很大概率会遇到:

2020-12-28 16:28:08.947  WARN 18018 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectingArgumentResolverBeanPostProcessor' defined in class path resource [org/springframework/data/web/config/ProjectingArgumentResolverRegistrar.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration'

bean装载的异常,主要原因就是没有找到相关的配置文件导致的。

 

编译后的依赖文件(maven依赖以及第三方jar)位置:/BOOT-INF/lib 。

自己项目编译后文件(其中应该包含配置文件):BOOT-INF/classes  



重点: 完整的pom.xml配置,以下是完整pom.xml build配置节。

<build>
        <defaultGoal>compile</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <!-- 打包本地jar包 -->
                        <extdirs>${project.basedir}/src/libs</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
            <directory>${project.basedir}/src/libs</directory>
            <targetPath>BOOT-INF/lib/</targetPath>
            <includes>
                <include>**/*.jar</include>
            </includes>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <targetPath>BOOT-INF/classes/</targetPath>
            </resource>

        </resources>

    </build>

 

 

以上是解决 用maven编译项目时,集成第三方jar包的解决方案。我在自己的项目中实际测试后,部署成功。如果疑问,请留言沟通。祝好。

 

posted @ 2020-12-28 17:27  jerry-Tom  阅读(195)  评论(0编辑  收藏  举报