作者:Eddy  历史版本:1  最后编辑:Eddy  更新时间:2024-11-15 09:05

编写版本:v3.5.5
适用版本:v3.5.1+

如何使用spring-boot打包插件构建fatjar部署包

修改springboot的pom.xml文件

找到ibps-provider-root(服务提供层)下modules/provider-boot的pom.xml文件。
其他服务修改是一样的。

  • 将该代码块的注释去掉
            <!-- <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin> -->
  • 删除以下代码块的内容
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <property name="classes.dir" value="${project.build.directory}/classes-exclude"/>
                                <property name="lib.dir" value="${project.build.directory}/lib-exclude"/>

                                <copy todir="${classes.dir}" includeEmptyDirs="false">
                                    <fileset dir="${project.build.directory}/classes">
                                    </fileset>
                                </copy>
                                <copy todir="${lib.dir}">
                                    <fileset dir="${project.build.directory}/lib"/>
                                </copy>

                                <unjar dest="${classes.dir}" overwrite="false">
                                    <patternset>
                                        <include name="**/appd"/>
                                        <include name="**/*.sh"/>
                                        <include name="**/*.bat"/>
                                        <include name="**/readMe"/>

                                        <include name="**/*.xsd"/>
                                        <include name="**/*.xml"/>
                                        <include name="**/provider"/>
                                        <include name="**/*.sql"/>
                                        <include name="**/*.ftl"/>
                                        <include name="**/*.yml"/>
                                        <include name="**/*.txt"/>
                                        <include name="**/*.conf"/>
                                        <include name="**/*.properties"/>
                                        <include name="**/*.setting"/>
                                        <include name="**/*.env"/>
                                        <include name="**/*.json"/>
                                        <include name="**/*.jks"/>

                                        <exclude name="**/*.map.xml"/>
                                        <exclude name="org/activiti/db/mapping/**/*.xml"/>
                                    </patternset>
                                    <fileset dir="${lib.dir}">
                                        <include name="lc-*.jar"/>
                                        <include name="ibps-*.jar"/>
                                    </fileset>
                                </unjar>

                                <!--
                                <delete includeEmptyDirs="true">
                                      <fileset dir="${lib.dir}">
                                        <include name="lc-*.jar"/>
                                        <include name="ibps-*.jar"/>
                                      </fileset>
                                </delete>
                                -->

                                <property name="jarname" value="boot.jar"/>
                                <property name="jarfile.exclude.basedir" value="${project.build.directory}/${project.artifactId}-exclude"/>
                                <property name="jarfile.exclude.name" value="${jarfile.exclude.basedir}/${jarname}"/>

                                <copy todir="${jarfile.exclude.basedir}" includeEmptyDirs="false">
                                    <fileset dir="${classes.dir}">
                                        <exclude name="**/*.class" />
                                    </fileset>
                                </copy>
                                <copy todir="${jarfile.exclude.basedir}/lib">
                                    <fileset dir="${lib.dir}"/>
                                </copy>
                                <pathconvert property="exclude.lib" pathsep=" ">
                                    <mapper>
                                        <chainedmapper>
                                            <flattenmapper />
                                            <globmapper from="*" to="lib/*" />
                                        </chainedmapper>
                                    </mapper>
                                    <fileset dir="${jarfile.exclude.basedir}/lib">
                                        <include name="*.jar" />
                                    </fileset>
                                </pathconvert>
                                <jar destfile="${jarfile.exclude.name}">
                                    <manifest>
                                          <attribute name="Class-Path" value=". ${exclude.lib}"/>
                                          <attribute name="Main-Class" value="com.lc.ibps.BootApplication"/>
                                    </manifest>
                                    <fileset dir="${classes.dir}">
                                        <include name="**/*.class" />
                                    </fileset>
                                </jar>

                                <delete includeEmptyDirs="true">
                                      <fileset dir="${classes.dir}"/>
                                      <fileset dir="${lib.dir}"/>
                                      <fileset dir="${project.build.directory}/lib"/>
                                </delete>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

Maven打包

在项目根目录下(即包含pom.xml的目录),在命令行里输入: mvn clean package即可, 等待打包完成,出现[INFO] BUILD SUCCESS即为打包成功。jar包在target文件夹中。

  • 打包中

  • 打包完成

  • 查看jar包

运行jar包

使用java -jar xxx.jar
举例:
进入jar包所在target文件夹。
此处powershell执行命令为: java -jar ibps-provider-boot-3.5.5-SNAPSHOT.jar

jar包启动环境变量配置问题

命令带有环境变量启动
找到配置文件的环境变量。
以下以Redis地址为例子:


其变量名为SPRING_REDIS_HOST,其命令如下:

  • 方案一(-DpropName=propValue):
    java -jar -DSPRING_REDIS_HOST="192.168.3.210" -DSPRING_REDIS_PORT="6379" ibps-provider-boot-3.5.5-SNAPSHOT.jar
  • 方案二(使用springboot的方式,–propName=propValue方式):
    java -jar ibps-provider-boot-3.5.5-SNAPSHOT.jar --spring.redis.host="192.168.3.210" --spring.redis.port="6379"