Home> Java> javaTutorial> body text

How does SpringBoot perform simple packaging and deployment?

不言
Release: 2018-09-12 16:16:38
Original
1741 people have browsed it

The content of this article is about how to perform simple packaging and deployment of SpringBoot? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Preface

This article mainly introduces some packaging matters and project deployment of SpringBoot as well as solutions to some problems encountered in it.

SpringBoot packaging

In SpringBoot packaging, we use a previous web project for packaging.
The first thing that needs to be clarified is whether the project is packaged in an executablejarpackage or awarpackage that runs undertomcat.
Although this project is built withmaven, it is more convenient to package withmaven, but here is also an explanation of how to package ordinary non-mavenpackaged projects.

Maven packaging

First is themavenway to package:
If it isjarpackage
needs to be inpom.xmlThe specified package is:

jar
Copy after login

If it is awarpackage.
You need to specify the package inpom.xml:

war
Copy after login

and use thetag to excludetomcat when packagingDependency

 org.springframework.boot spring-boot-starter-tomcat provided 
Copy after login

Then addSpringBootThe built-in packaging method
The example is as follows:

 compile src springboot-package   org.springframework.boot spring-boot-maven-plugin   true com.pancm.App     repackage      
Copy after login

Note:The tag specifies the name after packaging,specifies the main function.

You can also use theassemblyplug-in ofmaveninstead ofSpringBoot's own packaging method for packaging.
The example is as follows:

   org.apache.maven.plugins maven-assembly-plugin 2.5.5    com.pancm.App    jar-with-dependencies     
Copy after login

After adding the corresponding tags inpom.xml, we only need to add ) Enter

mvn clean package
Copy after login

to complete the packaging
If you want to exclude the test code, you can enter:

mvn clean package -Dmaven.test.skip=true
Copy after login

to package.

Generally we put theapplication.propertiesandlogback.xmlfiles in the resources folder, but after packaging, they will also be included in thejarorwarpackage, if we want to change the configuration, it will be more troublesome.
If you want to put them in the same directory as the project,application.propertiesYou can directly move them out of the directory at the same level as the project, because the Spring program will be loaded from the following paths according to priorityapplication.propertiesConfiguration file:

  • /config directory under the current directory

  • Current directory

  • /config directory in classpath

  • classpath root directory

springbootloaded by defaultlogbackis in theclasspathdirectory. At this time, we only need to specify the path oflogback.xmlin theapplication.propertiesconfiguration file.
Add the following:

logging.config=logback.xml
Copy after login

If a third-partyjarpackage is introduced, but it cannot be downloaded through themavenprivate server, you can compile it manually.
For example, I wrote a tool class asMytools, then made it into ajarpackage, and then placed it in my projectlibdirectory and need to reference it, then you can compile thejarpackage into the local warehouse, and thenpom.xmladd the corresponding name and version number.
Command example:

mvn install:install-file -Dfile=lib/pancmtools.jar -DgroupId=com.panncm.utils -DartifactId=pancm-utils -Dversion=1.0 -Dpackaging=jar
Copy after login

pom.xmlAdd

 com.panncm.utils pancm-utils 1.0 
Copy after login

to package.

Ordinary project packaging

If it is an ordinary project and is not built usingmaven, you can useeclipseand other tools for packaging.
If it is ajarpackage
First run the project ineclipse(run bymainmethod), and then run it ineclipseRight-click the projectexport ->java -> runnable jar file-> package required libraries into generated jarSpecify themainmethod, and then select the packaging name and packaging path. Clickfinishto complete packaging.

If it is awarpackage
right-click the projectexport ->web -> war fileineclipse, and then select package The name and packaging path. Clickfinishto complete packaging.

Ant Packaging

After introducing the above two kinds of packaging, here is an introduction to packaging through theantmethod (need to install theantenvironment, installation method Basically the same as maven, specify the path and configure environment variables, I won’t go into details here).
Generally after packaging, we need to put the package and configuration files in a directory. If we don’t want to copy and paste manually, we can useantto package and integrate the packaged files. together.
Here we will write abuild.xmlconfiguration file.

                         
Copy after login

注:是指定文件存放的文件夹,executable是使用cmd命令,line是执行的语句, 标签是将文件复制到指定的文件夹中。

然后再新建一个build.bat文件,里面只需要填写ant就行了。
准备完之后,只需双击build.bat,项目和配置文件就自动到build文件中了,省去了很多操作。

虽然现在流行通过jenkins进行打包部署,不过使用ant加maven进行打包也不错的,比较简单。

打包遇到的一些问题

问题:source-1.5 中不支持 diamond运算符

解决办法一:
在properties添加
1.81.8

 UTF-8 UTF-8 1.8 1.2.41 1.8 1.8 
Copy after login

解决方案二:
在plugin中添加1.81.8

   org.apache.maven.plugins maven-compiler-plugin 3.3  1.8 1.8    
Copy after login

问题二:打包出现某jar包无法打入

实际是可以下载,但是无法将此打入包中

解决办法:
pom.xml中添加

  spring-milestone http://repo.spring.io/libs-release  
Copy after login

问题三:mvn clean 失败,出现Failed to execute goal

原因: 在clean的时候,target里面的文件被占用了。
解决办法: 不占用就行了。

SpringBoot部署

如果是jar项目
Windows系统在项目同级目录下输入:

java -jar springboot-package
Copy after login

即可启动项目。
关闭项目,只需关掉dos界面就可以了。
也可以写一个bat文件进行运行。
示例:

@echo off title "springboot-package" java -jar springboot-package.jar
Copy after login

Linux系统在项目同级目录下输入:

nohup -jar springboot-package &
Copy after login

即可启动。
关闭输入:

kill -9 pid(jar的进程id)
Copy after login

也可以在init.d注册一个服务
示例:

ln -s /home/jars/app/springboot-package.jar /etc/init.d/springboot-package chmod +x /etc/init.d/springboot-package
Copy after login

然后输入:

service springboot-package start|stop|restart
Copy after login

进行启动或者停止。
当然也可以编写xshell脚本进行启动和关闭。
示例:

#!/bin/bash APPDIR=`pwd` PIDFILE=$APPDIR/springboot-package.pid if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then echo "springboot-package is already running..." exit 1 fi nohup java -jar $APPDIR/springboot-package.jar >/dev/null 2>&1 & echo $! > $PIDFILE echo "start springboot-package..."
Copy after login

如果是war项目
war放在tomcat/webapp目录下,然后启动tomcat就可以了。Windows系统 在tomcat/bin目录下双击startup.bat即可启动,双击shutdown.bat关闭。
Linux系统则在tomcat/bin目录下输入startup.sh即可启动, 输入shutdown.sh关闭
附SpringBoot打包部署的项目工程地址:
https://github.com/xuwujing/springBoot-study/tree/master/springboot-package

相关推荐:

Tomcat部署Web项目该如何实现?

编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行

The above is the detailed content of How does SpringBoot perform simple packaging and deployment?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!