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 executable jar package or a war package that runs under tomcat.
Although this project is built with maven, it is more convenient to package with maven, but here is also an explanation of how to package ordinary non-maven packaged projects.
First is the maven way to package:
If it is jar package
needs to be in pom.xml
The specified package is:
<packaging>jar</packaging>
If it is a war package.
You need to specify the package in pom.xml
:
<packaging>war</packaging>
and use the 06db57cb000bdd2564c5b32a302b10e2
tag to exclude tomcat when packaging Dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
Then add SpringBootThe built-in packaging method
The example is as follows:
<build> <defaultGoal>compile</defaultGoal> <sourceDirectory>src</sourceDirectory> <finalName>springboot-package</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> <configuration> <fork>true</fork> <mainClass>com.pancm.App</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Note:1079ce9524e93e3b51b338f3745023bd
The tag specifies the name after packaging, ff90c0d76fd88f6f044391486acd42eb
specifies the main function.
You can also use the assembly plug-in of maven instead of SpringBoot's own packaging method for packaging.
The example is as follows:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.pancm.App</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
After adding the corresponding tags in pom.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 the application.properties and logback.xml files in the resources folder, but after packaging, they will also be included in the jar or war package, 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 defaultlogback is in the classpath directory. At this time, we only need to specify the path of logback.xml in the application.properties configuration file.
Add the following:
logging.config=logback.xml
If a third-party jar package is introduced, but it cannot be downloaded through the maven private server, you can compile it manually.
For example, I wrote a tool class as Mytools, then made it into a jar package, and then placed it in my project lib directory and need to reference it, then you can compile the jar package into the local warehouse, and then pom.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
<dependency> <groupId>com.panncm.utils</groupId> <artifactId>pancm-utils</artifactId> <version>1.0</version> </dependency>
to package.
If it is an ordinary project and is not built using maven, you can use eclipse and other tools for packaging.
If it is a jar package
First run the project in eclipse (run by main method), and then run it in eclipse Right-click the project export ->java -> runnable jar file-> package required libraries into generated jar
Specify the main method, and then select the packaging name and packaging path. Click finish to complete packaging.
If it is a war package
right-click the projectexport ->web -> war file in eclipse
, and then select package The name and packaging path. Click finish to complete packaging.
After introducing the above two kinds of packaging, here is an introduction to packaging through the ant method (need to install the ant environment, 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 use ant to package and integrate the packaged files. together.
Here we will write a build.xml configuration file.
<?xml version="1.0" encoding="UTF-8"?> <project name="springboot-package" default="copyAll" basedir="."> <property name="build" value="build" /> <property name="target" value="target" /> <target name="clean"> <delete dir="${target}" /> <delete dir="${build}" /> </target> <target name="create-path" depends="clean"> <mkdir dir="${build}" /> </target> <target name="mvn_package" depends="create-path"> <exec executable="cmd" failonerror="true"> <arg line="/c mvn install:install-file -Dfile=lib/pancmtools.jar -DgroupId=com.panncm.utils -DartifactId=pancm-utils -Dversion=1.0 -Dpackaging=jar" /> </exec> <exec executable="cmd" failonerror="true"> <arg line="/c mvn clean package" /> </exec> </target> <target name="copyAll" depends="mvn_package"> <copy todir="${build}" file="${target}/springboot-package.jar"></copy> <copy todir="${build}" file="logback.xml"></copy> <copy todir="${build}" file="application.properties"></copy> <copy todir="${build}" file="run.bat"></copy> </target> </project>
注:ae6323cd3d9073a255ce30140f0b2f26
是指定文件存放的文件夹,executable是使用cmd命令,line是执行的语句, 标签是将文件复制到指定的文件夹中。
然后再新建一个 build.bat文件,里面只需要填写 ant
就行了。
准备完之后,只需双击build.bat,项目和配置文件就自动到build文件中了,省去了很多操作。
虽然现在流行通过jenkins进行打包部署,不过使用ant加maven进行打包也不错的,比较简单。
解决办法一:
在properties添加9f275c8252e38cef439ca9640a8586f71.8473c0ef5895a4256631a21aef4882dcf
和ddbf6c0d8fbca8bf4037c277cd3b3ba41.8b0d16fe7221e0b94e3dda453a094b16f
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <fastjson>1.2.41</fastjson> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
解决方案二:
在plugin中添加 e02da388656c3265154666b7c71a8ddc1.8055a816595e1cf5e4b7c6fe05c404a6c
和 b4bef09dd2761803871f1d83e55d08b21.802ae89aa2f73e0c6b8dded97c507c40d
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
实际是可以下载,但是无法将此打入包中
解决办法:
在pom.xml中添加
<repositories> <repository> <id>spring-milestone</id> <url>http://repo.spring.io/libs-release</url> </repository> </repositories>
原因: 在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!