打包

2017-06-16  本文已影响4人  perfect_jimmy

项目结构如下:

image.png

上传到服务器,解压之后:

image.png
  1. pom.xml配置
 <build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-assembly-plugin</artifactId>
               <configuration>
                   <appendAssemblyId>false</appendAssemblyId>
                   <descriptors>
                       <!-- 描述文件路径 -->
                       <descriptor>src/assembly/assembly.xml</descriptor>
                   </descriptors>
               </configuration>
               <executions>
                   <execution>
                       <!--名字任意 -->
                       <id>make-assembly</id>
                       <!-- 绑定到package生命周期阶段上 -->
                       <phase>package</phase>
                       <goals>
                           <!-- 只运行一次 -->
                           <goal>single</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
        </plugins>
    </build>
  1. assembly.xml
<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>bin</id>
    <formats>
        <format>tar.gz</format><!--打包的结果格式-->
    </formats>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>src\main\resources</directory>
            <outputDirectory>\config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src\bin</directory>
            <outputDirectory>\bin</outputDirectory>
            <lineEnding>unix</lineEnding>
            <fileMode>0755</fileMode>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>
  1. sh文件
    env.sh
#!/usr/bin/env bash
UEAP_HOME=$BASE_DIR
SERVER_NAME="springtutorial"
STARTUP_CLASS="com.tutorial.AppSpring"
MAXMEM="1g"
MINMEM="256m"
MAXPERM="512m"

export CLASSPATH=$BASE_DIR/config:$(ls $BASE_DIR/lib/*.jar | tr '\n' :)

run.sh

#! /bin/sh
if [ -z "$BASE_DIR" ] ; then
  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
  BASE_DIR=`dirname "$PRG"`/..

  BASE_DIR=`cd "$BASE_DIR" && pwd`
fi

source $BASE_DIR/bin/env.sh

#LOG_DIR="$BASE_DIR/logs"
#LOG_FILE="$LOG_DIR/server.log"
PID_DIR="$BASE_DIR/bin"
PID_FILE="$PID_DIR/.run.pid"

function running(){
    if [ -f "$PID_FILE" ]; then
        pid=$(cat "$PID_FILE")
        process=`ps aux | grep " $pid " | grep -v grep`;
        if [ "$process" == "" ]; then
            return 1;
        else
            return 0;
        fi
    else
        return 1
    fi
}

if running; then
    echo "$SERVER_NAME is running."
    exit 1
fi

jdk=`cat /etc/profile |grep -i java_home=|cut -d= -f2-|sed -e 's/ //g'`/bin/java
$jdk -version >/dev/null 2>&1

if [ $? -ne 0 ]
then
    echo "the os did not install java!"
    exit 1
fi

mkdir -p $PID_DIR
#touch $LOG_FILE
#mkdir -p $LOG_DIR

sleep 1
nohup $jdk -Xmx$MAXMEM -Xms$MINMEM -XX:PermSize=$MAXPERM -XX:MaxPermSize=$MAXPERM $STARTUP_CLASS >>/dev/null 2>&1 &
echo $! > $PID_FILE
chmod 755 $PID_FILE

echo "start $SERVER_NAME service:"
PID=$!
sleep 1
echo "start master finished, PID=$PID"
echo "checking if $PID is running..."
sleep 1
kill -0 $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
    echo "$PID is running, start $SERVER_NAME server."
    echo "$SERVER_NAME run successful."
else
    echo "start $SERVER_NAME failed."
fi
#tail -F $LOG_FILE

stop.sh

#!/usr/bin/env bash
if [ -z "$BASE_DIR" ] ; then
  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
  BASE_DIR=`dirname "$PRG"`/..

  BASE_DIR=`cd "$BASE_DIR" && pwd`
fi

source $BASE_DIR/bin/env.sh


LOG_DIR="$BASE_DIR/logs"
LOG_FILE="$LOG_DIR/server.log"
PID_DIR="$BASE_DIR/bin"
PID_FILE="$PID_DIR/.run.pid"

function running(){
    if [ -f "$PID_FILE" ]; then
        pid=$(cat "$PID_FILE")
        process=`ps aux | grep " $pid " | grep -v grep`;
        if [ "$process" == "" ]; then
            return 1;
        else
            return 0;
        fi
    else
        return 1
    fi
}

if ! running; then
    echo "$SERVER_NAME is not running."
    exit 1
fi
count=0
pid=$(cat $PID_FILE)
while running;
do
    let count=$count+1
    echo "Stopping $SERVER_NAME $count times"
    if [ $count -gt 5 ]; then
        echo "kill -9 $pid"
        kill -9 $pid
    else
        ##$JAVA $TOOLS_ARGS com.starit.ueap.common.shell.StopServerTool -host 127.0.0.1 -port $JMX_PORT $@
        kill $pid
    fi
    sleep 3;
done
echo "Stop $SERVER_NAME successfully."
rm $PID_FILE
上一篇下一篇

猜你喜欢

热点阅读