SpringBoot打包 lib分离 config分离

本文详细介绍了一种使用Spring Boot进行一键式应用部署的方法。通过配置Maven插件,包括Compiler、resource、jar、surefire、spring-boot-maven-plugin等,实现自动编译、资源处理、依赖复制及最终的ZIP打包。同时,提供了start.sh脚本,用于启动已打包的应用,简化了部署流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最终打包后的目录结构

│   start.sh
├───config
│       application-dev.yml
│       application-prod.yml
│       application.yml
│       ehcache.xml
│       logback.xml
├───logs
├───lib
│       spring-context-support-5.1.2.RELEASE.jar
│       spring-core-5.1.2.RELEASE.jar
│       spring-oxm-5.1.2.RELEASE.jar
│       spring-retry-1.2.2.RELEASE.jar
│       spring-tx-5.1.2.RELEASE.jar
│       stax2-api-3.1.4.jar
│       woodstox-core-5.0.3.jar
│       zkclient-0.10.jar
│       zookeeper-3.4.11.jar
│ 		 .........
└───logs

pom.xml

<build>
        <plugins>
            <!-- Compiler 插件, 设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- resource插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.yml</exclude>
                        <exclude>static/**</exclude>
                        <exclude>templates/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>cn.wisec.meerkat</groupId>
                            <artifactId>meerkat-slice</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <excludeTransitive>false</excludeTransitive>
                            <stripVersion>false</stripVersion>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.3</version>
                <executions>  <!--执行器 mvn assembly:assembly -->
                    <execution>
                        <id>make-zip</id><!--名字任意 -->
                        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
                        <goals>
                            <goal>single</goal><!-- 只运行一次 -->
                        </goals>
                        <configuration>
                            <descriptors> <!--描述文件路径 -->
                                <descriptor>src/main/bin/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

src/main/bin/assembly.xml

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/config/</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>lib/*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>start.sh</include>
                <include>logs</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

src/main/bin/start.sh

#!/bin/sh
dir=`cd $(dirname $0); pwd -P`
jar=$(find ./lib -type f -name  meerkat-slice-*.jar)
echo $jar
nohup java -server -Duser.dir=$dir -XX:-UseGCOverheadLimit -Xms1024m -Xmx30720m -XX:PermSize=256M -XX:MaxPermSize=512M -Dloader.path=./lib,./config -jar $jar > /dev/null &

注意事项

  • 不同的项目需要在pom.xml/build/plugins中
 		<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>cn.wisec.meerkat</groupId> //需要换成你打包工程的groupId
                            <artifactId>meerkat-slice</artifactId>//需要换成你打包工程的artifactId
                        </include>
                    </includes>
                </configuration>
		 </plugin>
  • 在start.sh中将jar=$(find ./lib -type f -name meerkat-slice-*.jar)中的meerkat-slice改成你打包工程的artifactId
<think>好的,用户问的是关于Spring Boot打包路径隔离的设置。我需要先回忆一下相关的内容,然后组织一个清晰的回答。首先,用户可能是在开发过程中遇到了不同环境打包的需求,比如测试环境和生产环境需要不同的配置,导致打包时需要分离路径或者资源。 我应该先解释什么是打包路径隔离,可能涉及不同的配置文件、资源文件或者依赖管理。然后,分步骤介绍如何实现,比如使用Maven或Gradle的profile功能,或者Spring Boot的profile机制。需要提到如何在application.properties或application.yml中配置不同的profile,以及如何指定激活的profile。 另外,用户可能想知道如何通过构建工具来指定打包路径,比如在pom.xml中配置不同的资源目录,或者使用不同的构建配置来生不同环境的jar包。这时候可能需要举例子,比如用Maven的profiles来定义dev、test、prod环境,每个环境对应不同的资源路径。 还要注意是否需要分离依赖,比如有些依赖只在特定环境下需要,这时候可以通过Maven的scope或者profile来管理。最后,可能还要提到如何运行不同环境的包,比如使用java -jar命令时指定active profile。 需要检查是否有遗漏的步骤,或者是否有更简便的方法。同时,确保术语准确,比如正确使用Spring Boot的@Profile注解,配置文件的命名规则等。可能还要提醒用户注意路径的正确性,避免打包时资源未正确加载的问题。 最后,总结一下常见的应用场景,帮助用户理解何时需要使用路径隔离,比如多环境部署、配置分离、资源管理等方面。这样用户不仅能知道怎么做,还能明白为什么需要这样做。</think>Spring Boot 打包路径隔离通常用于 **多环境配置管理** 或 **资源文件分离**。以下是常见的实现方式及步骤: --- ### 一、多环境配置文件隔离 #### 1. 创建环境专属配置文件 在 `src/main/resources` 下创建不同环境的配置文件: ```bash application-dev.yml # 开发环境 application-test.yml # 测试环境 application-prod.yml # 生产环境 ``` #### 2. 激活指定环境 通过启动参数或构建工具指定激活的环境: ```bash # 命令行激活测试环境 java -jar app.jar --spring.profiles.active=test # 或在 application.yml 中设置默认环境 spring: profiles: active: dev ``` --- ### 二、构建时资源路径隔离(Maven为例) #### 1. 配置 Maven Profiles 在 `pom.xml` 中定义不同环境的资源路径: ```xml <profiles> <profile> <id>dev</id> <build> <resources> <resource> <directory>src/main/resources-dev</directory> </resource> </resources> </build> </profile> <profile> <id>prod</id> <build> <resources> <resource> <directory>src/main/resources-prod</directory> </resource> </resources> </build> </profile> </profiles> ``` #### 2. 打包时指定 Profile ```bash mvn package -P dev # 打包开发环境资源 ``` --- ### 三、输出路径隔离 #### 1. 自定义打包输出目录 在 `pom.xml` 中修改打包路径: ```xml <build> <outputDirectory> ${project.basedir}/target/${env}-build # 例如 dev-build, prod-build </outputDirectory> </build> ``` #### 2. 通过 Maven 属性动态赋值 定义属性并传递环境变量: ```xml <properties> <env>default</env> </properties> <build> <outputDirectory>${project.basedir}/target/${env}-build</outputDirectory> </build> ``` 打包命令: ```bash mvn package -Denv=test ``` --- ### 四、依赖隔离 使用 Maven Profiles 控制依赖: ```xml <profiles> <profile> <id>with-optional-dep</id> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>optional-lib</artifactId> <version>1.0.0</version> <scope>runtime</scope> </dependency> </dependencies> </profile> </profiles> ``` 通过 `-P with-optional-dep` 选择性引入依赖。 --- ### 五、实践建议 1. **优先级规则** Spring Boot 配置优先级:命令行参数 > 外部配置文件 > Jar 包内配置文件。 2. **目录结构示例** ```bash src/ ├── main/ │ ├── resources/ │ │ ├── application.yml # 公共配置 │ │ ├── application-dev.yml │ │ ├── application-prod.yml │ │ └── static/ # 公共静态资源 │ ├── resources-dev/ # 开发环境专属资源 │ └── resources-prod/ # 生产环境专属资源 ``` 3. **结合 Docker 使用** 通过挂载卷实现外部配置覆盖: ```bash docker run -v /host/config:/app/config -e SPRING_PROFILES_ACTIVE=prod my-spring-app ``` --- 通过以上方法,可实现 **配置分离**、**资源隔离** 和 **环境差异化构建**,适合需要严格区分开发/测试/生产环境的场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值