Spring Boot打包方式

 2019-11-02 21:06  阅读(1034)
文章分类:Spring boot

1 部署到外部Tomcat中

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.user</groupId>
        <artifactId>zjsz-user</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>

        <dependencies>
            <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>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
            </dependency>

        </dependencies>

        <build>
            <plugins>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <warName>user</warName>  <!--外部访问到项目名 http://localhost:8080/user/.....-->
                    </configuration>
                </plugin>
            </plugins>

            <!--将xml打包进去-->
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                </resource>
            </resources>
        </build>

    </project>

然后在修改一下spring boot入口,继承重新SpringBootServletInitializer 里面打configure方法

@SpringBootApplication
    public class ZjszUserApplication extends SpringBootServletInitializer {

        @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(ZjszUserApplication.class); }

        public static void main(String[] args) throws Exception {
            SpringApplication.run(ZjszUserApplication.class, args);
        }

    }

都添加好后,打一个war出来,然后放到Tomcat中去。启动完成。。

20191102100554\_1.png

2 已jar包方式启动

<packaging>jar</packaging>
    .......................
    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>

                <!-- maven打包跳过测试类-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.21.0</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>

            </plugins>

            <!--将xml打包进去--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources>
        </build>

最下面打resources最好都加一下,因为有的时候打出来都jar包并不包含resources这个文件夹,会导致启动找不到对应都配置信息。

打包完成后,java -jar xxxxx.jar 启动。效果如下

20191102100554\_2.png


来源:http://ddrv.cn

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> Spring Boot打包方式

相关推荐