“This is the 39th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

This article project code gitee address: gitee.com/wei_rong_xi…

In this article, we’ll take a look at how to package your project and start execution directly with a startup script. Whether you are using NacOS, Skywalking, or any other component, you can start it directly from a script, without having to manually type commands, specify JVM parameters, etc. And of course, we wanted to do that for our own projects. For operation and maintenance personnel, deployment can provide great convenience.

First, start building

I’ll show you each step in detail below, using project Rob-outred-Gateway as an example.

1.1 to prepare package. The XML

What is package.xml for?

  • It specifies the format in which we end up packaging, usually ZIP.

  • Package the necessary project files into a ZIP package.

    • Readme.md, etc
    • *. Bat and *. Sh startup scripts
    • The jar package

The complete package.xml looks like this:


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

    <id>bin</id>

    <! -- Finally packaged into a zip file for publishing -->
    <formats>
        <format>zip</format>
    </formats>

    <fileSets>
        <! Package the project description files into the root directory of the zip file.
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>
                <include>README*</include>
                <include>CHANGELOG*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>

        <! Package the Windows environment startup script file in the project directory into the root directory of the zip file.
        <fileSet>
            <directory>./src/main/resources/package</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <lineEnding>windows</lineEnding>
            <includes>
                <include>*.bat</include>
            </includes>
        </fileSet>

        <! Package the Linux environment startup script file in the project directory into the root directory of the zip file.
        <fileSet>
            <directory>./src/main/resources/package</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <lineEnding>unix</lineEnding>
            <fileMode>0755</fileMode>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>

        <! Package the jar files of the project into the lib directory of the zip file.
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}lib</outputDirectory>
            <includes>
                <include>${project.artifactId}-${project.version}.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>
Copy the code

We create a Package folder to store packaging-related file information, including package.xml.

1.2 Writing the start.sh script

Since I am mostly deployed on a Linux server, this article only provides the.sh script, which requires the Windows.bat file, please search for it yourself.

What does start.sh do?

  • Get Java environment variables
  • Specifies JVM parameters
  • Assemble start command
  • Execute the assembled commands

The complete start.sh is shown below:

