通过appassembler-maven-plugin和maven-assembly-plugin插件生成spring boot执行包

注意事项

使用该方法配置时请不要使用spring-boot-maven-plugin插件。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

1. appassembler-maven-plugin配置

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.10</version>
    <configuration>
        <!--lib下直接存放jar,没有路径文件夹(如com/apache),如果没有这个选项则放在lib下的jar包会被com/apache类似的多层文件夹包裹起来-->
        <repositoryLayout>flat</repositoryLayout>
        <configurationDirectory>conf</configurationDirectory>
        <configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
        <!-- 对配置目录开启资源过滤 -->
        <filterConfigurationDirectory>true</filterConfigurationDirectory>
        <copyConfigurationDirectory>true</copyConfigurationDirectory>
        <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
        <!--生成的项目的目录位置,这里的client是项目的名称,你可以根据你的需要自己随便命名-->
        <assembleDirectory>${project.build.directory}/appassembler</assembleDirectory>
        <binFileExtensions>
            <unix>.sh</unix>
        </binFileExtensions>
        <platforms>
<!--                        <platform>windows</platform>-->
            <platform>unix</platform>
        </platforms>
        <repositoryName>lib</repositoryName>
        <encoding>UTF-8</encoding>
        <programs>
            <program>
                <!--指定主类,脚本名。会生成shell/bat两种类型,也可用platforms指定运行平台-->
                <mainClass>com.tm.sbia.Application</mainClass>
                <!-- 生成的脚本文件的名称,比如start.sh,你也可以根据你的需要命名成其他名字 -->
                <id>start</id>
                <!--java启动参数-->
                <jvmSettings>
                    <extraArguments>
                        <extraArgument>-server</extraArgument>
                        <extraArgument>-Xmx2G</extraArgument>
                        <extraArgument>-Xms2G</extraArgument>
                    </extraArguments>
                </jvmSettings>
            </program>
        </programs>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>assemble</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2. maven-assembly-plugin配置

2.1 maven configuration

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
    	<!-- 指定最终生成安装包的名字 -->
        <finalName>tm-sbia</finalName>
        <descriptors>
            <descriptor>src/main/resources/assembly/package.xml</descriptor>
        </descriptors>
    </configuration>
</plugin>

2.2 package.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<assembly
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
        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>pkg</id>
    <formats>
        <format>zip</format>
    </formats>
	
	<!-- 生成的zip包中为 tm-sbia-pkg.zip -->
	<!-- zip包中包含目录 sbia.tm.com -->
	<!-- Sets the base directory of the resulting assembly archive.
    If this is not set and includeBaseDirectory == true, ${project.build.finalName} will be used instead.
    (Since 2.2-beta-1) -->
    <baseDirectory>sbia.tm.com</baseDirectory>
    <includeBaseDirectory>true</includeBaseDirectory>
    
    <fileSets>
        <fileSet>
            <directory>target/appassembler/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>start.sh</include>
            </includes>
        </fileSet>

        <!-- 把项目的脚本文件目录( src/cmd )中的启动脚本文件,打包进zip文件的跟目录 -->
        <!-- 因为appassembler-maven-plugin只生成启动脚本,所以新增停止脚本,并放置到src/cmd目录下 -->
        <fileSet>
            <directory>src/cmd</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>stop.sh</include>
            </includes>
            <filtered>true</filtered>
            <fileMode>755</fileMode>
        </fileSet>

        <fileSet>
            <directory>target/appassembler/conf</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
            	<include>application.yml</include>
                <include>*prod.yml</include>
                <include>*test.yml</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>target/appassembler/lib</directory>
            <outputDirectory>lib</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

3. start.sh – generated by appassemble

#!/bin/sh
# ----------------------------------------------------------------------------
#  Copyright 2001-2006 The Apache Software Foundation.
#
#  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.
# ----------------------------------------------------------------------------
#
#   Copyright (c) 2001-2006 The Apache Software Foundation.  All rights
#   reserved.


# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

PRGDIR=`dirname "$PRG"`
BASEDIR=`cd "$PRGDIR/.." >/dev/null; pwd`

# Reset the REPO variable. If you need to influence this use the environment setup file.
REPO=


# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  Darwin*) darwin=true
           if [ -z "$JAVA_VERSION" ] ; then
             JAVA_VERSION="CurrentJDK"
           else
             echo "Using Java version: $JAVA_VERSION"
           fi
		   if [ -z "$JAVA_HOME" ]; then
		      if [ -x "/usr/libexec/java_home" ]; then
			      JAVA_HOME=`/usr/libexec/java_home`
			  else
			      JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
			  fi
           fi       
           ;;
esac

if [ -z "$JAVA_HOME" ] ; then
  if [ -r /etc/gentoo-release ] ; then
    JAVA_HOME=`java-config --jre-home`
  fi
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi

# If a specific java binary isn't specified search for the standard 'java' binary
if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD=`which java`
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly." 1>&2
  echo "  We cannot execute $JAVACMD" 1>&2
  exit 1
fi

if [ -z "$REPO" ]
then
  REPO="$BASEDIR"/lib
fi

CLASSPATH="$BASEDIR"/conf:"$REPO"/*

ENDORSED_DIR=
if [ -n "$ENDORSED_DIR" ] ; then
  CLASSPATH=$BASEDIR/$ENDORSED_DIR/*:$CLASSPATH
fi

if [ -n "$CLASSPATH_PREFIX" ] ; then
  CLASSPATH=$CLASSPATH_PREFIX:$CLASSPATH
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  [ -n "$HOME" ] && HOME=`cygpath --path --windows "$HOME"`
  [ -n "$BASEDIR" ] && BASEDIR=`cygpath --path --windows "$BASEDIR"`
  [ -n "$REPO" ] && REPO=`cygpath --path --windows "$REPO"`
fi

exec "$JAVACMD" $JAVA_OPTS -server -Xmx2G -Xms2G \
  -classpath "$CLASSPATH" \
  -Dapp.name="start" \
  -Dapp.pid="$$" \
  -Dapp.repo="$REPO" \
  -Dapp.home="$BASEDIR" \
  -Dbasedir="$BASEDIR" \
  com.tm.sbia.Application \
  "$@"

4. stop.sh

#!/bin/bash

# main class
MAIN_CLASS="com.tm.sbia.Application"

pid=$(ps -ef | grep "$MAIN_CLASS" | grep -v grep | awk '{print $2}');

if [[ -z "${pid}" ]]; then
    echo application is already stop
else
    echo kill ${pid}
    kill -9 ${pid}
fi

5. 最终安装包的目录结构

在这里插入图片描述

参考

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值