Spring Boot 部署到tomcat
Spring Boot 的jar转war
修改启动类
- 默认的启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* @SpringBootConfiguration :sprring boot配置类
* @Configuration : spring配置类
* @Component :有此注解代表是一个spring组件
* @EnableAutoConfiguration 自动配置
* @AutoConfigurationPackage 自动配置包
* @Import({Registrar.class}) 自动配置 ‘包注册’
* @Import({AutoConfigurationImportSelector.class}) 自动配置导入选择
* getAutoConfigurationEntry方法下的
* List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes); 获取所有的配置
* SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
* return EnableAutoConfiguration.class; 加载配置
* 配置的核心文件 META-INF/spring.factories.(spring-boot-autoconfigure)
*/
@SpringBootApplication()
// @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) //去掉springboot 默认的数据源配置
public class ElasticsearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticsearchApplication.class, args);
}
}
- 实现SpringBootServletInitializer,重写configure()方法
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ElasticsearchApplication.class);
}
}
修改pox文件
- 修改打包方式(修改
packaging
属性)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.merit</groupId>
<artifactId>elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <!-- 将 jar修改为 war -->
<name>Elasticsearch</name>
<description>Demo project for Spring Boot</description>
IDEA Tomcat部署
去除冲突
- 移除
spring-boot-starter-web
的tomca包(如果添加了spring-boot-starter-tomcat
的依赖请注掉)
<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>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>-->
- 打包名字修改
<build>
<!--打包后的名字-->
<finalName>Elasticsearch</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 在工作中,很多情况下我们打包是不想执行测试用例的可能是测试用例不完事,或是测试用例会影响数据库数据跳过测试用例执 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!--跳过项目运行测试用例-->
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
<!--自定义mapper时需要添加添加的-->
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
运行配置
-
选择启动配置,添加Tomcat
-
配置deployment,选择项目对应的war
- Application context 为上下文路径,一般会默认追加到访问路径处,URL为项目的根路径
- 选择maven清除缓存,点击install(install其实是包含clear命令的)进行打包,选择tomcat点击运行即可
- 访问地址为tomcat上显示的地址,idea启动时会自动打开浏览器跳转到对应地址的
单独Tomcat部署
在maven选择install后会在target文件夹下生成对应的war,将war包复制到tomcat的webapper下即可