SpringBoot java程序打包

目標

打包後形成一個xxxx.tar.gz的壓縮包。解壓後,文件夾的名字爲程序的名字+版本號。文件夾內有一個可執行jar,springboot配置文件。部署在Linux服務器上可以使用springboot提供的啓動命令進行啓動:xxx.jar start|stop|run等等。

過程

在pom.xml文件中的build這一節中,引入springboot的打包插件和assembly插件。如下:

<build>
        <finalName>${artifactId}</finalName>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>make-zip</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/main/script/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>

spring-boot-maven-plugin插件是爲了打包成springboot的可執行jar。
maven-assembly-plugin插件是爲了後續的壓縮包文件的規整。

在項目中新建 src/main/script/assembly.xml 文件。內容如下:

<?xml version="1.0" encoding="utf-8"?>
<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>${project.version}</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <baseDirectory>${project.artifactId}-${project.version}</baseDirectory>
    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>application.properties</include>
                <!-- <include>log4j.xml</include> -->
            </includes>
            <fileMode>0644</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/script</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>auth.conf</include>
            </includes>
            <fileMode>0644</fileMode>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>auth.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>

    </fileSets>
</assembly>

這裏注意幾個點

  • pom文件裏
    • finalName 是打包後jar的名字,這裏我希望是不帶版本號的名字,因此配置的是 ${artifactId}
  • assembly文件中

    • id 是壓縮包中程序名後綴,例如:程序名是auth,壓縮包按照上述配置,就是auth-版本號.tar.gz;
    • baseDirectiory 中配置的是最終打包後文件夾的名字,這裏我期望文件夾上帶版本號,便於運維直接看到程序的版本號。
  • 剩下的就是配置文件的拷貝,這些提前在工程中做好。

效果如下:

  • 打包後的是左邊壓縮包,解壓後是右邊的文件夾

打包後解壓

  • 打開文件夾,如下:可執行jar,applicattion.properties是程序配置文件,auth.conf 是 jar配置文件(配置java啓動參數等)
    這裏配置文件都是springboot的基本知識,如果不太明白的,建議看springboot官方文檔。

打開文件夾

  • 運行一次之後,的效果,生成了用於存放pid的文件夾。

運行之後

好了,搞定。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章