微信登录

部署 - 打包与部署 - 打包为可执行 JAR

Java - Web - Spring 《部署 - 打包与部署 - 打包为可执行 JAR》

一、引言

在 Java Web 开发中,Spring 框架凭借其强大的功能和便捷的开发体验,成为了众多开发者的首选。当我们完成一个基于 Spring 的 Web 应用开发后,就需要将其打包并部署到服务器上。其中,打包为可执行 JAR 是一种非常常见且便捷的方式。本文将详细介绍如何将 Spring Web 应用打包为可执行 JAR 并进行部署。

二、可执行 JAR 概述

可执行 JAR 文件是一种包含了所有依赖库和资源的 JAR 文件,它可以直接在 Java 虚拟机(JVM)上运行,无需额外配置复杂的服务器环境。这种方式简化了应用的部署过程,提高了部署效率。

三、准备工作

在开始打包之前,我们需要创建一个简单的 Spring Web 应用。这里我们使用 Spring Initializr 来快速创建项目,添加以下依赖:

  • Spring Web
  • Spring Boot DevTools(可选,用于开发时热部署)

项目结构

创建好的项目结构大致如下:

  1. src
  2. ├── main
  3. ├── java
  4. └── com
  5. └── example
  6. └── demo
  7. ├── DemoApplication.java
  8. └── controller
  9. └── HelloController.java
  10. └── resources
  11. ├── application.properties
  12. └── static
  13. └── templates
  14. └── test
  15. └── java
  16. └── com
  17. └── example
  18. └── demo
  19. └── DemoApplicationTests.java

示例代码

1. DemoApplication.java

  1. package com.example.demo;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class DemoApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9. }

2. HelloController.java

  1. package com.example.demo.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloController {
  6. @GetMapping("/hello")
  7. public String hello() {
  8. return "Hello, Spring Web!";
  9. }
  10. }

四、打包为可执行 JAR

使用 Maven 打包

如果你使用的是 Maven 作为项目构建工具,在 pom.xml 中添加以下配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.example</groupId>
  7. <artifactId>demo</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <parent>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-parent</artifactId>
  13. <version>2.5.4</version>
  14. <relativePath/> <!-- lookup parent from repository -->
  15. </parent>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-devtools</artifactId>
  27. <scope>runtime</scope>
  28. <optional>true</optional>
  29. </dependency>
  30. </dependencies>
  31. <build>
  32. <plugins>
  33. <plugin>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-maven-plugin</artifactId>
  36. </plugin>
  37. </plugins>
  38. </build>
  39. </project>

在项目根目录下,打开命令行工具,执行以下命令进行打包:

  1. mvn clean package

执行完毕后,在 target 目录下会生成一个以项目名和版本号命名的 JAR 文件,例如 demo-0.0.1-SNAPSHOT.jar

使用 Gradle 打包

如果你使用的是 Gradle 作为项目构建工具,在 build.gradle 中添加以下配置:

  1. plugins {
  2. id 'org.springframework.boot' version '2.5.4'
  3. id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  4. id 'java'
  5. }
  6. group = 'com.example'
  7. version = '0.0.1-SNAPSHOT'
  8. sourceCompatibility = '1.8'
  9. repositories {
  10. mavenCentral()
  11. }
  12. dependencies {
  13. implementation 'org.springframework.boot:spring-boot-starter-web'
  14. runtimeOnly 'org.springframework.boot:spring-boot-devtools'
  15. testImplementation 'org.springframework.boot:spring-boot-starter-test'
  16. }
  17. test {
  18. useJUnitPlatform()
  19. }

在项目根目录下,打开命令行工具,执行以下命令进行打包:

  1. gradle clean build

执行完毕后,在 build/libs 目录下会生成一个以项目名和版本号命名的 JAR 文件,例如 demo-0.0.1-SNAPSHOT.jar

五、部署可执行 JAR

打包完成后,我们可以将生成的 JAR 文件部署到服务器上。以下是部署步骤:

  1. 将 JAR 文件上传到服务器的指定目录。
  2. 打开命令行工具,进入 JAR 文件所在目录。
  3. 执行以下命令启动应用:
    1. java -jar demo-0.0.1-SNAPSHOT.jar
  4. 应用启动后,在浏览器中访问 http://localhost:8080/hello,如果看到 Hello, Spring Web! 则表示应用部署成功。

后台运行

为了让应用在服务器后台持续运行,我们可以使用 nohup 命令:

  1. nohup java -jar demo-0.0.1-SNAPSHOT.jar > app.log 2>&1 &

该命令将应用的输出重定向到 app.log 文件中,并在后台运行。

六、总结

步骤 操作内容
1 创建 Spring Web 应用
2 配置项目构建工具(Maven 或 Gradle)
3 执行打包命令(mvn clean packagegradle clean build
4 将生成的 JAR 文件上传到服务器
5 在服务器上使用 java -jar 命令启动应用
6 (可选)使用 nohup 命令让应用在后台运行

通过将 Spring Web 应用打包为可执行 JAR,我们可以简化部署过程,提高开发和运维效率。希望本文能帮助你顺利完成 Spring 应用的打包和部署。

部署 - 打包与部署 - 打包为可执行 JAR