美文网首页
spirng boot 简单应用部署方式

spirng boot 简单应用部署方式

作者: jack_fd | 来源:发表于2018-10-15 10:24 被阅读0次

默认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的服务化打包方案,希望对大家有所帮助,如果大家有任何疑问请给我留言。

相关文章

网友评论

      本文标题:spirng boot 简单应用部署方式

      本文链接:https://www.haomeiwen.com/subject/xegxzftx.html