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 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.
Maven packaging
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 <scope></scope>
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:<finalname></finalname>
The tag specifies the name after packaging, <mainclass></mainclass>
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.
Ordinary project packaging
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.
Ant 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>
注:<mkdir dir="${build}"></mkdir>
是指定文件存放的文件夹,executable是使用cmd命令,line是执行的语句, 标签是将文件复制到指定的文件夹中。
然后再新建一个 build.bat文件,里面只需要填写 ant
就行了。
准备完之后,只需双击build.bat,项目和配置文件就自动到build文件中了,省去了很多操作。
虽然现在流行通过jenkins进行打包部署,不过使用ant加maven进行打包也不错的,比较简单。
打包遇到的一些问题
问题:source-1.5 中不支持 diamond运算符
解决办法一:
在properties添加<maven.compiler.source>1.8</maven.compiler.source>
和<maven.compiler.target>1.8</maven.compiler.target>
<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中添加 <source>1.8</source>
和 <target>1.8</target>
<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>
问题二:打包出现某jar包无法打入
实际是可以下载,但是无法将此打入包中
解决办法:
在pom.xml中添加
<repositories> <repository> <id>spring-milestone</id> <url>http://repo.spring.io/libs-release</url> </repository> </repositories>
问题三:mvn clean 失败,出现Failed to execute goal
原因: 在clean的时候,target里面的文件被占用了。
解决办法: 不占用就行了。
SpringBoot部署
如果是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!

How does Java alleviate platform-specific problems? Java implements platform-independent through JVM and standard libraries. 1) Use bytecode and JVM to abstract the operating system differences; 2) The standard library provides cross-platform APIs, such as Paths class processing file paths, and Charset class processing character encoding; 3) Use configuration files and multi-platform testing in actual projects for optimization and debugging.

Java'splatformindependenceenhancesmicroservicesarchitecturebyofferingdeploymentflexibility,consistency,scalability,andportability.1)DeploymentflexibilityallowsmicroservicestorunonanyplatformwithaJVM.2)Consistencyacrossservicessimplifiesdevelopmentand

GraalVM enhances Java's platform independence in three ways: 1. Cross-language interoperability, allowing Java to seamlessly interoperate with other languages; 2. Independent runtime environment, compile Java programs into local executable files through GraalVMNativeImage; 3. Performance optimization, Graal compiler generates efficient machine code to improve the performance and consistency of Java programs.

ToeffectivelytestJavaapplicationsforplatformcompatibility,followthesesteps:1)SetupautomatedtestingacrossmultipleplatformsusingCItoolslikeJenkinsorGitHubActions.2)ConductmanualtestingonrealhardwaretocatchissuesnotfoundinCIenvironments.3)Checkcross-pla

The Java compiler realizes Java's platform independence by converting source code into platform-independent bytecode, allowing Java programs to run on any operating system with JVM installed.

Bytecodeachievesplatformindependencebybeingexecutedbyavirtualmachine(VM),allowingcodetorunonanyplatformwiththeappropriateVM.Forexample,JavabytecodecanrunonanydevicewithaJVM,enabling"writeonce,runanywhere"functionality.Whilebytecodeoffersenh

Java cannot achieve 100% platform independence, but its platform independence is implemented through JVM and bytecode to ensure that the code runs on different platforms. Specific implementations include: 1. Compilation into bytecode; 2. Interpretation and execution of JVM; 3. Consistency of the standard library. However, JVM implementation differences, operating system and hardware differences, and compatibility of third-party libraries may affect its platform independence.

Java realizes platform independence through "write once, run everywhere" and improves code maintainability: 1. High code reuse and reduces duplicate development; 2. Low maintenance cost, only one modification is required; 3. High team collaboration efficiency is high, convenient for knowledge sharing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
