zoukankan      html  css  js  c++  java
  • spring boot 打jar包分离lib和resources

    为什么要配置、依赖文件分离:

    1.在传统jar包中,所有文件都打包到一个jar包中,jar非常臃肿,在进行服务更新部署时非常不便,而且传输不稳定时导致传输失败。如果过实行文件分离、在依赖不改变的情况下,仅仅上传更新后的 编译文件是非常xxxxxxxxxxxxxxxxxxxxxxxxxxxx方便的。

    1. 如果要修改一些配置文件:properties、xml,静态文件等可以直接在服务器上编辑。

    那么怎么实行配置、依赖文件分离呢?

    插件介绍

    1. maven-jar-plugin 这个插件式专门用来打包用的,可以配置需要打包进去的文件,程序的入口类等。
    2. maven-resources-plugin 这个插件是用来拷贝资源文件的。
    3. maven-maven-dependency-plugin 这个插件是用来拷贝依赖库的。
    4. maven-assembly-plugin 可以说包含了以上插件的功能,但是可以做到更精细的控制。
    5. spring-boot-maven-plugin 这个不用说,springboot 项目最重要的插件,整个项目的打包处理过程还是要依附于它。

    打包成可执行jar,不仅仅局限SpringBoot项目(主入口函数存在)

    maven-jar-plugin 插件打包jar

    在pom文件中配置,但是这样 依赖的jar并不会打进来(后面会有解决方法),适用不需要依赖文件的项目。

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.3</version>
    
    
                    <configuration>
                        <archive>
                            <manifest>
    
                                <!--是否添加依赖-->
                                <addClasspath>true</addClasspath>
    
                                <!--设置启动类-->
                                <mainClass>xxx.xxx.Main</mainClass>
    
                            </manifest>
                        </archive>
                        <!--设置生成jar输出位置-->
                        <outputDirectory>${project.build.directory}</outputDirectory>
    
                    </configuration>
                </plugin> 
    

    maven-assembly-plugin 插件打包jar

                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
    
                        <!--不添加AssemblyId-->
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptorRefs>
                            <!--配置打包的时候一并打包依赖jar-->
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <!--入口类-->
                                <mainClass>xxx.xxx.Main</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <!--绑定生命周期-->
                            <phase>package</phase>
                            <goals>
                                <!--执行assembly -->
                                <goal>assembly</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    打包SpringBoot 项目

    方案一、

    <plugins>
            <!--打包jar-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                   
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--jar包不包含唯一版本标识-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定入口类-->
                            <mainClass>xxx.xxx.Application</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                            <Class-Path>/resources</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}/dis</outputDirectory>
                </configuration>
            </plugin>
    
            <!--拷贝依赖 copy-dependencies-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                    ${project.build.directory}/dis/lib/
                                </outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <!--拷贝资源文件 copy-resources-->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <resources>
                                    <resource>
                                        <directory>src/main/resources</directory>
                                    </resource>
                                </resources>
                                <outputDirectory>${project.build.directory}/dis/resources</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                     
                        <layout>ZIP</layout>
                        <!--使用外部配置文件,jar包里没有资源文件-->
                        <addResources>true</addResources>
                   
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
            </plugins>
        </build>
    

    方案二

    这里依赖assembly.xml 描述文件

                    <plugins>
                        <plugin>
                            <artifactId>maven-assembly-plugin</artifactId>
                            <configuration>
                                <appendAssemblyId>false</appendAssemblyId>
                                <descriptors>
                                    <descriptor>assembly.xml</descriptor>
                                </descriptors>
                                <outputDirectory>${project.build.directory}/dist/</outputDirectory>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>make-assembly</id>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>single</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
    
                        <!-- 打包成jar文件,并指定lib文件夹以及resources资源文件夹 -->
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-jar-plugin</artifactId>
                            <configuration>
                                <archive>
                                    <manifest>
                                        <mainClass>xxx.xxx.Application</mainClass>
                                        <!--依赖前缀-->
                                        <classpathPrefix>lib/</classpathPrefix>
                                        <addClasspath>true</addClasspath>
                                    </manifest>
                                    <manifestEntries>
                                        <Class-Path>resources/</Class-Path>
                                    </manifestEntries>
                                </archive>
                            </configuration>
                        </plugin>
                    </plugins>
    

    assembly.xml

    <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>distribution</id>
    
        <!--输出格式 zip 最终结果生成zip -->
        <formats>
            <format>zip</format>
        </formats>
    
        <includeBaseDirectory>false</includeBaseDirectory>
    
        <!--设置需要输出文件-->
        <fileSets>
            <fileSet>
                <directory>src/main/resources/</directory>
                <outputDirectory>/resources</outputDirectory>
            </fileSet>
        
        </fileSets>
    
        <dependencySets>
            <dependencySet>
                <!--依赖包的输出目录-->
                <outputDirectory>/lib</outputDirectory>
                <scope>runtime</scope>
                <excludes>
                    <exclude>${project.groupId}:${project.artifactId}</exclude>
                </excludes>
            </dependencySet>
            <dependencySet>
                <!--jar包的输出目录-->
                <outputDirectory>/</outputDirectory>
                <includes>
                    <include>${project.groupId}:${project.artifactId}</include>
                </includes>
            </dependencySet>
        </dependencySets>
    </assembly>
    

     

  • 相关阅读:
    Oracle中job的使用详解
    Control File (二)重建CONTROLFILE --- NORESETLOG
    Oracle Analyze 命令 详解
    深入学习Oracle分区表及分区索引
    B树索引和位图索引的区别!
    RAID0_RAID1_RAID10_RAID5各需几块盘才可组建
    Oracle IO优化心得
    修改dbwr后台进程数量
    查看ORACLE执行计划的几种常用方法
    printf()格式化输出详解及echo带颜色输出
  • 原文地址:https://www.cnblogs.com/aligege/p/12896022.html
Copyright © 2011-2022 走看看