补习系列-springboot-使用assembly进行项目打包

简介: 目录springboot-maven插件1. 项目打包Jar2. 项目完整构建3. 本地包依赖参考文档springboot-maven插件springboot-maven插件repackage目标声明Requires a Maven project to be executed.

目录

springboot-maven插件

springboot-maven插件

repackage目标声明


Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: compile+runtime.
Since version: 1.1.
Binds by default to the lifecycle phase: package.

1. 项目打包Jar

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

如此,执行mvn package可自动生成一个独立可执行的jar文件

2. 项目完整构建

通常,项目发布时除了jar包,还会包含配置文件、启停脚本等,此时需要借助assembly插件完成重复打包
构建结构

base
    - bin
        - start.sh
        - stop.sh
    - application.properties
    - log4j.properties
    - app-0.0.1-SNAPSHOT.jar

pom定义

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>bundle</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>${basedir}/src/main/build/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

说明
将assembly定义在spring-boot:repackage之后,这样maven在执行package阶段时会按照声明顺序处理;
assembly.xml存放于src/main/build目录,此外用于发布的config、bin目录及文件都放到这个目录;

assembly定义

<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>bundle</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory> <!-- disable the creation of root's distribution dir in the archive -->
    
     <fileSets>  
        <!-- config files -->
        <fileSet>  
            <directory>${basedir}/src/main/build/config</directory>  
            <excludes></excludes>  
             <includes>
                <include>application*.properties</include>
                <include>log4j.properties</include> 
            </includes>
            <fileMode>0644</fileMode>
            <outputDirectory>/</outputDirectory>  
        </fileSet>  
        <!-- scripts -->
        <fileSet>
            <directory>${basedir}/src/main/build/bin</directory>
            <includes>
                <include>*.sh</include>
            </includes>
            <fileMode>0755</fileMode>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <!-- executable jar -->
         <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.artifactId}-${project.version}.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>  
    
</assembly>

关于内置变量

3. 本地包依赖

  • 定义scope=system依赖
<dependency>
   <groupId>com.xxx.component</groupId>
   <artifactId>mongoop</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <scope>system</scope>
   <systemPath>D:\work\maven\repo\m2\xxx.jar</systemPath>
  </dependency>
  • 声明springboot打包时包含system范围的依赖
<build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
     <includeSystemScope>true</includeSystemScope>
    </configuration>
    <executions>
     <execution>
      <goals>
       <goal>repackage</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>

参考文档

springboot文档-maven插件使用
关于springboot-repackage
maven内置变量

img_9b09a36f6de95886f52ce82fa1e89c88.jpe

作者: zale

出处: http://www.cnblogs.com/littleatp/, 如果喜欢我的文章,请关注我的公众号

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文链接  如有问题, 可留言咨询.

目录
相关文章
|
3月前
|
Java 开发工具 Maven
springboot项目打包为sdk供其他项目引用
springboot项目打包为sdk供其他项目引用
|
4月前
|
Java 测试技术 Maven
Spring Boot项目打包配置详解
Spring Boot项目打包配置详解
89 0
|
5月前
|
Java
dockerfile打包阿里云镜像 springboot实战项目(阿里云镜像仓失败 处理)
dockerfile打包阿里云镜像 springboot实战项目(阿里云镜像仓失败 处理)
|
2月前
|
SQL 监控 Java
nacos常见问题之dubbo+nacos+springboot3的native打包成功后运行出现异常如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
43 2
|
2月前
|
jenkins Java 持续交付
详解如何使用Jenkins一键打包部署SpringBoot项目
详解如何使用Jenkins一键打包部署SpringBoot项目
100 0
|
3月前
|
Java 应用服务中间件 Maven
SpringBoot分离资源文件打包
SpringBoot分离资源文件打包
|
3月前
|
JSON Java Maven
SpringBoot使用git-commit-id-maven-plugin打包
【2月更文挑战第1天】 git-commit-id-maven-plugin 是一个maven 插件,用来在打包的时候将git-commit 信息打进jar中。 这样做的好处是可以将发布的某版本和对应的代码关联起来,方便查阅和线上项目的维护。至于它的作用,用官方说法,这个功能对于大型分布式项目来说是无价的。
90 0
|
4月前
|
Java Maven Docker
SpringBoot项目打包部署到阿里云服务器、通过Maven插件制作Docker镜像、部署项目容器、配置生产环境
SpringBoot项目打包部署到阿里云服务器、通过Maven插件制作Docker镜像、部署项目容器、配置生产环境
139 0
|
4月前
|
Java
springboot项目打包瘦身
springboot项目打包瘦身