使用spring boot建立fat jar APP

介绍

在好久很好久之前,咱们部署web程序的方式是怎么样的呢?配置好服务器,将本身写的应用程序打包成war包,扔进服务器中指定的目录里面。固然免不了要配置一些负责的xml和自定义一些servlet。java

如今有了spring boot,一切都变了,咱们能够将web应用程序打包成fat jar包,直接运行就好了。git

本文将会关注于怎么使用Spring Boot建立一个fat jar包。github

全部你须要作的就是添加以下依赖:web

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

build和run

有了上面的配置,只须要使用spring

mvn clean install

就能够生成相应的jar包了。 shell

若是要运行它,使用:tomcat

java -jar <artifact-name>

便可。很是简洁。springboot

若是你要在服务器上面永久运行该服务,即便登陆的用户退出服务器,则可使用nohup命令:ruby

nohup java -jar <artifact-name>

fat jar和 fat war

在上面的例子中,全部的依赖jar包都会被打包进入这一个fat jar中,若是你使用了tomcat,那么tomcat也会被打包进去。服务器

但有时候咱们仍是须要打包成war包,部署在服务器中,这种状况只须要将pom.xml中的packaging属性修改成war便可。

更多配置

大多状况下,咱们不须要额外的配置,若是咱们有多个main class,咱们须要指定具体的哪一个类:

<properties>
        <start-class>com.flydean.FatJarApp</start-class>
    </properties>

若是你没有从spring-boot-starter-parent继承,那么你须要将main class添加到maven plugin中:

<plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.flydean.FatJarApp</mainClass>
                    <layout>ZIP</layout>
                </configuration>
            </plugin>
        </plugins>

有些状况下,你须要告诉maven来unpack一些依赖:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <requiresUnpack>
            <dependency>
                <groupId>org.jruby</groupId>
                <artifactId>jruby-complete</artifactId>
            </dependency>
        </requiresUnpack>
    </configuration>
</plugin>

本文的代码请参考https://github.com/ddean2009/learn-springboot2/tree/master/springboot-fatjar

更多教程请参考 flydean的博客

相关文章
相关标签/搜索