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.
This article mainly introduces some packaging matters and project deployment of SpringBoot as well as solutions to some problems encountered in it.
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.
First is themavenway to package:
If it isjarpackage
needs to be inpom.xml
The specified package is:
jar
If it is awarpackage.
You need to specify the package inpom.xml
:
war
and use the
tag to excludetomcat when packagingDependency
org.springframework.boot spring-boot-starter-tomcat provided
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
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
After adding the corresponding tags inpom.xml, we only need to add ) Enter
mvn clean package
to complete the packaging
If you want to exclude the test code, you can enter:
mvn clean package -Dmaven.test.skip=true
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
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
pom.xmlAdd
com.panncm.utils pancm-utils 1.0
to package.
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 jar
Specify 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.
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.
注:
是指定文件存放的文件夹,executable是使用cmd命令,line是执行的语句, 标签是将文件复制到指定的文件夹中。
然后再新建一个build.bat文件,里面只需要填写ant
就行了。
准备完之后,只需双击build.bat,项目和配置文件就自动到build文件中了,省去了很多操作。
虽然现在流行通过jenkins进行打包部署,不过使用ant加maven进行打包也不错的,比较简单。
解决办法一:
在properties添加
和
UTF-8 UTF-8 1.8 1.2.41 1.8 1.8
解决方案二:
在plugin中添加和
org.apache.maven.plugins maven-compiler-plugin 3.3 1.8
实际是可以下载,但是无法将此打入包中
解决办法:
在pom.xml中添加
spring-milestone http://repo.spring.io/libs-release
原因: 在clean的时候,target里面的文件被占用了。
解决办法: 不占用就行了。
如果是jar项目
Windows系统在项目同级目录下输入:
java -jar springboot-package
即可启动项目。
关闭项目,只需关掉dos界面就可以了。
也可以写一个bat文件进行运行。
示例:
@echo off title "springboot-package" java -jar springboot-package.jar
Linux系统在项目同级目录下输入:
nohup -jar springboot-package &
即可启动。
关闭输入:
kill -9 pid(jar的进程id)
也可以在init.d
注册一个服务
示例:
ln -s /home/jars/app/springboot-package.jar /etc/init.d/springboot-package chmod +x /etc/init.d/springboot-package
然后输入:
service springboot-package start|stop|restart
进行启动或者停止。
当然也可以编写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..."
如果是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
相关推荐:
编写简单的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!