在 Java Web 开发中,Spring 框架凭借其强大的功能和便捷的开发体验,成为了众多开发者的首选。当我们完成一个基于 Spring 的 Web 应用开发后,就需要将其打包并部署到服务器上。其中,打包为可执行 JAR 是一种非常常见且便捷的方式。本文将详细介绍如何将 Spring Web 应用打包为可执行 JAR 并进行部署。
可执行 JAR 文件是一种包含了所有依赖库和资源的 JAR 文件,它可以直接在 Java 虚拟机(JVM)上运行,无需额外配置复杂的服务器环境。这种方式简化了应用的部署过程,提高了部署效率。
在开始打包之前,我们需要创建一个简单的 Spring Web 应用。这里我们使用 Spring Initializr 来快速创建项目,添加以下依赖:
创建好的项目结构大致如下:
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ └── controller
│ │ └── HelloController.java
│ └── resources
│ ├── application.properties
│ └── static
│ └── templates
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
HelloController.java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Web!";
}
}
如果你使用的是 Maven 作为项目构建工具,在 pom.xml
中添加以下配置:
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在项目根目录下,打开命令行工具,执行以下命令进行打包:
mvn clean package
执行完毕后,在 target
目录下会生成一个以项目名和版本号命名的 JAR 文件,例如 demo-0.0.1-SNAPSHOT.jar
。
如果你使用的是 Gradle 作为项目构建工具,在 build.gradle
中添加以下配置:
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
在项目根目录下,打开命令行工具,执行以下命令进行打包:
gradle clean build
执行完毕后,在 build/libs
目录下会生成一个以项目名和版本号命名的 JAR 文件,例如 demo-0.0.1-SNAPSHOT.jar
。
打包完成后,我们可以将生成的 JAR 文件部署到服务器上。以下是部署步骤:
java -jar demo-0.0.1-SNAPSHOT.jar
http://localhost:8080/hello
,如果看到 Hello, Spring Web!
则表示应用部署成功。为了让应用在服务器后台持续运行,我们可以使用 nohup
命令:
nohup java -jar demo-0.0.1-SNAPSHOT.jar > app.log 2>&1 &
该命令将应用的输出重定向到 app.log
文件中,并在后台运行。
步骤 | 操作内容 |
---|---|
1 | 创建 Spring Web 应用 |
2 | 配置项目构建工具(Maven 或 Gradle) |
3 | 执行打包命令(mvn clean package 或 gradle clean build ) |
4 | 将生成的 JAR 文件上传到服务器 |
5 | 在服务器上使用 java -jar 命令启动应用 |
6 | (可选)使用 nohup 命令让应用在后台运行 |
通过将 Spring Web 应用打包为可执行 JAR,我们可以简化部署过程,提高开发和运维效率。希望本文能帮助你顺利完成 Spring 应用的打包和部署。