Spring Boot打包(Jar 与 War)

主要说明Springboot如何兼容jar包与war包的打包方式;

war依赖支持 依 在启动类包下配置环境参数

<profiles>
    <profile>
        <id>jarid>
        <activation>
            <activeByDefault>trueactiveByDefault>
        activation>
        <properties>
            <packaging.type>jarpackaging.type>
        properties>
    profile>
    <profile>
        <id>warid>
        <properties>
            <packaging.type>warpackaging.type>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-tomcatartifactId>
                <scope>providedscope>
            dependency>
        dependencies>
    profile>
profiles>

更改更 packaging,采用变量方式动态赋值

<packaging>${packaging.type}packaging>

maven-resources-plugin 插件去掉 插 include(项目自身决定)

若war包需要将静态资源放⼊⼯程包内部,请注意此处限制;

<resources>
    <resource>
        
        <directory>src/main/resourcesdirectory>
        
        
        <filtering>truefiltering>
    resource>
resources>

增加增 war插件


<plugins>
    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-war-pluginartifactId>
        <version>3.0.0version>
    plugin>
plugins>

启动类支持 启 war打包

在springboot启动类同级创建war支持类

public class Application extends SpringBootServletInitializer { 
  @Override 
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
    return builder.sources(App.class); 
  } 
}

App.class为springboot的启动类

打包

打jar包

mvn clean package -Dmaven.test.skip=true -P dev

打war包

mvn clean package -Dmaven.test.skip=true -P war,dev

你可能感兴趣的:(spring,boot,jar,java)