当前位置: 首页 > article >正文

Maven分离资源文件

Spring Boot 项目默认的会将所有资源文件、依赖文件、配置文件等打包成单一的 jar 文件,但是有时候我们并不想让配置文件、依赖包都跟可执行文件打包到一起。

这时候可以在 pom.xml 文件中进行配置,从而使资源文件、依赖包和可执行文件分离。

本文主要是分离配置文件,pom.xml配置如下:

<build>
    <!-- 资源配置 -->
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <!--排除配置文件-->
            <excludes>
                <!--使用通配符,当然可以定义多个exclude标签进行排除-->
                <exclude>**/application*.yml</exclude>
            </excludes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot-dependencies.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- 资源文件处理插件 -->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <!-- 复制配置文件 -->
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <!-- 复制配置文件到指定目录 -->
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>**/application*.yml</include>
                                </includes>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/config</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打包时使用 resources 的 exclude 排除指定的资源文件,使用 maven-resources-plugin 将配置文件输出到外部目录。

启动 jar 时使用以下命令即可启动:

java -Dloader.path=config/ -jar xxxx.jar

如果是有依赖文件可以逗号分隔:

java -Dloader.path=lib/,config/ -jar xxxx.jar


http://www.kler.cn/a/131955.html

相关文章:

  • Django——模板层、模型层
  • Qt控件按钮大全
  • <Linux>(极简关键、省时省力)《Linux操作系统原理分析之Linux 进程管理 4》(8)
  • Beego之Bee工具使用
  • npm使用国内淘宝镜像的方法
  • 腾讯云重新注册算不算新用户?算!
  • ffmpeg知识点整理
  • ER 图是什么
  • MySQL优化(1):B+树与索引
  • Python武器库开发-flask篇之Get与Post(二十五)
  • 4-flask-cbv源码、Jinja2模板、请求响应、flask中的session、flask项目参考
  • Flink和Kafka连接时的精确一次保证
  • 竞赛 题目:基于机器视觉opencv的手势检测 手势识别 算法 - 深度学习 卷积神经网络 opencv python
  • 如何打包成一个可安装的Android应用程序包
  • 理解 R-CNN:目标检测的一场革命
  • 优化收益与用户体验:游戏APP需接入的广告类型
  • CAPL如何对以太网报文的长度字段和校验和字段设置错误值
  • 【数据结构】快速排序算法你会写几种?
  • OpenCV快速入门:像素操作和图像变换
  • The import xxx.xxx.xxxx is never used