spring boot项目将配置文件打包在外部

Posted 阳光11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot项目将配置文件打包在外部相关的知识,希望对你有一定的参考价值。

一、打成jar包不应该包含配置文件

 

二、添加打成压缩包的插件,配置插件中添加项目的配置文件

 

 

参考文件

1.pom.xml

 1 <build>
 2         <!-- 产生的构件的文件名,默认值是${artifactId}-${version}。 -->
 3         <finalName>${artifactId}</finalName>
 4         <plugins>
 5             <plugin>
 6                 <groupId>org.springframework.boot</groupId>
 7                 <artifactId>spring-boot-maven-plugin</artifactId>
 8                 <executions>
 9                     <execution>
10                         <goals>
11                             <goal>repackage</goal>
12                         </goals>
13                     </execution>
14                 </executions>
15                 <configuration>
16                     <includeSystemScope>true</includeSystemScope>
17                 </configuration>
18             </plugin>
19             <plugin>
20                 <groupId>org.apache.maven.plugins</groupId>
21                 <artifactId>maven-assembly-plugin</artifactId>
22                 <configuration>
23                     <descriptors>
24                         <descriptor>src/main/assembly/package.xml</descriptor>
25                     </descriptors>
26                 </configuration>
27                 <executions>
28                     <execution>
29                         <id>make-assembly</id>
30                         <phase>package</phase>
31                         <goals>
32                             <goal>single</goal>
33                         </goals>
34                     </execution>
35                 </executions>
36             </plugin>
37         </plugins>
38         <resources>
39             <resource>
40                 <directory>src/main/java</directory>
41                 <includes>
42                     <include>**/*.xml</include>
43                 </includes>
44                 <filtering>true</filtering>
45             </resource>
46             <resource>
47                 <directory>src/main/resources</directory>
48                 <excludes>
49                     <exclude>**/*.properties</exclude>
50                     <exclude>**/*.yml</exclude>
51                 </excludes>
52                 <filtering>false</filtering>
53             </resource>
54             <resource>
55                 <directory>lib/</directory>
56                 <targetPath>lib</targetPath>
57                 <includes>
58                     <include>**/*.jar</include>
59                 </includes>
60             </resource>
61         </resources>
62     </build>
View Code

 

2.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>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>*.properties</include>
            </includes>
        </fileSet>

        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>
View Code

 

以上是关于spring boot项目将配置文件打包在外部的主要内容,如果未能解决你的问题,请参考以下文章

尝试在外部在tomcat 9上运行带有rest控制器的spring boot war文件

spring boot 打包

无法在外部 tomcat 上部署 Spring Boot 应用程序

Spring Boot 应用程序在本地主机上运行,​​但在外部 Tomcat 上返回 404

在外部 tomcat 中定义 Spring Boot 应用程序的上下文路径

Spring Boot 外置配置文件