springboot打成war并引入本地/第三方jar包

1.新建lib目录 如图:
这里写图片描述
2.pom文件加入本地依赖(坐标随便写,注意scope)

<dependency>
	<groupId>zmxy</groupId>
	<artifactId>sdk-java-source</artifactId>
	<version>20180208162128</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/src/main/resources/lib/zmxy-sdk-java-20180208162128-source.jar</systemPath>
</dependency>

3.如果是打成war


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;

@EnableCaching
@EnableScheduling
@EnableAspectJAutoProxy
@EnableTransactionManagement
@ServletComponentScan //扫描自定义filter和servlet、listener之类的
@MapperScan(basePackages = "ccm.mapper")
@SpringBootApplication
public class DatebookApplication extends SpringBootServletInitializer implements CommandLineRunner {

	public static void main(String[] args) {
		SpringApplication.run(DatebookApplication.class, args);
	}

	/**
	 * 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式
	 */
	 @Override
	 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
	 this.setRegisterErrorPageFilter(false);
	 return application.sources(DatebookApplication.class);
	 }

	// springboot运行后此方法首先被调用
	// 实现CommandLineRunner抽象类中的run方法
	@Override
	public void run(String... args) throws Exception {
		System.out.println("Report time for you: " + new Date().toString() + " ---> darling~ yours springboot2.0 project has been successfully started!nice day,right?");
	}
}

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!--本机运行把scope注释掉,打包时scope要带着-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provided</scope>
</dependency>

4.最后就是build

<build>
	<finalName>wongh</finalName>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>${java.version}</source>
				<target>${java.version}</target>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<configuration>
				<webResources>
					<resource>
						<directory>src/main/resources/lib</directory>
						<targetPath>WEB-INF/lib/</targetPath>
						<includes>
							<include>**/*.jar</include>
						</includes>
					</resource>
				</webResources>
			</configuration>
		</plugin>
	</plugins>
</build>

5.运行
这里写图片描述
这里写图片描述

  1. 如果是打成jar包 比较简单
<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.jar</include>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <mainClass>fangrong.com.cn.Startup</mainClass>
                    <fork>true</fork>
                    <addResources>true</addResources> 
                    <includeSystemScope>true</includeSystemScope>    主要就是这一行
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>



在Maven项目中,我们可以通过以下几种方式将第三方本地JAR文件引入war包中: 1. 通过 system scope 引入:在项目的pom.xml文件中,添加如下配置: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/libs/example.jar</systemPath> </dependency> ``` 这种方式直接指定了本地JAR文件的路径,并将其作为系统路径引入。 2. 通过 systemPath 引入:在系统的pom.xml文件中,添加如下配置: ```xml <dependencies> ... <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>/path/to/example.jar</systemPath> </dependency> ... </dependencies> ``` 与第一种方式类似,也是通过指定本地JAR文件的路径进行引入。 3. 通过 install 到本地仓库引入:首先需要使用以下命令将本地JAR文件安装到本地Maven仓库中: ``` mvn install:install-file -Dfile=/path/to/example.jar -DgroupId=com.example -DartifactId=example -Dversion=1.0 -Dpackaging=jar ``` 在项目的pom.xml文件中,添加如下配置: ```xml <dependencies> ... <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0</version> </dependency> ... </dependencies> ``` 这种方式会将本地JAR文件安装到Maven本地仓库中,然后从本地仓库中引入。 无论使用哪种方式,都需要在pom.xml文件中添加相应的依赖配置,以确保Maven能够正确地将第三方本地JAR文件打包进入war包中。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值