博主遇见过这种问题,在分布式项目中,我们常常会通过配置文件的更改达到部署项目,增加项目,实现负载均衡等一系列操作,我们常常用的是springboot打成的jar部署(方便快捷),要是配置文件在jar,外面不能更改,那么我们的操作将变得麻烦
目录
2. 在src/main/resources/下新建一个config文件,并新增一个package.xml内容如下:
注意
如果用的querydsl 必须注释 pom文件中的配置
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
1. 首先看看项目结构
目标是将自定义config文件下的properties文件和默认application.properties文件放在jar包外
下面是教程:
pom文件配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix></classpathPrefix>
<mainClass>com.server.ServerApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- The configuration of the plugin -->
<configuration>
<descriptors>
<descriptor>src/main/resources/config/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
上面配置中的2个plugin分别是:
- maven-jar-plugin:负责将应用程序打包成可执行的jar文件
- maven-assembly-plugin:负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署
2. 在src/main/resources/下新建一个config文件,并新增一个package.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<!--<include>*.sql</include>-->
<include>*.bat</include>
<include>*.md</include>
<!--<include>README*</include>-->
<!--<include>LICENSE*</include>-->
<!--<include>NOTICE*</include>-->
<!--<include>build.info</include>-->
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>config/*.xml</include>
<include>config/*.properties</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
start.bat配置 方便我们启动
可以看到,已经将application.properties和config下的properties文件放在jar包外了。但是这里需要注意的是:我们现在只是将配置文件打包在jar包外,实际jar包里面还有一份,jar包运行的时候实质还是执行的jar包里面的配置文件,现在修改外面的配置文件是没有用的。那我们为什么还要打包出来呢?实际上我们可以通过在pom.xml文件添加配置将jar包里面的配置删掉。但是,这样做如果程序本身在开发的时没有将配置路径写成外部路径的话会报错导致程序无法运行的。所以我更推荐通过命令指定程序运行时执行外部配置文件,当不执行命令参数时,程序会执行jar包里面的配置正常运行。执行命令参数如下:
java -jar -Dspring.config.location=%cd%\application.properties %cd%\server-0.0.1-SNAPSHOT.jar
- %d%:表示当前application.properties所在的目录路径(需要自行修改成自己的路径)
- xxx.jar:表示自己的jar包名字
- -Dspring.config.location:指定spring运行时执行的config路径,这里指我们的application.properties文件所在路径
- -Dlogging.path:指定日志文件存放路径(根据自己需求填写)
3. 打包后结果
解压后,点击start.bat就可以运行了
注意下面这个配置必须有,这个是启动springboot项目必须的
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
编写启动脚本
微服务部署 windows 多服务的区别_多点服务和微服务区别-CSDN博客
目的:方便博主回忆!