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的路径


方法二:

加入<includeSystemScope>true</includeSystemScope>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
<compilerArguments>
<extdirs>lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>

posted @ 2018-03-04 14:09  車輪の唄  阅读(17)  评论(0编辑  收藏  举报  来源