欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot jar包方式打包发布

程序员文章站 2024-02-02 12:46:22
...

很多大佬都已经写过了关于Spring boot 打包发布的博客,之前做项目的时候使用spring boot进行开发,碰到很多问题这里就写一个让自己回顾一下。

我这里是用idea 创建的spring boot项目

Spring boot jar包方式打包发布

创建spring boot项目选择这个后Next就可以了

Spring boot jar包方式打包发布

下一步后这个位置可以选择你用的爽的项目管理,我这里用的是maven,直接下一步。

Spring boot jar包方式打包发布

做的一个预备项目,先实现功能,这里就只用了这几个依赖。这样一个spring boot的项目就搭好了。

Spring boot jar包方式打包发布

进入后整个界面就是这样的,进来后先先写一个小Demo运行一下,运行成功就证码项目搭建好了,之后再配置application.yml文件,本来是.properties,为了看起来好看一些,这里就改成了.yml文件,已经配置

server:            
  port: 9090
  tomcat:
    accesslog:
      request-attributes-enabled: true
spring:
  datasource:
    data-username: flowmarket
    url: jdbc:mysql://localhost:3306/flowmarket?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
    username: miaoyue
    password: mysqlmiaoyue
    driver-class-name: com.mysql.jdbc.Driver
#静态文件配置 这东西是在做图片上传到本地服务器后无法通过url访问配置的一个地址映射
  mvc:
    static-path-pattern: /**
  resources:
    static-locations: classpath:/META-INF/resources/,classpath:/resources/,\
                                              classpath:/static/,classpath:/public/,file:${web.upload-path}
web.upload-path: /var/apps/userimg/
#分页工具配置  高端大气上档次的工具 好吧主要是我用起来爽
pagehelper:
  helper-dialect: mysql
  support-methods-arguments: true
  params: countSql
  reasonable: false
#mybatis 映射配置
mybatis:
  mapper-locations: classpath:mapper/*.xml

接下来POM文件配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.miaoyue.loanmall</groupId>
	<artifactId>loan_api</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>loan_api</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.9.5</version>
		</dependency>-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15</artifactId>
            <version>1.46</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</dependency>
		<!-- 分页插件pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.3</version>
		</dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.20</version>
		</dependency>
		<dependency>
			<groupId>com.TSign</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.6</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-core</artifactId>
			<version>3.0.1</version>
		</dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
		<dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
        <!-- 分页插件pagehelper -->
    </dependencies>

	<build>
		<finalName>loan</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.0.1.RELEASE</version>
				<configuration>
					<executable>true</executable>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
		</plugin>
			<!-- mybatis generator 自动生成代码插件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<configuration>
					<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
			</plugin>

			<!--用于解决没有web.xml报错-->
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.0.0</version>
			</plugin>
		</plugins>
	</build>


</project>

在POM文件配置好之后 就可以做下面 打包发布

Spring boot jar包方式打包发布

跳过测试单元打包,我这是因为打包时,测试类总是报错。逼不得已只能跳过。

打包完成后会在项目中生成一个项目名.jar的包这时候就可以用  java -jar 项目名.jar 来运行jar包

这样运行在服务器就不能进行其它操作,这时候我们需要在后台运行     

java -jar 项目名.jar &     这个命令就可以在后台运行

这样运行在服务器后台,有时候会挂断,所以直接使用下面这个命令就可以

nohup java -jar shareniu.jar &  

在其它环境下可能会遇到各种问题,不一定通用。写的不好还请见谅,有问题一起讨论讨论哈。

相关标签: spring boot jar