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

本文介绍如何将无法通过Maven获取的外部JAR(如Oracle JDBC驱动)打包进WAR或JAR文件中,包括修改pom.xml配置、添加资源路径等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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>
				<includes>
					<include>*.yml</include>
				</includes>
			</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>
				<includes>
					<include>*.yml</include>
				</includes>
			</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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值