spring boot + maven 打包 zip、tar.gz、dir(1)

1. pom.xml 插件配置

    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!--<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.luozongcong.demoundertow.DemoUndertowApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>-->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <!--2.3 、2.4 、2.6 、3.3.0-->
                <version>3.3.0</version>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skipAssembly>false</skipAssembly>
                    <finalName>${project.artifactId}-${project.version}-${profileActive}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <attach>false</attach>
                    <descriptors>
                        <descriptor>src/main/assembly/${profileActive}.xml</descriptor>
                    </descriptors>
                    <outputDirectory>./dist</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!--MAVEN打包选择运行环境-->
    <!-- dev:开发环境(默认) test:测试环境 uat:用户验收 prod:生产环境 -->
    <profiles>
        <profile>
            <id>windows</id>
            <properties>
                <profileActive>windows</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>linux</id>
            <properties>
                <profileActive>linux</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profileActive>prod</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>

2. logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 2019-2029 geekidea(https://demo.com/demoundertow)
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<configuration>

    <property name="CONTEXT_NAME" value="demoundertow"/>
    <property name="LOG_PATH" value="logs"/>
    <property name="MAX_FILE_SIZE" value="10MB"/>
    <property name="MAX_HISTORY" value="30"/>

    <contextName>${CONTEXT_NAME}</contextName>

    <!-- 彩色日志 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />

    <!-- 控制台日志样式 -->
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr([%15.15t]){faint} [%X{requestId}] %clr(%-40.40logger{39}){cyan} [%5.5L] %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
    <!-- 文件日志样式 -->
    <property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} [%t] [%X{requestId}] %-40.40logger{39} %L : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <!-- 禁用logback自身日志输出 -->
    <statusListener class="ch.qos.logback.core.status.NopStatusListener" />

    <!-- 控制台 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <!-- 运行日志文件 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_PATH}/${CONTEXT_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${CONTEXT_NAME}-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
    </appender>

    <!-- 错误日志文件 -->
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_PATH}/${CONTEXT_NAME}-error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_PATH}/${CONTEXT_NAME}-error-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
            <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 异步写日志 -->
    <appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>1024</queueSize>
        <appender-ref ref ="FILE"/>
    </appender>
    <appender name="ASYNC_ERROR_FILE" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>1024</queueSize>
        <appender-ref ref ="ERROR_FILE"/>
    </appender>

    <!-- 不同环境的日志级别配置 -->
    <springProfile name="dev">
        <!--<logger name="com.luozongcong.demoundertow" level="DEBUG"/>-->
    </springProfile>

    <!-- 解决SpringBootAdmin错误日志问题 -->
    <logger name="org.apache.catalina.connector.CoyoteAdapter" level="OFF"/>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="ASYNC_FILE" />
        <appender-ref ref="ASYNC_ERROR_FILE" />
    </root>

</configuration>

3. assembly配置

