The Java non Web Springboot project is packaged and deployed to the Linux server (Maven, Gradle)

preface

Let's start directly. Non Web project deployment is very simple without complicated configuration

1, Modify pom.xml configuration file

Let me take Maven as an example

This is the case when my project is started locally. It is the same when the server is started after deployment. Stop it first.

Open pom.xml, compare whether the following code is missing in your build, and add the missing configuration by yourself. be careful! If the configuration of spring boot Maven plugin in your build contains lombok configuration, you must delete it, otherwise the package will fail. Deleting lombok configuration will not affect the use of lombok.

	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <!--Package the configuration required for deployment and enable true-->
                        <includeSystemScope>true</includeSystemScope>
                    </excludes>
                </configuration>
            </plugin>

            <!--Configuration required for package deployment-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!--Solve the package deployment error no main manifest attribute, in . . . jar-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.zjsos.pingyangparkinglot.PingyangParkinglotApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <!--Hot deployment configuration-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--fork: If this item is not configured, the entire devtools It won't work-->
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

2, Start packing

First, click "Terminal" under idea

Then enter "mvn clean" and enter

After it is completed, enter "mvn package" and press enter. When this step appears, the packaging is completed. The jar package path is in the target folder of the project root directory

Open the target, which contains the jar package named after your project

Before deploying to the server, you can test whether the jar package is normal. Enter cmd in the path bar of the folder where the jar package is located and press enter to open the command line

Enter java -jar in the command line, your jar package name. Jar, and press enter to run

It works normally. We deploy it to the server

3, Deploy to server

The server system takes Centos7 without interface as an example. The connection tool uses Xshell. Open Xshell, create a new one in the upper left corner, and enter the server IP to be connected in the "host". If there is no special change, the port is generally 22 or 122

After entering the IP and port, click "user authentication" on the left, enter the account and password, and click Connect

Welcome to xxx appears. The connection is successful

Deploy the project in the main directory of our mount. The mount is not necessary. The servers you buy are all mounted. Just find out which folder it is. Mine is / data. Enter / data, create the project folder, and enter the folder

Then click the green box above the Xshell to open the file transfer box Xftp of the graphical interface between the local machine and the server. On the left, find the jar package on the local machine and drag it to the server on the right

Then return to the Xshell and run the jar package, but this operation mode is only temporary. It will stop when you leave the interface. The following methods can keep the project running

First, download the file I gave you. This is a general script for jar to start on linux. You don't need points
Startup scripts and configuration files
After downloading, drag the files in the extracted folder to the folder where the server jar package is located

Enter ls to view the file, and then enter sh build.sh to run the file. It will show that the construction is successful. At this time, you will find an autoScript folder, cd autoScript, which contains several sh files, sh startup.sh, and the project will start and never stop unless you run sh shutdown.sh. OK, done. Rest assured to exit your server.

summary

In this part, Maven is taken as an example to package, but after the package is completed, the operation of deploying to the server is the same. The indolent.properties configuration file in the startup script can configure the configuration file on nacos. Personal small projects are temporarily unavailable. Later, the Web project for deploying Springboot will be written. The configuration of this environment is more complex. I'll put a portal at that time.

Tags: Java Linux Maven Spring Boot server

Posted on Sun, 05 Dec 2021 01:27:33 -0500 by meomike2000