zoukankan      html  css  js  c++  java
  • Spring Boot使用Maven自定义打包方式

    前言:本文将告诉你如何将程序Jar与与依赖Jar及配置文件分离打包,以下列举了两种不同Maven打包方式,其打包效果一致!

    一、第一种Maven打包方式,将jar及resources下全部配置文件,拷贝到指定目录:

    <!--配置项-->
    <properties>
    <!--自定义配置-->
    <project.jar.output.directory>E:/IDEAFile/file-copy/target/project</project.jar.output.directory>
    </properties>
    <build>
            <plugins>
                <!--项目依赖的jar文件,放置默认配置目录下-->
               <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
                <!-- 设置jar的入口类 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.example.filecopy.FileCopyApplication</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
    
            <!-- 使用maven-resources-plugin插件复制resources目录下所有文件到指定的路径-->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/project</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>src/main/resources</directory>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <!--使用maven-antrun-plugin插件将jar复制到指定的目录下-->
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <!-- 在maven进行package的时候执行-->
                            <phase>package</phase>
                            <configuration>
                                <tasks>
                                    <!--todir:是将要复制jar包到的地方,overwrite:是否重写-->
                                    <copy todir="${project.jar.output.directory}" overwrite="true">
                                        <!--获取父目录下的target文件夹中的jar-->
                                        <fileset dir="${project.build.directory}">
                                            <include name="*.jar"/>
                                        </fileset>
                                    </copy>
                                </tasks>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

    第二种Maven打包方式使用 assembly插件,将jar及配置文件进行压缩打包到指定目录:

    <plugins>
              <!-- 项目依赖的jar文件,放置默认配置目录下-->
               <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
                <!-- 设置jar的入口类-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.example.filecopy.FileCopyApplication</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
    
                 <!--assembly插件-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <!--指定压缩包名称-->
                        <finalName>project</finalName>
                        <!--指定assembly配置文件配置-->
                        <descriptors>
                            <descriptor>/assembly/assembly.xml</descriptor>
                        </descriptors>
                        <!--打包tar.gz输出target文件夹中-->
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
          </plugins>

    assembly文件:

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
        <id>leaves</id>
        <formats>
            <!--压缩文件形式 可选 zip tar.gz等 -->
            <format>zip</format>
        </formats>
        <includeBaseDirectory>true</includeBaseDirectory>
    
        <!-- 项目文件处理 -->
        <fileSets>
            <!--配置文件输出位置根目录文件夹下-->
            <fileSet>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**</include>
                </includes>
                <filtered>true</filtered>
                <outputDirectory>${file.separator}</outputDirectory>
            </fileSet>
    
            <!-- 项目代码生成的jar文件放在根目录  -->
            <fileSet>
                <directory>${project.build.directory}</directory>
                <outputDirectory>${file.separator}</outputDirectory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </fileSet>
        </fileSets>
    </assembly>

    个人总结:

    我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!

  • 相关阅读:
    tensorflow2中pydot问题
    tensorflow2的差异总结
    tensorflow2.0中引入keras和原来的keras的差异
    Linux 后台任务进程管理工具supervisor的使用
    【Go学习】go 原生库net/http发送 GET POST 请求
    正则校验crontab格式
    【FastAPI 学习十二】定时任务篇
    【FastAPI 学习 十一】 项目目录结构demo(自己改版)
    【FastAPI 学习 十】使用Redis
    【FastAPI 学习 九】图片文件上传
  • 原文地址:https://www.cnblogs.com/bgyb/p/14203876.html
Copyright © 2011-2022 走看看