1. 程式人生 > >spring boot引入本地jar

spring boot引入本地jar

問題描述:spring boot引入到本地jar到專案(阿里雲簡訊),使用maven package打包,出現以下異常

2017-07-29 00:07:43,822 ERROR SpringApplication:827 - Application startup failed
java.lang.NoClassDefFoundError: com/aliyuncs/profile/IClientProfile
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods
(Unknown Source) at java.lang.Class.getDeclaredMethods(Unknown Source) at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521) at org.springframework.util
.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)

NoClassDefFoundError,解壓打包好的jar發現lib裡面沒有匯入本地的兩個jar,故懷疑是maven打包出現問題。搜尋資料發現,將pom新增以下外掛可解決。

<build>
        <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> <extdirs>${project.basedir}/lib</extdirs> </compilerArguments> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <targetPath>lib/</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources> </build>

targetPath:jar輸出的目錄(相對於專案根目錄)
directory:為需要匯入的jar的路徑