3.1 linux环境

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.3 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>linux</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <baseDirectory>${project.artifactId}-${project.version}</baseDirectory>

    <dependencySets>
        <dependencySet>
            <!-- Enable access to all projects in the current multimodule build! <useAllReactorProjects>true</useAllReactorProjects> -->
            <!-- Now, select which projects to include in this module-set. -->
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <useTransitiveDependencies>true</useTransitiveDependencies>
            <unpack>false</unpack>
            <useStrictFiltering>true</useStrictFiltering>
            <useTransitiveFiltering>true</useTransitiveFiltering>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>*</include>
            </includes>
            <fileMode>0777</fileMode>
            <outputDirectory>conf</outputDirectory>
            <lineEnding>unix</lineEnding>
        </fileSet>
        <fileSet>
            <directory>${basedir}/bin</directory>
            <includes>
                <include>*.sh</include>
            </includes>
            <fileMode>0777</fileMode>
            <outputDirectory>bin</outputDirectory>
            <lineEnding>unix</lineEnding>
        </fileSet>
        <fileSet>
            <directory>.</directory>
            <excludes>
                <exclude>*/**</exclude>
            </excludes>
            <outputDirectory>logs</outputDirectory>
        </fileSet>
    </fileSets>

</assembly>

3.2 windows环境

<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>windows</id>
    <formats>
        <format>dir</format>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>bin</directory>
            <includes>
                <include>*.bat</include>
            </includes>
            <outputDirectory>./</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>./</directory>
            <includes>
                <include>README.*</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>java</directory>
            <includes>
                <include>*</include>
            </includes>
            <outputDirectory>java</outputDirectory>
            <filtered>true</filtered>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <includes>
                <include>*</include>
            </includes>
            <outputDirectory>config</outputDirectory>
            <filtered>true</filtered>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

4. bin 快捷启动

4.1 linux下启动run.sh(./run.sh)

#!/bin/bash

cd `dirname $0`
cd ..
HOME=`pwd`

export DemoUndertowApplication_SERVER_PID=$HOME/bin/linkis.pid

if [[ -f "${DemoUndertowApplication_SERVER_PID}" ]]; then
    pid=$(cat ${DemoUndertowApplication_SERVER_PID})
    if kill -0 ${pid} >/dev/null 2>&1; then
      echo "DemoUndertowApplication_SERVER Remote Server $pid is already running."
      exit 1
    fi
fi

export DemoUndertowApplication_SERVER_LOG_PATH=$HOME/logs
export DemoUndertowApplication_SERVER_HEAP_SIZE="512M"
#启动类
export DemoUndertowApplication_SERVER_CLASS=${DemoUndertowApplication_SERVER_CLASS:-com.luozongcong.demoundertow.DemoUndertowApplication}
echo "启动类 DemoUndertowApplication_SERVER_CLASS is : $DemoUndertowApplication_SERVER_CLASS !"

profiles='dev'
if [ $1 ];then
        type=$1
fi

export DemoUndertowApplication_SERVER_JAVA_OPTS=" -Xmx$DemoUndertowApplication_SERVER_HEAP_SIZE -XX:+UseG1GC  -Xloggc:$HOME/logs/DemoUndertowApplication-gc.log"

#java $DemoUndertowApplication_SERVER_JAVA_OPTS -cp $HOME/conf:$HOME/lib/* $DemoUndertowApplication_SERVER_CLASS --spring.profiles.active=$profiles  2>&1 > $DemoUndertowApplication_SERVER_LOG_PATH/DemoUndertowApplication.log &
nohup java -Xms64m -Xmx128m $DemoUndertowApplication_SERVER_JAVA_OPTS -cp $HOME/conf:$HOME/lib/* $DemoUndertowApplication_SERVER_CLASS --spring.profiles.active=$profiles  > ./bin/nohup.out 2>&1 &

pid=$!
sleep 2
if [[ -z "${pid}" ]]; then
    echo "DemoUndertowApplication SERVER start failed!"
    exit 1
else
    echo "DemoUndertowApplication SERVER $pid start succeed!"
    echo $pid > $DemoUndertowApplication_SERVER_PID
fi

4.2 linux下停止stop.sh(./stop.sh)

#!/bin/bash

cd `dirname $0`
cd ..
HOME=`pwd`

export SERVER_PID=$HOME/bin/linkis.pid

function wait_for_server_to_die() {
  local pid
  local count
  pid=$1
  timeout=$2
  count=0
  timeoutTime=$(date "+%s")
  let "timeoutTime+=$timeout"
  currentTime=$(date "+%s")
  forceKill=1

  while [[ $currentTime -lt $timeoutTime ]]; do
    $(kill ${pid} > /dev/null 2> /dev/null)
    if kill -0 ${pid} > /dev/null 2>&1; then
      sleep 3
    else
      forceKill=0
      break
    fi
    currentTime=$(date "+%s")
  done

  if [[ forceKill -ne 0 ]]; then
    $(kill -9 ${pid} > /dev/null 2> /dev/null)
  fi
}

if [[ ! -f "${SERVER_PID}" ]]; then
    echo "server is not running!"
else
    pid=$(cat ${SERVER_PID})
    if [[ -z "${pid}" ]]; then
      echo "server is not running!!"
    else
      wait_for_server_to_die $pid 40
      $(rm -f ${SERVER_PID})
      echo "server $SERVER_PID $pid is stopped!!!."
    fi
fi

4.3 linux下实时日志查看log.sh(./log.sh)

tail -f nohup.out

4.4 windows下已配置jdk的启动startup.bat(双击)

title com.luozongcong.demoundertow.DemoUndertowApplication
set MAIN_CLASS=com.luozongcong.demoundertow.DemoUndertowApplication
set CLASSPATH=lib/*
java -cp %CLASSPATH%; %MAIN_CLASS%

4.5 windows 下指定jdk路径的启动startup-uselocal.bat(双击)

title com.luozongcong.demoundertow.DemoUndertowApplication
set MAIN_CLASS=com.luozongcong.demoundertow.DemoUndertowApplication
set CLASSPATH=lib/*
set SCRIPT_DIR=%~dp0
set JVM_DIR=%cd%\java
for %%I in ("%SCRIPT_DIR%") do set JAVA_HOME=%%~dpfIjava\jdk1.8.0_191
echo %JAVA_HOME%
%JAVA_HOME%\bin\java -cp %CLASSPATH%; %MAIN_CLASS%
pause

5. windows下打包

5.1 打包结果

 

 5.3 linux下打包结果

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值