Spring Boot 部署应用war

Spring Boot 部署应用war

1、配置pom.xml,增加packaging改成war。

2、将嵌入的Tomcat依赖方式改成provided,增加依赖项。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

3、对Main类进行修改,使其继承SpringBootServletInitializer类,并重载configure方法。

package com.idodo.demo;

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 DemoApplication extends SpringBootServletInitializer {

  @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }
  
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

4、右键pom.xml文件,选择Run As -> Maven install 打包。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project demo: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]

解决办法:将jdk\lib目录下的tools.jar,拷贝至jre目录C:\Program Files\Java\jre1.8.0_51\lib下。

5、执行成功。

[INFO] Installing D:\j2ee\workspace\demo\target\demo-0.0.1-SNAPSHOT.war to C:\Users\Administrator\.m2\repository\com\idodo\demo\0.0.1-SNAPSHOT\demo-0.0.1-SNAPSHOT.war
[INFO] Installing D:\j2ee\workspace\demo\pom.xml to C:\Users\Administrator\.m2\repository\com\idodo\demo\0.0.1-SNAPSHOT\demo-0.0.1-SNAPSHOT.pom
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 03:31 min
[INFO] Finished at: 2018-12-17T16:53:37+08:00
[INFO] ————————————————————————

6、将demo-0.0.1-SNAPSHOT.war重命名为demo.war,拷贝至D:\j2ee\apache-tomcat-8.5.34\webapps目录下。

7、启动bin\startup.bat。

8、访问http://localhost:8080/demo/hello?name=jzh

发表回复

您的电子邮箱地址不会被公开。