Maven引入本地Jar包并打包进War包

一、背景

在开发钉钉第三方企业应用登录时,需要引用钉钉提供的JAR包,但仓库没有提供,只能采用本地文件的方式引入


image.png

POM文件:

//该工程以WAR包形式部署
    <packaging>war</packaging>


        <dependency>
            <groupId>com.taobao.top</groupId>
            <artifactId>top-api-sdk-dev</artifactId>
            <version>dingtalk-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}/lib/taobao-sdk-java-auto_1479188381469-20180525.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.taobao.top</groupId>
            <artifactId>lippi-oapi-encrpt</artifactId>
            <version>dingtalk-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}/lib/lippi-oapi-encrpt.jar</systemPath>
        </dependency>


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

采用官方demo中采用的spring-boot-maven-plugin在打包时将本地依赖jar包引入JAR包。
但本工程以WAR包形式打包,虽然依赖也能打出来,但是打在WEB-INF/lib-provided文件夹而不是WEB-INF/lib文件夹中,导致TOMCAT部署的时候找不到JAR包中的引用类。


image.png

解决

采用如下插件,编译阶段将scope为system的jar包打入war包中

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>compile</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
                <includeScope>system</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>
image.png