Springboot打包成jar发布

本文介绍Spring Boot项目打包与运行方法。打包方式有jar包和war包,介绍了二者区别及打包步骤。还提及打包中遇到的编码、Maven版本、资源文件打包问题及解决办法。最后说明运行jar包的命令,以及发布到服务器时数据库配置的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打包的方式

  • 打包成jar包
  • 打包成war包
    区别:jar包内置了tomcat、netty等服务器,更改只需要修改pom.xml的坐标即可,war不内置服务器,需要上传到服务器tomcat解压后运行

如何打包?

  1. 打包成jar,pom.xml中设置打包的形式,war/jar包
 <packaging>jar</packaging>
  1. 添加maven打包插件
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
  1. 正常情况下,IDEA右侧点击Maven----》clean-----》package即可打包成功。
    在这里插入图片描述

所遇到的问题,及解决办法

  1. 提示要设置UTF-8 编码,主要是编码格式不对。
  2. maven版本过高
  3. 没有把项目resource文件的配置文件以及XML文件一起打包
    然后就会出现以下问题。---------------------》
Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.

在这里插入图片描述

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources) on project bookshop: Input length = 1 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

在这里插入图片描述

解决办法

1、设置IDEA的项目编码格式为UTF-8
在这里插入图片描述
如果不行,就在pom.xml中添加以下配置:

 <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
     <!--  跟 <dependencies>同级-->

2.maven版本过高,降低maven版本

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
</plugin>

3.如果配置了上面的依旧不行,,,那就一定要配置下面这个,主要是把resource文件配置,一起打包到jar包里。(我就是上面两部都不行,配置了这个就好了)
在pom.xml中配置

  <resources>
            <!--把java下的.xml和properties文件编译打包-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <!-- </resources>  跟   </plugins> 同级,都是在<build>标签内-->

结果(此时,再次IDEA右侧点击Maven----》clean-----》package即可打包成功)

在这里插入图片描述
这里就是我们辛辛苦苦打包出来的jar包了
在这里插入图片描述

如何运行它呢?

没错,就是它-----》target 目录下 打开控制台 使用命令 java -jar bookshop-0.0.1-SNAPSHOT.jar 就可以起来了。如果要发布到服务器,使用云端数据,那你要配置数据库文件的IP、端口、账号、密码、连接方式(尤其要降低mysql版本或者升高版本的要特别注意)

看看效果

在这里插入图片描述
在这里插入图片描述

到这里终于完成了。如果熟悉linux的话可以发布到linux

关于运行命令的话可以去了解相关的命令

nohup java -jar xxx.jar &
java -jar xxx.jar 
java -jar xxx.jar &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值