spirng boot 简单应用部署方式

默认spring boot 项目整个配置文件都被到打成的jar中,针对不同的环境修改配置文件就变成了一件很困难的事情

Spring Boot 部署结构

assembly     根目录
├── bin     命令目录
│   ├── dump.sh     jdk常用命令
│   ├── server.sh   服务命令
│   ├── start.bat   启动程序(windows)
│   ├── start.sh    启动程序(linux)
│   └── stop.sh     停止程序(linux)
├── conf    配置文件目录
│   ├── application.yml         总配置文件
│   ├── application-dev.yml     开发环境配置文件
│   ├── application-test.yml    测试环境配置文件
│   ├── application-pro.yml     正式环境配置文件
│   └── logback-spring.xml      日志配置文件
├── lib     依赖jar
└── logs    日志目录
    ├── demo.log        日志文件
    └── stdout.log      命令行输出文件

Sprint Boot 项目结构

assembly
├── pom.xml
├── src
│   └──main
│       ├── assembly
│       │   ├── bin    启动相关目录
│       │   │   ├── dump.sh     jdk常用命令
│       │   │   ├── server.sh   服务命令
│       │   │   ├── start.bat   启动程序(windows)
│       │   │   ├── start.sh    启动程序(linux)
│       │   │   └── stop.sh     停止程序(linux)
│       │   └── release.xml     编译配置参数
│       └── resources   配置文件目录
│           ├── application-dev.yml     总配置文件
│           ├── application-pro.yml     开发环境配置文件
│           ├── application-test.yml    测试环境配置文件
│           ├── application.yml         正式环境配置文件
│           └── logback-spring.xml      日志配置文件
└── target
    ├── assembly-1.0-SNAPSHOT.jar
    └── assembly-1.0-SNAPSHOT.tar.gz    安装包

依赖插件

  • maven-assembly-plugin
  • maven-jar-plugin
  • maven-compiler-plugin

release.xml 说明

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>dist</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/assembly/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <directoryMode>0777</directoryMode>
            <includes>
                <include>**/*</include>
            </includes>
            <fileMode>0777</fileMode>
            <lineEnding>unix</lineEnding>
        </fileSet>
        <fileSet>
            <directory>target/classes</directory>
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.yml</include>
            </includes>
            <excludes>
                <exclude>generator/**</exclude>
                <exclude>mapper/**</exclude>
                <exclude>**/*.class</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <outputDirectory>logs</outputDirectory>
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>
</assembly>

pom.xml plugins 配置

注: 编译时不能使用spring-boot-maven-plugin插件,该插件会将依赖打入到jar包内,jar体积会增大

       <plugins>
            <!--
            打包时必需注释掉,避免打出安装包包含所有jar包;
            代码下载下来之后可以临时打开使用一次。-->
<!--            <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>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.github.assembly.AssemblyApplication</mainClass>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/assembly/release.xml</descriptor>
                    </descriptors>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>

示例地址

https://github.com/jack-fd/assembly.git

以上所述是介绍Springboot基于assembly的服务化打包方案,希望对大家有所帮助,如果大家有任何疑问请给我留言。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 时光匆匆独白 将颠沛磨成卡带 已枯卷的情怀 踏碎成年代 就老去吧 孤独别醒来
    MlzR阅读 153评论 0 0
  • 十六、飞鱼 生物被火团吞没后,白虎吐出的龙卷风,很快卷到火团旁,两者接触的一刹那,融成一束红白黑黄四色通天巨住...
    萁豆阅读 624评论 0 1
  • 是年,X市X墅,男导女星出事了,众传纷扬。 公安结案:系女星寂寞,在家换豢养万元洋犬,唤名公崽,遂食同桌,寝同床,...
    西电木子杰阅读 230评论 0 0