springboot项目中Maven打jar包部署服务器引用多环境yml配置文件的三种方式

本文介绍如何在Spring Boot项目中通过三种方式管理不同环境的端口配置,包括将yml打包到jar、外部配置文件和依赖资源文件。详细步骤涉及Maven插件配置及启动命令的调整。

先创建好四个yml文件:可以统一将端口号写在application.yml文件,也可以每个环境写上不一样的端口号,我这里是统一每个环境的端口号都一样的

本地启动jar包命令:java -jar xxx.jar --spring.profiles.active=你需要指定的yml名字

 服务器启动jar包命令:nohup java -server -jar xxx.jar --spring.profiles.active=你需要指定的yml名字 >>nohup 2>&1 &

 

主要配置:application.ym;

开发环境:application-dev.ym,

测试环境:application-test.ym,

生产环境:application-prof.ym,

1、第一种:直接将yml配置打包到jar包里面

  1.1:将上面的yml准备好之后,修改pow文件的打包依赖

  1.2:直接使用标题下的启动命令
 

	<!--打包命令-->
	<plugins>            
         <plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
<!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<compilerVersion>1.8</compilerVersion>
					<!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不同于source的情况 -->
					<source>1.8</source><!-- 源代码使用的JDK版本 -->
					<target>1.8</target><!-- 需要生成的目标class文件的编译版本 -->
					<encoding>utf-8</encoding><!-- 字符集编码 -->
					<skipTests>true</skipTests><!-- 跳过测试 -->
				</configuration>
			</plugin>
<!--部署linux服务器依赖-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.0.2</version>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.xxgame.admin.web.AdminApplication</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<addMavenDescriptor />
						<manifestEntries>
							<Implementation-Version>${maven.build.timestamp}</Implementation-Version>
							<Class-Path>resources/</Class-Path>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
		</plugins>

2、第二种编译jar文件指定外部配置文件

2.1:将上面的yml准备好之后,修改pow文件的打包依赖

2.2:在maven-jar-pluugin依赖里加忽略配置的命令

2.3:直接在jar包同级目录下创建一个config文件夹,在里面编写你需要的yml配置文件,因为config是默认最高级配置中心

2.4:直接使用标题下的启动命令

<build>

		<!--打包命令-->
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<compilerVersion>1.8</compilerVersion>
					<!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不同于source的情况 -->
					<source>1.8</source><!-- 源代码使用的JDK版本 -->
					<target>1.8</target><!-- 需要生成的目标class文件的编译版本 -->
					<encoding>utf-8</encoding><!-- 字符集编码 -->
					<skipTests>true</skipTests><!-- 跳过测试 -->
				</configuration>
			</plugin>

	

			<!--部署linux服务器依赖-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.0.2</version>
				<configuration>
					<!--打包忽略文件-->
					<excludes>
						<exclue>**/*.yml</exclue>
						<exclue>**/*.xml</exclue>
					</excludes>
					<archive>
						<manifest>
							<mainClass>com.xxgame.admin.web.AdminApplication</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<addMavenDescriptor />
						<manifestEntries>
							<Implementation-Version>${maven.build.timestamp}</Implementation-Version>
							<Class-Path>resources/</Class-Path>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>

			
		</plugins>
	</build>

 

3、第三种:使用依赖,直接将配置文件打包出来存在resources/

3.1:在pow文件添加依赖

3.2:启动命令在标题下面


原理:在打包依赖忽略掉yml配置,并且将resource文件夹的文件copy出来,所有jar包里面就没有配置了,并且外部生成resource配置文件,在编译jar包时候时候就会主动引用同目录下的外部文件,当然对文件名是有限制的,暂时我知道的只有:resource和config;

