SpringBoot2.0.2将外部jar打包到war或jar

1问题描述

由于需要使用外部jar,无法从maven上下载引用,如Oracle或SQL Server的JDBC驱动包
在直接打包的时候无法添加到war或jar,则需要方法将外部jar打包到war或jar

2解决办法

*1)在根目录下创建lib文件夹,并将驱动jar放到里面引用使用
这里写图片描述
*2)在pom.xml中添加外部jar的引用

        <dependency>
            <groupId>ojdbc</groupId>
            <artifactId>ojdbc</artifactId>
            <version>7</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ojdbc7.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>sqljdbc</groupId>
            <artifactId>sqljdbc</artifactId>
            <version>42</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/sqljdbc42.jar</systemPath>
        </dependency> 

*3)在pom.xml上添加插件支持将外部jar打包到war

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <directory>lib</directory>
                        <targetPath>WEB-INF/lib/</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>

*4)在pom.xml上添加配置支持将外部jar打包到jar

        <resources>
            <resource>
                <directory>lib</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>

3完整pom.xml参考

注意:打包成war的时候注释jar的配置,打包成jar的时候注释war的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.manson</groupId>
    <artifactId>manson</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>

    <name>manson</name>
    <description>manson project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> 
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>ojdbc</groupId>
            <artifactId>ojdbc</artifactId>
            <version>7</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ojdbc7.jar</systemPath>
        </dependency><!-- 引用外部jar -->
        <dependency>
            <groupId>sqljdbc</groupId>
            <artifactId>sqljdbc</artifactId>
            <version>42</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/sqljdbc42.jar</systemPath>
        </dependency><!-- 引用外部jar -->   
    </dependencies>

    <build>
        <finalName>manson</finalName>
        <resources>
            <resource>
                <directory>lib</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><!-- 打包成jar使用 -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>lib</directory>
                            <targetPath>WEB-INF/lib/</targetPath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin><!-- 打包成war使用 -->

        </plugins>
    </build>
</project>

猜你喜欢

转载自blog.csdn.net/u010520912/article/details/80436207