#! /bin/bash
error_exit ()
{
    echo "ERROR: The $1 !!"
    exit 1
}
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java
[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
[ ! -e "$JAVA_HOME/bin/java" ] && unset JAVA_HOME

if [ -z "$JAVA_HOME" ]; then
  if $darwin; then

    if [ -x '/usr/libexec/java_home'];then
      export JAVA_HOME=`/usr/libexec/java_home`

    elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then
      export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"
    fi
  else
    JAVA_PATH=`dirname $(readlink -f $(which javac))`
    if [ "x$JAVA_PATH"! ="x" ]; then
      export JAVA_HOME=`dirname $JAVA_PATH 2>/dev/null`
    fi
  fi
  if [ -z "$JAVA_HOME" ]; then
        error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better!"
  fi
fi

export SERVER="rob-necessities-gateway"

export JAVA_HOME
export JAVA="$JAVA_HOME/bin/java"
export BASE_DIR=`cd $(dirname $0) /.. ;pwd` /${SERVER}

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# JVM Configuration
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"

JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
if [[ "$JAVA_MAJOR_VERSION" -ge "9"]].then
  JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/gc.log:time,tags:filecount=10,filesize=102400"
else
  JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext"
  JAVA_OPT="${JAVA_OPT} -Xloggc:${BASE_DIR}/logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
fi

JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/lib/${SERVER}.jar"
JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"

echo "$JAVA ${JAVA_OPT}"

# start
nohup $JAVA ${JAVA_OPT} >> /dev/null 2>&1 &
echo "server is starting"
Copy the code

Now that the preparation is almost complete, let’s start working with the POM.xml file.

1.3 edit pom. XML

In the

tag of the POM file, add the following package plugin content:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <descriptors>
            <descriptor>./src/main/resources/package/package.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Copy the code

Two, packaging verification

After the previous preparation process, we can directly conduct packaging verification by using the command MVN install. Here I use the interface of IDEA to operate, and select install:

The packaged file is in the target directory of the project, as shown below:

The zip file is the one we need, and its contents are as follows:

In lib are jar packages.

Deployment verification

3.1 Environment Preparation

We upload the zip file to the server and unzip it for startup test. The decompression command is as follows:

[root@hecs-402944 opt]# unzip rob - necessities - gateway - 0.0.1 - the SNAPSHOT - bin. ZipArchive: Rob-scale-gateway-0.0.1 -SNAPSHOT-bin.zip creating: Rob-scale-gateway-0.0.1 -SNAPSHOT/ creating: Archive: Rob-scale-gateway-0.0.1 -SNAPSHOT/ creating: Rob - necessities - gateway - 0.0.1 - the SNAPSHOT/lib/inflating: rob - necessities - gateway - 0.0.1 - the SNAPSHOT/startup. Sh inflating: Rob - necessities - gateway - 0.0.1 - the SNAPSHOT/lib/rob - necessities - gateway - 0.0.1 - the SNAPSHOT. The jarCopy the code

Note: If you are installing the Oracle JDK, you will have no problem launching it directly. However, if you are installing the OpenJDK, you need to pay attention to two points:

  • Getting openJDK installed correctly usually requires a two-step installation process

    Install the jre:

    Sudo yum install java-1.8.0-openJDK -yCopy the code

    The JDK installation:

    Sudo yum install java-1.8.0-openjdk-devel-yCopy the code
  • The environment variables are configured correctly

    vi /etc/profile
    Copy the code

    Add environment variables to it

    exportJAVA_HOME = / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.322. B06-1. El7_9. X86_64export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    export PATH=$PATH:$JAVA_HOME/bin
    Copy the code

    Do not know the path to Java can be found using the following command and steps:

    [root @ hecs - 402944 rob - necessities - gateway - 0.0.1 - the SNAPSHOT]# which javaThe/usr/bin/Java/root @ hecs - 402944 rob - necessities - gateway - 0.0.1 - the SNAPSHOT]# ls -lrt /usr/bin/javaLRWXRWXRWX 1 root root 22 2月 25 11:04 /usr/bin/java -> /etc/alternatives/ Java [root@hecs-402944 Rob - necessities - gateway - 0.0.1 - the SNAPSHOT]# ls -lrt /etc/alternatives/javaLRWXRWXRWX 1 root root 73 2月 25 11:04 /etc/alternatives/ Java -> The/usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.322. B06-1. El7_9. X86_64 / jre/bin/JavaCopy the code

    Where /usr/lib/jvm/java-1.8.0-openJDK-1.8.0.322.b06-1.el7_9.x86_64 is the installation path.

3.2 start

Go to the decompressed project folder and run the script command:

[root @ hecs - 402944 rob - necessities - gateway - 0.0.1 - the SNAPSHOT]# ./startup.sh / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.322. B06-1. El7_9. X86_64 / bin/Java - Xms512m - Xmx512m - Xmn256m - Djava. Ext dirs = / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.322. B06-1. El7_9. X86_64 / jre/lib/ext: / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.322. B06-1. El7_9. X86_64 / lib/ext - Xloggc: / opt/rob - necessities - gateway/logs/gc log - verbose: gc - XX: + PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M  -jar /opt/rob-necessities-gateway/lib/rob-necessities-gateway.jar --server.max-http-header-size=524288 server is startingCopy the code

Note that if there is no change, you can execute the output command line directly and look at the error message, as follows:

[root @ hecs - 402944 rob - necessities - gateway - 0.0.1 - the SNAPSHOT]# /usr/lib/jvm/java-1.8.0-openJDK-1.8.0.322.b06-1.el7_9.x86_64 /bin/ Java -xms512m -xmx512m -xmn256m - Djava. Ext dirs = / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.322. B06-1. El7_9. X86_64 / jre/lib/ext: / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.322. B06-1. El7_9. X86_64 / lib/ext - Xloggc: / opt/rob - necessities - gateway/logs/gc log - verbose: gc - XX: + PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M  -jar /opt/rob-necessities-gateway/lib/rob-necessities-gateway.jar --server.max-http-header-size=524288
Error: Unable to access jarfile /opt/rob-necessities-gateway/lib/rob-necessities-gateway.jar
Copy the code

The jar package rob-expands-gateway. jar was not found. The reason is clear, our JAR package has version number:

How to modify?

Notice the package.xml file we mentioned earlier, as shown below:

The two parts in the red box are actually the project name and the project version of POM.xml, which together produce the name of the final JAR package.

So our configuration in startup.sh should be consistent with this, i.e

export SERVER="Rob - necessities - gateway - 0.0.1 - the SNAPSHOT"
Copy the code

Startup. sh: startup.sh: startup.sh: startup.sh


After all the above procedures, the project folder will generate its log file:

This is the end of this article! I hope it will be helpful.

This article project code gitee address: gitee.com/wei_rong_xi…