<build>

		<!--打包命令-->
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<compilerVersion>1.8</compilerVersion>
					<!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不同于source的情况 -->
					<source>1.8</source><!-- 源代码使用的JDK版本 -->
					<target>1.8</target><!-- 需要生成的目标class文件的编译版本 -->
					<encoding>utf-8</encoding><!-- 字符集编码 -->
					<skipTests>true</skipTests><!-- 跳过测试 -->
				</configuration>
			</plugin>

	

			<!--部署linux服务器依赖-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.0.2</version>
				<configuration>
					<!--打包忽略文件-->
					<excludes>
						<exclue>**/*.yml</exclue>
						<exclue>**/*.xml</exclue>
					</excludes>
					<archive>
						<manifest>
							<mainClass>com.xxgame.admin.web.AdminApplication</mainClass>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<addMavenDescriptor />
						<manifestEntries>
							<Implementation-Version>${maven.build.timestamp}</Implementation-Version>
							<Class-Path>resources/</Class-Path>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>

<!--将配置文件从jar独立出来,实现动态修改配置文件-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<encoding>UTF-8</encoding>
							<outputDirectory>${project.build.directory}/resources</outputDirectory>
							<resources>
								<resource>
									<directory>src/main/resources/</directory>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

 

 

<think>首先,用户的问题是:"我想解决在SpringBoot集成嵌入式TongWeb时,打JAR文件后在服务器上找不到license文件的问题"。具体来说,是SpringBoot集成嵌入式TongWeb后,打JAR文件,在服务器上运行时找不到license文件。从引用中提取关键信息:-引用[2]:提到替换pom.xml中的依赖,使用tongweb-spring-boot-starter,并排除tomcat依赖。-引用[3]:提到修改application.yml配置文件,将server.tomcat改为server.tongweb,并添加license配置:server.tongweb.license.type=file,server.tongweb.license.path=classpath:tongweb/license.dat。用户的问题是在打JAR文件后,license文件缺失。意思是当应用程序作为JAR文件运行时,它无法找到license.dat文件。在SpringBoot中,当打JAR文件时,资源文件(如classpath下的文件)会被嵌入到JAR中。问题可能是:1.License文件没有正确放置在项目中。2.配置路径不正确。3.运行时环境问题。解决方案步骤:1.**确保license文件在正确的位置**:根据引用[3],配置是`classpath:tongweb/license.dat`。这意味着license文件应该放在项目的`src/main/resources/tongweb/`目录下。这样,当项目构建时,它会被复制到classpath中。-用户需要检查项目中是否有这个目录和文件。2.**验证配置**:在application.yml或application.properties中,确保配置正确:```yamlserver:tongweb:uri-encoding:UTF-8license:type:filepath:classpath:tongweb/license.dat```-路径`classpath:tongweb/license.dat`应该指向资源目录下的文件。3.**打问题**:当使用Maven或Gradle打JAR时,资源文件应该被含。用户需要确保在构建过程中,`src/main/resources`下的文件被正确打。-检查pom.xml或build.gradle,确保没有排除资源文件。4.**运行时问题**:在服务器上运行JAR文件时,classpath资源应该可访问。如果license文件没有被打JAR,或者路径错误,会导致找不到文件。常见原因:-文件路径大小写不匹配(尤其在Linux服务器上)。-文件没有实际存在于资源目录中。-配置错误。解决方案:-**确认文件位置**:在项目中创建`src/main/resources/tongweb/`目录,并将license.dat文件放入其中。-**检查构建输出**:构建JAR文件后,解压JAR文件(使用`jarxfyour.jar`),检查BOOT-INF/classes/tongweb/license.dat是否存在。-**更新配置**:如果文件在classpath中,路径应为`classpath:tongweb/license.dat`。确保没有多余的前缀。-**使用绝对路径**:如果问题依然存在,可以尝试使用绝对文件路径,但这不是最佳实践,因为部署环境可能变化。例如:```yamlpath:/path/to/license.dat```但最好保持为classpath相对路径。-**日志检查**:运行应用程序时,查看日志输出。TongWeb启动时可能会打印license加载信息或错误。引用整合:-从引用[2]和[3],配置是关键。用户需要确保依赖和配置正确。回答结构:1.**解释问题原因**:简要说明为什么license文件在JAR中找不到。2.**提供解决方案**:-步骤1:放置license文件在正确位置。-步骤2:验证配置文件。-步骤3:检查构建和打。-步骤4:备选方案(如使用绝对路径)。3.**添加引用**:末尾添加引用标识。4.**生成相关问题**:按照系统指令,在最后添加相关问题。相关问题生成:1.如何验证SpringBootJAR文件中的资源文件是否正确含?2.在嵌入式服务器中处理license文件的其他最佳实践是什么?3.如何调试SpringBoot应用程序中的类路径资源加载问题?确保中文回答。</think>### SpringBoot集成嵌入式TongWeb打JAR后License文件缺失的解决方案 当SpringBoot集成嵌入式TongWeb并打JAR文件后,服务器上找不到license文件的问题通常是由于资源文件路径配置错误或构建打过程中资源未被正确含所致。以下是逐步解决方案,基于引用内容和最佳实践: #### 1. **确保license文件位置正确** - **创建资源目录**:在项目的`src/main/resources`下创建子目录`tongweb`,并将`license.dat`文件放入其中。路径应为: ``` src/main/resources/tongweb/license.dat ``` - **验证classpath**:在JAR中,该文件会被映射到`BOOT-INF/classes/tongweb/license.dat`。构建后可通过命令检查: ```bash jar tf your-app.jar | grep license.dat # 确认文件存在 ``` - **原因**:引用[3]指定路径为`classpath:tongweb/license.dat`,这意味着文件必须位于classpath的`tongweb`子目录下[^3]。 #### 2. **更新配置文件** - 在`application.yml`或`application.properties`中,严格按引用[3]配置: ```yaml server: tongweb: uri-encoding: UTF-8 license: type: file path: classpath:tongweb/license.dat # 确保路径一致 ``` - **关键点**: - 使用`classpath:`前缀指定资源路径。 - 避免绝对路径(如`/home/user/license.dat`),否则在JAR中无法访问。 #### 3. **检查Maven/Gradle构建配置** - **Maven示例**:在`pom.xml`中确保资源目录被含: ```xml <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/tongweb/*.dat</include> # 显式含license文件 </includes> </resource> </resources> </build> ``` - **Gradle示例**:在`build.gradle`中添加: ```gradle sourceSets { main { resources { srcDirs = ["src/main/resources"] include "tongweb/**" } } } ``` - **依赖管理**:引用[2]要求排除Tomcat并添加TongWeb依赖: ```xml <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> <dependency> <groupId>com.tongweb.springboot</groupId> <artifactId>tongweb-spring-boot-starter-2.x</artifactId> <version>7.0.E.6_P7</version> </dependency> ``` #### 4. **备选方案:使用外部路径** 如果仍无法解决,可将license文件放在JAR外部,并配置绝对路径: ```yaml server: tongweb: license: type: file path: file:/opt/app/tongweb/license.dat # 服务器上的绝对路径 ``` - **操作步骤**: 1. 在服务器创建目录(如`/opt/app/tongweb`)。 2. 将`license.dat`复制到该目录。 3. 确保应用程序有读取权限。 #### 5. **验证和调试** - **日志检查**:启动JAR时添加调试参数: ```bash java -jar your-app.jar --debug ``` 检查日志中是否有`TongWeb license loaded`或`FileNotFoundException`信息。 - **运行时解压验证**: ```bash unzip -l your-app.jar | grep license.dat # 确认文件在JAR内 ``` #### 常见错误原因 - **路径大小写敏感**:Linux服务器区分大小写,确保路径中的`tongweb`与目录名一致。 - **构建工具忽略资源**:Maven/Gradle未配置含`.dat`文件。 - **配置拼写错误**:如`server.tongweb`误写为`server.tomcat`(引用[3]强调需改为`tongweb`)[^3]。 ### 相关问题 1. 如何在Spring Boot中调试类路径资源加载失败的问题? 2. 嵌入式TongWeb在Spring Boot中有哪些性能优化方法? 3. 如何处理Spring Boot JAR文件中的外部配置文件依赖? [^1]: 引用[1] [^2]: 引用[2] [^3]: 引用[3]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值