How does SpringBoot perform simple packaging and deployment?
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>
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>
The tag specifies the name after packaging, <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}" />
是指定文件存放的文件夹,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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Introduction to Jasypt Jasypt is a java library that allows a developer to add basic encryption functionality to his/her project with minimal effort and does not require a deep understanding of how encryption works. High security for one-way and two-way encryption. , standards-based encryption technology. Encrypt passwords, text, numbers, binaries... Suitable for integration into Spring-based applications, open API, for use with any JCE provider... Add the following dependency: com.github.ulisesbocchiojasypt-spring-boot-starter2. 1.1Jasypt benefits protect our system security. Even if the code is leaked, the data source can be guaranteed.

Usage scenario 1. The order was placed successfully but the payment was not made within 30 minutes. The payment timed out and the order was automatically canceled. 2. The order was signed and no evaluation was conducted for 7 days after signing. If the order times out and is not evaluated, the system defaults to a positive rating. 3. The order is placed successfully. If the merchant does not receive the order for 5 minutes, the order is cancelled. 4. The delivery times out, and push SMS reminder... For scenarios with long delays and low real-time performance, we can Use task scheduling to perform regular polling processing. For example: xxl-job Today we will pick

1. Redis implements distributed lock principle and why distributed locks are needed. Before talking about distributed locks, it is necessary to explain why distributed locks are needed. The opposite of distributed locks is stand-alone locks. When we write multi-threaded programs, we avoid data problems caused by operating a shared variable at the same time. We usually use a lock to mutually exclude the shared variables to ensure the correctness of the shared variables. Its scope of use is in the same process. If there are multiple processes that need to operate a shared resource at the same time, how can they be mutually exclusive? Today's business applications are usually microservice architecture, which also means that one application will deploy multiple processes. If multiple processes need to modify the same row of records in MySQL, in order to avoid dirty data caused by out-of-order operations, distribution needs to be introduced at this time. The style is locked. Want to achieve points

Springboot reads the file, but cannot access the latest development after packaging it into a jar package. There is a situation where springboot cannot read the file after packaging it into a jar package. The reason is that after packaging, the virtual path of the file is invalid and can only be accessed through the stream. Read. The file is under resources publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

When Springboot+Mybatis-plus does not use SQL statements to perform multi-table adding operations, the problems I encountered are decomposed by simulating thinking in the test environment: Create a BrandDTO object with parameters to simulate passing parameters to the background. We all know that it is extremely difficult to perform multi-table operations in Mybatis-plus. If you do not use tools such as Mybatis-plus-join, you can only configure the corresponding Mapper.xml file and configure The smelly and long ResultMap, and then write the corresponding sql statement. Although this method seems cumbersome, it is highly flexible and allows us to

1. Customize RedisTemplate1.1, RedisAPI default serialization mechanism. The API-based Redis cache implementation uses the RedisTemplate template for data caching operations. Here, open the RedisTemplate class and view the source code information of the class. publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations, BeanClassLoaderAware{//Declare key, Various serialization methods of value, the initial value is empty @NullableprivateRedisSe

SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

This article will write a detailed example to talk about the actual development of dubbo+nacos+Spring Boot. This article will not cover too much theoretical knowledge, but will write the simplest example to illustrate how dubbo can be integrated with nacos to quickly build a development environment.
