美文网首页
springboot之assembly打包插件(二)

springboot之assembly打包插件(二)

作者: 超天大圣JR | 来源:发表于2021-03-23 15:56 被阅读0次

    assembly打包插件之可修改配置文件(yml等)

    一、目录结构

    -stest-service
      -script
        -bin
          -env.sh
          -start.sh
          -stop.sh
      -src
        -main
          -assembly
            -assembly.xml
          -java
            -com.baidu.api
            -Application
          -resource
            -application.yml
            -logback-spring.xml
      -pom.xml
    

    二、需要的依赖包(pom.xml)和 maven的构建过程

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.qax</groupId>
        <artifactId>stest-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <description>Demo project for Spring Boot</description>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    
    
        <build>
            <!-- 打包后的启动jar名称 -->
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <!-- 用于排除jar中依赖包 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                        <layout>ZIP</layout>
                        <includes>
                            <!-- 项目启动jar包中排除依赖包 -->
                            <include>
                                <groupId>nothing</groupId>
                                <artifactId>nothing</artifactId>
                            </include>
                        </includes>
                    </configuration>
                </plugin>
    
                <!-- 将依赖cp到lib目录下 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-lib</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <!-- maven编译 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <!-- 不同版本需要制定具体的版本进行编译 -->
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
    
                <!-- 打包时跳过测试 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
    
                <!-- 将项目中代码文件打成jar包 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <!-- 打包后的jar包中不包括配置文件 -->
                            <!-- 通常是指classpath下目录下的文件,这样可以避免编写时的找不到相应文件 -->
                            <exclude>*.xml</exclude>
                            <exclude>*.properties</exclude>
                            <exclude>*.yml</exclude>
                        </excludes>
                        <archive>
                            <manifest>
                                <!-- 项目启动类 -->
                                <mainClass>com.qax.api.StestApplication</mainClass>
                                <!-- 依赖的jar的目录前缀 -->
                                <classpathPrefix>./lib/</classpathPrefix>
                                <!-- 将依赖加进 Class-Path -->
                                <addClasspath>true</addClasspath>
                                <!-- 是否使用唯一的时间戳快照版本而不是-快照版本。默认值为 true -->
                                <useUniqueVersions>false</useUniqueVersions>
                            </manifest>
                            <!-- 将config目录加入classpath目录 -->
                            <manifestEntries>
                                <Class-Path>./config/</Class-Path>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
    
                <!-- 打包插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <!-- 配置包名称 -->
                        <finalName>${project.artifactId}--${project.version}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>install</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    
    

    三、集成过程(assembly.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly>
        <!-- 可自定义,这里指定的是项目名称,打包唯一标识-->
        <id>service</id>
        <!-- 打包的类型,如果有N个,将会打N个类型的包 -->
        <formats>
            <format>tar.gz</format>
            <format>zip</format>
        </formats>
        <!--外层是否包一层-->
        <includeBaseDirectory>true</includeBaseDirectory>
    
        <fileSets>
            <!-- 配置文件打包-打包至config目录下 -->
            <fileSet>
                <directory>src/main/resources/</directory>
                <outputDirectory>config</outputDirectory>
                <fileMode>0644</fileMode>
                <includes>
                    <include>*.yml</include>
                    <include>*.xml</include>
                    <include>*.properties</include>
                </includes>
            </fileSet>
            <!-- 启动文件目录 -->
            <fileSet>
                <directory>${basedir}/script/bin</directory>
                <outputDirectory>bin</outputDirectory>
                <fileMode>0755</fileMode>
                <includes>
                    <include>**.sh</include>
                    <include>**.bat</include>
                </includes>
            </fileSet>
            <fileSet>
                <directory>${project.basedir}/target</directory>
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
    
    
            <fileSet>
                <directory>${project.build.directory}/lib</directory>
                <outputDirectory>/lib</outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
                <fileMode>0755</fileMode>
            </fileSet>
        </fileSets>
    </assembly>
    

    四、logback配置

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration debug="false">
        <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
        <property name="LOG_HOME" value="/xxx/xxx/xxx/logs/services" />
        <springProperty scope="context" name="log.name" source="spring.application.name" defaultValue="localhost.log"/>
        <!-- 控制台输出 -->
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{43}\(%L\) : %msg%n</pattern>
            </encoder>
        </appender>
        <!-- 按照每天生成日志文件 -->
        <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%m:日志消息,%n是换行符-->
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{43}\(%L\) : %msg%n</pattern>
            </encoder>
            <append>true</append>
            <file>${LOG_HOME}/${log.name}/${log.name}.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>${LOG_HOME}/${log.name}/${log.name}.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>4</maxIndex>
            </rollingPolicy>
    
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>50MB</maxFileSize>
            </triggeringPolicy>
        </appender>
        <!-- show parameters for hibernate sql 专为 Hibernate 定制 -->
        <logger name="org.hibernate.type.descriptor.sql.BasicBinder"  level="INFO" />
        <logger name="org.hibernate.type.descriptor.sql.BasicExtractor"  level="INFO" />
        <logger name="org.hibernate.SQL" level="INFO" />
        <logger name="org.hibernate.engine.QueryParameters" level="INFO" />
        <logger name="org.hibernate.engine.query.HQLQueryPlan" level="INFO" />
    
        <!-- 日志输出级别 -->
        <root level="INFO">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="FILE" />
        </root>
    
    </configuration>
    
    

    五、application.yml配置

    server:
      port: 12222
    spring:
      application:
        name: stest-service
    
    logging:
      config: classpath:logback-spring.xml
    

    六、脚本文件

    1. env.sh
    
    #!/bin/sh
    export SERVER="stest-service"
    export SERVER_PORT="12222"
    
    . ${NGSOC_INSTALL_PATH}/bin/shell/set-env.sh
    
    ================================================================================
    2. start.sh
    
    #!/bin/sh
    
    export BASE_DIR=`cd $(dirname $0)/..; pwd`
    
    . ${BASE_DIR}/bin/env.sh
    
    pid=`ps -ef | grep java | grep ${SERVER} | grep -v grep | awk '{print $2}'`
    if [ "$pid" ] ; then
        echo "${SERVER} running."
        exit -1;
    fi
    
    nohup java -Xms2048m -Xmx2048m -jar ${BASE_DIR}/${SERVER}.jar > /dev/null 2>&1 &
    
    count=0
    result=`ss -ntlp | grep ${SERVER_PORT}`
    while [ -z "$result" ]; do
        if [ $count -eq 60 ];then
            echo "${SERVER} Start Failed,"
            exit -1
        fi
            count=`expr $count + 1`
        sleep 1
        result=`ss -ntlp | grep ${SERVER_PORT}`
    done
    echo "${SERVER} Started Successfully"
    
    ===============================================================================
    
    3. stop.sh
    
    #!/bin/sh
    
    export BASE_DIR=`cd $(dirname $0)/..; pwd`
    
    . ${BASE_DIR}/bin/env.sh
    
    pid=`ps -ef | grep java | grep ${SERVER} | grep -v grep | awk '{print $2}'`
    if [ -z "$pid" ] ; then
        echo "No ${SERVER} running."
        exit -1;
    fi
    echo "The ${SERVER}(${pid}) is running..."
    kill ${pid}
    echo "Send shutdown request to ${SERVER}(${pid}) OK"
    
    

    七、打包结果

     -target
       -stest-service.jar
       -stest-service--0.0.1-SNAPSHOT.tar.gz
       -stest-service--0.0.1-SNAPSHOT.zip
    

    八、gz包解析后目录

    stest-service--0.0.1-SNAPSHOT
      -bin
        -env.shassembly
        -start.sh
        -stop.sh
      -config
        -application.yal
        -logback-spring.xml
      -lib
        -xxx.jar
        -xxx.jar
        -xxx.jar
        ......
      -stest-service.jar
    

    相关文章

      网友评论

          本文标题:springboot之assembly打包插件(二)

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