美文网首页
Springboot同时出Jar和War

Springboot同时出Jar和War

作者: 木脑 | 来源:发表于2019-09-27 16:50 被阅读0次

    修改maven配置
    为同时支持war / jar部署,主程序的pom.xml需作如下修改,该配置默认以jar方式打包;以war方式打包须使用命令 mvn package -Pwar

    请移除原有maven-jar-plugin,并保证在项目里有默认application.properties文件(配置可使用本地配置)

    <packaging>${packaging.type}</packaging> 
    ...
    <profiles>
            <profile>
                <id>jar</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <packaging.type>jar</packaging.type>
                </properties>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-web</artifactId>
                        <exclusions>
                            <exclusion>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-starter-tomcat</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-starter-logging</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                        <scope>compile</scope>
                    </dependency>
                </dependencies>
                <build>
                    <finalName>项目名</finalName>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.3</version>
                            <configuration>
                                <source>1.8</source>
                                <target>1.8</target>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-maven-plugin</artifactId>
                            <configuration>
                                <finalName>${project.name}</finalName>
                                <mainClass>入口类</mainClass>
                                <layout>ZIP</layout>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>repackage</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-jar-plugin</artifactId>
                            <version>2.4</version>
                            <configuration>
                            <excludes>
                                <exclude>**/application.yml</exclude>
                            </excludes>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
            <profile>
                <id>war</id>
                <properties>
                    <packaging.type>war</packaging.type>
                </properties>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-web</artifactId>
                        <exclusions>
                            <exclusion>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-starter-tomcat</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-starter-logging</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                        <scope>provided</scope>
                    </dependency>
                </dependencies>
                <build>
                    <finalName>项目名</finalName>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.3</version>
                            <configuration>
                                <source>1.8</source>
                                <target>1.8</target>
                            </configuration>
                        </plugin>
                        <plugin>
                            <artifactId>maven-war-plugin</artifactId>
                            <configuration>
                                <failOnMissingWebXml>false</failOnMissingWebXml>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
     
    

    修改启动类
    启动类须继承SpringBootServletInitializer

    @SpringBootApplication
    public class 入口Application extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(UserServiceApplication.class);
        }
     
        public static void main(String[] args) {
            SpringApplication.run(入口Application.class, args);
        }
    }
    

    相关文章

      网友评论

          本文标题:Springboot同时出Jar和War

          本文链接:https://www.haomeiwen.com/subject/mecryctx.html