SpringBoot打成war包

修改pom.xml添加一下代码


<packaging>warpackaging>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-tomcatartifactId>
    <version>providedversion>
dependency>

<build>
	<finalName>${project.artifactId}finalName>
	<plugins>
		<plugin>
		    <groupId>org.apache.maven.pluginsgroupId>
		    <artifactId>maven-war-pluginartifactId>
		    <configuration>
		        <failOnMissingWebXml>falsefailOnMissingWebXml>
		    configuration>
		plugin>
	plugins>
	
	
build>

启动类继承SpringBootServletInitializer类

# 如果要打jar包,需要将继承类给去掉
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

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

现在就可以运行jar和war包了

进入项目根目录,执行打包命令,打成war包

mvn clean package

将生产的war包复制到tomcat的webapps目录下,重启tomcat

访问地址

注意:如果war放在webapps下,访问的时候需要加项目路径,如果放在ROOT目录下就不用加项目路径

你可能感兴趣的:(spring,boot,maven,tomcat)