SpringBoot项目打包war发布到Jetty9
1.前言
虽然官方推荐SpringBoot项目采用jar包方式发布,但是也可以打成war包发布到外部web容器(如Jetty,tomcat等)。接下来我们讨论一下,如何将一个SpringBoot项目打包成war包。
更多参考:《SpringBoot项目发布到服务器的两种方式war和jar》。
2.搭建SpringBoot项目
2.1创建SpringBoot目录
参考《Maven的约定》手工创建工程目录。当然,你绝对可以使用IDE来创建。
# 存放pom.xml和所有的子目录
~/Desktop$ mkdir FlowerShop
# 项目的java源代码
~/Desktop/FlowerShop$ mkdir -p src/main/java
# 创建包com.flower和com.flower.controller
~/Desktop/FlowerShop$ mkdir -p src/main/java/com/flower/controller
# 目录结构
~/Desktop/FlowerShop$ tree -L 6
.
└── src └── main └── java └── com └── flower └── controller
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
这个目录对于我们将要演示的内容已经足够了。
2.2 初始化pom.xml
创建pom.xml
~/Desktop/FlowerShop$ touch pom.xml
- 1
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.flower</groupId> <artifactId>FlowerShop</artifactId> <version>1.0-SNAPSHOT</version> <!--指定打包类型:pom,war,jar--> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <!--指定打包的名称:FlowerShopDemo.war或FlowerShopDemo.jar--> <finalName>FlowerShopDemo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
2.3创建启动类
创建MainApplicationStarter.java启动类
~/Desktop/FlowerShop$ touch src/main/java/com/flower/MainApplicationStarter.java
~/Desktop/FlowerShop$ vim src/main/java/com/flower/MainApplicationStarter.java
- 1
- 2
MainApplicationStarter.java中的内容
package com.flower;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class MainApplicationStarter{ public static void main(String[] args) { SpringApplication.run(MainApplicationStarter.class, args); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
2.4创建Controller
创建HomeController.java
~/Desktop/FlowerShop$ touch src/main/java/com/flower/controller/HomeController.java
~/Desktop/FlowerShop$ vim src/main/java/com/flower/controller/HomeController.java
- 1
- 2
HomeController.java中的内容
package com.flower.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController{ @GetMapping("/home") public String getHomePage() { return "I Love you to come to my Flower Shop!"; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
2.5运行测试
~/Desktop/FlowerShop$ mvn spring-boot:run
- 1
SpringBoot项目创建成功!未对项目进行调整前,打出war包部署到Jetty9,启动会报错:
~/Documents/jetty-distribution-9.4.27.v20200227/bin$ ./jetty.sh start
...
2020-04-15 11:00:30.101:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@a859c5{FlowerShopDemo,/FlowerShopDemo,file:///tmp/jetty-0_0_0_0-8888-FlowerShopDemo_war-_FlowerShopDemo-any-8018217352624732370.dir/webapp/,UNAVAILABLE}{/home/kyun/Documents/jetty-distribution-9.4.27.v20200227/webapps/FlowerShopDemo.war}
java.util.ServiceConfigurationError: org.apache.juli.logging.Log: Provider org.eclipse.jetty.apache.jsp.JuliLog not a subtype
at java.util.ServiceLoader.fail(ServiceLoader.java:239)
at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:376)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at org.apache.juli.logging.LogFactory.<init>(LogFactory.java:91)
at org.apache.juli.logging.LogFactory.<clinit>(LogFactory.java:66)
at org.apache.tomcat.websocket.WsWebSocketContainer.<init>(WsWebSocketContainer.java:92)
...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
3.调整SpringBoot工程为打war包做准备
3.1修改pom.xml
org.apache.juli.logging.Log: Provider org.eclipse.jetty.apache.jsp.JuliLog not a subtype
:Jetty容器提供的JuliLog不是org.apache.juli.logging.Log的子类,这是由Jetty的加载机制造成的(直接部署到tomcat是没有这个问题的)。pom.xml作如下修改:
- spring-boot-starter-web依赖中将tomcat排除
- 引入spring-boot-starter-jetty依赖
- 注释掉 spring-boot-maven-plugin插件
<?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.flower</groupId> <artifactId>FlowerShop</artifactId> <version>1.0-SNAPSHOT</version> <!--指定打包类型pom, war, jar--> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
<!-- 引入Jetty依赖 -->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> </dependencies> <build>
<!--指定打包的名称:FlowerShopDemo.war或FlowerShopDemo.jar--> <finalName>FlowerShopDemo</finalName> <plugins> <!--<plugin>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-maven-plugin</artifactId>--> <!--</plugin>--> </plugins> </build>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
3.2修改启动类
因为我们要用外部web容器启动项目,所以启动类必须继承SpringBootServletInitializer,并重写configure方法。如果使用内嵌的web容器则不需要继承SpringBootServletInitializer。
package com.flower;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class MainApplicationStarter extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(MainApplicationStarter.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { // 注意这里要指向原先用main方法执行的Application启动类 return builder.sources(MainApplicationStarter.class); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
4.打包war部署到Jetty9
进行打包
~/Desktop/FlowerShop$ mvn clean package
- 1
将FlowerShop/target/xxx.war包复制到Jetty的webapps目录下,重启Jetty,在浏览器输入:
成功!
注意:
SpringBoot项目官方推荐部署方式是jar包运行方式,即nohup java -jar xxx.jar &
。
如果要用jar包方式部署,那么就要进行还原,但是不需要全部还原,只需添加回
spring-boot-maven-plugin插件即可:
<plugin>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
- 1
- 2
- 3
- 4
欢迎关注
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/105527061
- 点赞
- 收藏
- 关注作者
评论(0)