SpringBoot打包jar、war部署项目

悠悠 2022-06-14 04:18 403阅读 0赞

我的项目是在IntelliJ里面做的,springboot框架做的web项目打包部署有2种方式,一种是jar包,一种是war文件。

第一种:

首先打包war文件形式:

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>SpringBootDemo</groupId>
  7. <artifactId>SpringBootDemo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>war</packaging>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <!-- 这里一定要配置上java的版本,如果是1.7版本的可不用配置 -->
  13. <java.version>1.7</java.version>
  14. <servlet.version>3.0.1</servlet.version>
  15. </properties>
  16. <build>
  17. <plugins>
  18. <plugin>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-maven-plugin</artifactId>
  21. </plugin>
  22. </plugins>
  23. </build>
  24. <!-- 父依赖 -->
  25. <parent>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-parent</artifactId>
  28. <version>1.5.2.RELEASE</version>
  29. </parent>
  30. <dependencies>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-web</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  43. </dependency>
  44. <!--打包war必须的依赖-->
  45. <dependency>
  46. <groupId>javax.servlet</groupId>
  47. <artifactId>javax.servlet-api</artifactId>
  48. <version>${servlet.version}</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-tomcat</artifactId>
  53. <!--<scope>provided</scope>-->
  54. </dependency>
  55. </dependencies>
  56. </project>

Application.java启动文件:

  1. package com;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.builder.SpringApplicationBuilder;
  5. import org.springframework.boot.web.support.SpringBootServletInitializer;
  6. /**
  7. * Created by Administrator on 2017/6/2.
  8. */
  9. @SpringBootApplication
  10. public class Application extends SpringBootServletInitializer{
  11. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  12. return application.sources(Application.class);
  13. }
  14. public static void main(String[] args) {
  15. SpringApplication.run(Application.class, args);
  16. }
  17. }

然后

Center

Center 1

build之后在target文件夹下面就会看到SpringBootDemo-1.0-SNAPSHOT.war文件,丢到tomcat上面运行就可以了。

第二种打包jar部署:

首先下载apache-maven-3.5.0(版本随意,我下的最新的)

配置环境环境变量自己看着来:

Center 2

然后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>SpringBootDemo</groupId>
  7. <artifactId>SpringBootDemo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <!-- 这里一定要配置上java的版本,如果是1.7版本的可不用配置 -->
  13. <java.version>1.7</java.version>
  14. <servlet.version>3.0.1</servlet.version>
  15. </properties>
  16. <build>
  17. <plugins>
  18. <plugin>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-maven-plugin</artifactId>
  21. <configuration>
  22. <executable>true</executable>
  23. </configuration>
  24. <executions>
  25. <execution>
  26. <goals>
  27. <goal>repackage</goal>
  28. </goals>
  29. </execution>
  30. </executions>
  31. </plugin>
  32. </plugins>
  33. </build>
  34. <!-- 父依赖 -->
  35. <parent>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-parent</artifactId>
  38. <version>1.5.2.RELEASE</version>
  39. </parent>
  40. <dependencies>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-web</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-test</artifactId>
  48. <scope>test</scope>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  53. </dependency>
  54. </dependencies>
  55. </project>

Application.java:

  1. package com;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * Created by Administrator on 2017/6/2.
  6. */
  7. @SpringBootApplication
  8. public class Application {
  9. public static void main(String[] args) {
  10. // 启动Spring Boot项目的唯一入口
  11. SpringApplication.run(Application.class, args);
  12. }
  13. }

开始使用mvn打包,进到项目根目录下:执行mvn clean package如下图

Center 3

执行完毕之后找到target文件夹,使用java -jar 执行就可以了,如下图:

Center 4

springboot的两种打包方式以及完毕。

发表评论

表情:
评论列表 (有 0 条评论,403人围观)

还没有评论,来说两句吧...

相关阅读