关于 Spring Boot - 2 的说明
这是一篇关于 Spring Boot 以及 JAR 和 WAR 部署的说明。
引言
这是一篇关于 Spring Boot 以及 JAR 和 WAR 部署的说明。
背景
在之前的说明中,我讨论了 Spring Boot 和 JAR 部署。在这篇说明中,我将探讨使用单一实现将 Sprint Boot 应用程序打包成 JAR 和 WAR 包的可能性。
通常,如果您使用 Spring Boot,您可能想将您的项目打包成 JAR。但是如果您确实想使用 servlet 容器,您仍然可以这样做。
- "spring-boot-example-implementation" 是一个 Maven 项目,它包含了 Spring Boot 应用程序的所有实现;
- "spring-boot-example-jar-package" 没有自己的源代码。它负责将应用程序打包成可执行的 JAR 包;
- "spring-boot-example-war-package" 没有自己的源代码。它负责将应用程序打包成 WAR 包,以便部署到 servlet 容器,例如 Tomcat。
- 所有这 3 个项目共享同一个父 POM 项目 "spring-boot-example",它为所有子项目提供通用的基本配置。
"spring-boot-example" POM
"spring-boot-example" 是一个简单的 POM 项目,它声明了三个模块级别的项目,并将编译器配置为目标 Java 1.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"> <properties> <spring-boot.version>1.5.7.RELEASE</spring-boot.version> </properties> <modelVersion>4.0.0</modelVersion> <groupId>com.song.example</groupId> <artifactId>spring-boot-example</artifactId> <version>0.0.1</version> <packaging>pom</packaging> <modules> <module>spring-boot-example-implementation</module> <module>spring-boot-example-jar-package</module> <module>spring-boot-example-war-package</module> </modules> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration><source>1.8</source><target>1.8</target></configuration> </plugin> </plugins> </build> </project
"spring-boot-example-implementation"
"spring-boot-example-implementation" 的 POM 如下。
<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> <parent> <groupId>com.song.example</groupId> <artifactId>spring-boot-example</artifactId> <version>0.0.1</version> </parent> <artifactId>spring-boot-example-implementation</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring-boot.version}</version> </dependency> </dependencies> </project>
为了节省我的时间,这个项目的内容与我之前的说明完全相同,除了 POM 文件。我鼓励您仔细阅读它,了解 Spring Boot 应用程序的关键部分。
"spring-boot-example-jar-package"
"spring-boot-example-jar-package" 项目没有源代码。它所做的只是将 "spring-boot-example-implementation" 项目打包成一个可以部署的可执行 JAR 文件。
<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> <parent> <groupId>com.song.example</groupId> <artifactId>spring-boot-example</artifactId> <version>0.0.1</version> </parent> <artifactId>spring-boot-example-jar-package</artifactId> <properties> <start-class>com.song.web.boot.ApplicationStart</start-class> </properties> <dependencies> <dependency> <groupId>com.song.example</groupId> <artifactId>spring-boot-example-implementation</artifactId> <version>0.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>com.song.web.boot.ApplicationStart</mainClass> </configuration> <executions> <execution> <goals><goal>repackage</goal></goals> </execution> </executions> </plugin> </plugins> </build> </project>
- 由于 "spring-boot-example-implementation" 项目已经声明了基本 Spring Boot 应用程序所需的所有依赖项,因此无需在此 POM 中声明任何其他包;
- 我们需要在 "spring-boot-maven-plugin" 中指定 "mainClass",以便 Java 知道在哪里找到 "static main()" 方法来启动程序。
"spring-boot-example-war-package"
"spring-boot-example-war-package" 项目没有源代码。它所做的只是将 "spring-boot-example-implementation" 项目打包成一个 WAR 文件,您可以将其部署到 servlet 容器。
<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> <parent> <groupId>com.song.example</groupId> <artifactId>spring-boot-example</artifactId> <version>0.0.1</version> </parent> <artifactId>spring-boot-example-war-package</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>com.song.example</groupId> <artifactId>spring-boot-example-implementation</artifactId> <version>0.0.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>${spring-boot.version}</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
- 当我们把应用程序部署到 servlet 容器时,我们不需要把内嵌的 Tomcat 打包到 WAR 文件中。"spring-boot-starter-tomcat" 依赖被标记为 "provided";
- 因为我们在项目中没有 "web.xml" 文件,所以我们需要指定 "failOnMissingWebXml" 为 false。
调试和运行
如果您将项目加载到 Eclipse 中,您可以直接在 "spring-boot-example-implementation" 项目上运行或调试应用程序。
在 "spring-boot-example-jar-package" 项目中,您可以使用以下命令来启动应用程序。
mvn spring-boot:run
在成功构建 Maven 后,您可以使用以下命令启动 JAR 文件。
java -jar target/spring-boot-example-jar-package.jar
如果您愿意,您也可以将 Tomcat 服务器连接到 "spring-boot-example-war-package" 以通过 Tomcat 服务器在 Eclipse 中运行和调试应用程序。
当然,您需要测试将您的 WAR 文件部署到一个独立的 servlet 容器。我已经在独立的 Tomcat 服务器中测试了我的 WAR。
- 当您将应用程序作为 JAR 启动时,它监听的端口号是 "8090"。端口号在 "spring-boot-example-implementation" 项目中配置。您可以查看我之前的说明以了解详细信息;
- 当您将应用程序作为 WAR 启动时,Tomcat 将不会遵守此端口号。它将监听它自己的端口号。在我的例子中,它是 "8080"。
以下显示了当我们在 Tomcat 中托管应用程序时从浏览器访问应用程序的结果。
关注点
- 这是一篇关于 Spring Boot 以及 JAR 和 WAR 部署的说明;
- 希望您喜欢我的博文,并希望这篇笔记能以某种方式帮助您。
历史
首次修订 - 2017/12/12