亲宝软件园·资讯

展开

Spring boot jar Spring boot打包jar分离lib和resources方法实例

人气:0

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

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

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

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

插件介绍

打包成可执行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>

加载全部内容

相关教程
猜你喜欢
用户评论