Spring Boot打包方式

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】

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) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Spring Boot打包方式

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