我们在发布服务的时候,有的时候会出现配置文件在jar包或者war包内,每次修改都需要重新打包。这样就会浪费很多时间,特别是线上的问题。需要即刻生效。这个时候就需要我们在打包的时候处理好。
第一步,在pom.xml修改。
在主pom.xml的build处添加如下代码
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 并把文件复制到target目录下-->
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 指定不需要处理的资源 <excludes> <exclude>WEB-INF/*.*</exclude> </excludes> -->
<!--<excludes>-->
<!--<exclude>**/*.xml</exclude>-->
<!--</excludes>-->
<includes>
<include>application.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
这样,当我们打好包的时候,就会在jar包附近看到application.properties文件,这个文件存储的是我们需要配置的参数
第二步:发布
通常情况下,打包只需要用:
jar -jar xx.jar
这样就可以了。
但是如果用第一步的情况下,我们需要使用下面启动包
java –jar target/xxxx.jar –spring.config.location=target/conf/相应环境的配置文件.yml (# xxxx.jar 是编译生成的可执行jar包,这个命令是指定环境配置并运行项目)