[Learn] self centos7 + maven + jenkins + springboot multi-module module project deployment

  1. Come have a git repository, which is based on a spring boot maven project, open the kind of success can be run directly by the IDE, not saying much.
  2. centos7 64 bit, comes java1.8 environment. (I was deepin system)
  3. See the developer installed in the machine maven environment (in an effort to install the same version in the server maven).
    View maven version of IDEA.
    Here Insert Picture Description
  4. Installation and start jenkins, guarantee available, slightly, the article is.
  5. Jenkins install the necessary plug-in, maven-intergration, publish-over-ssh.
  6. Configure the plug.

Configuring maven-intergration of
problems maven, and before you install a maven tools available can also be configured specifically to install a maven also be jenkins in (I use is this way, but very slow, waited at least 10min).
Skip.

Publish over SSH configuration

Here Insert Picture Description
Configuration
https://blog.csdn.net/MenofGod/article/details/81941223

Note If you do not want to use this way of publickkey ssh connection, you can also connect with a username and password.
Point the "Advanced" button, and then test connection to see results.

  1. Construction of multi-module module project
    if the project is to build a single module is very simple to find the corresponding target folders jar file to run in production.
    But multi-module project of how to do it?

    I structure this pilot project in the following figure:
    Here Insert Picture Description
    In this pom file (arrow @SpringBootApplicationadd the following to the module where the pom.xml file):

    </dependencies>

    <build>
        <finalName>testcode</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.1.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

最终项目build完成之后,会在starter项目的target文件夹中生成一个 testcode.jar 文件,直接用命令行运行这个jar即可,已亲测。

java -jar testcode.jar

如此即可看到springboot项目的成功启动,并可正常访问。(当然前提是你的java命令可用,防火墙端口等已允许通过)

但实际上我们每次build的时候还是应该把原来的进程kil掉,再重新build生成。
因此我们并不会每次用手工,要用命令来完成。

  1. 相关命令和脚本

经查找生成的包的路径var/lib/jenkins/jobs/flag2020-maven-springboot/workspace/starter/target

启动命令:

java -jar testcode.jar

可看到项目启动成功。
Here Insert Picture Description

尝试一下kill进程(这句命令有点复杂,以后还要学习,来自https://blog.csdn.net/zjh_746140129/article/details/80904876)):

ps -ef | grep testcode.jar | grep -v grep | awk '{print $2}' | xargs kill -9

Here Insert Picture Description
可见kill成功。

扩展阅读:

grep -v grep https://blog.csdn.net/dieyong/article/details/93008252
awk用法 https://www.cnblogs.com/bugingcode/p/8287914.html
linux判断某个进程是否存在https://blog.csdn.net/dc_show/article/details/41806265

我专门打开一个浏览器页面访问这个springboot项目的swagger页面。
几次尝试后,发现每当我启动项目时,页面有内容。
每当我kill之后,页面就不可访问。
可见这两个命令确实实现了作用。

  1. 讲上述kill进程等内容写入脚本。
    学习了网上找到的其他资料,改写了脚本如下:
#!/bin/bash
#判断进程是否存在,如果不存在就启动它如果存在就重启它
PIDS=`ps -ef |grep testcode.jar |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
	kill -9 $PIDS
fi

#ps -ef | grep testcode.jar | grep -v grep | awk '{print $2}' | xargs kill -9
cd /var/lib/jenkins/jobs/my-project/workspace/starter/target
java -jar testcode.jar

Here Insert Picture Description

然后执行,顺利成功!
Here Insert Picture Description

  1. However, due to reasons script, started springboot, the script does not terminate and return, jenkins has been in a "build in progress" status, as shown below.
    How to handle it?
    Here Insert Picture Description
    To be mentioned here nohup命令, a lot of information, just search it on the line.
    Here I put before recording.
    linux nohup

  2. jenkins inaccessible after the build is complete, how else?
    The investigation is completed and then jenkins build kill off related processes, we are now trying to not let it kill enough.

So the transformation of the script a bit and put in two places.
Here Insert Picture Description
Here Insert Picture Description
Then finally he succeeded.

Here Insert Picture Description

Finally be completed desired results, roughly where the record about.

Published 177 original articles · won praise 47 · views 430 000 +

Guess you like

Origin blog.csdn.net/festone000/article/details/104027964