SpringBoot项目打包成jar并运行

1、确保SpringBoot项目在IDE中可以正常运行

2、在pom.xml中添加如下配置

<build>
    
    <finalName>springboot_quick_1finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
                
                <mainClass>com.lcy.ApplicationmainClass>
            configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackagegoal>
                    goals>
                execution>
            executions>
        plugin>
    plugins>
build>

为防止部分网友弄错配置,如下展示添加配置后pom.xml文件中的所有内容

<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.0modelVersion>
    <groupId>com.lcygroupId>
    <artifactId>springboot_quick_1artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.0.RELEASEversion>
    parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

<build>
    
    <finalName>springboot_quick_1finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
                
                <mainClass>com.lcy.ApplicationmainClass>
            configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackagegoal>
                    goals>
                execution>
            executions>
        plugin>
    plugins>
build>
project>

3、进入项目所在文件夹内,执行命令:mvn install

4、等第3步执行完成,界面显示BUILD SUCCESS后,再执行命令:mvn package spring-boot:repackage

5、等第4步执行完成,界面显示BUILD SUCCESS后,进入项目的target文件夹内,可以找到jar

SpringBoot项目打包成jar并运行_第1张图片

 

 6、运行jar:java -jar springboot_quick_1.jar

SpringBoot项目打包成jar并运行_第2张图片

你可能感兴趣的:(SpringBoot项目打包成jar并运行)