使用 maven-assembly-plugin 打包项目

此种方式可避免resource节点对compile阶段的影响,compile阶段会读取resource节点的信息但是不会读取assembly的配置文件

1. pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.skd</groupId>
    <artifactId>client</artifactId>
    <version>1.0</version>
    <name>client</name>
    <description>client for file monitor</description>

    <properties>
        <encoding>UTF-8</encoding>
        <maven-compiler-plugin-version>3.8.0</maven-compiler-plugin-version>
        <maven-jar-plugin-version>3.1.0</maven-jar-plugin-version>
        <maven-source-plugin-version>3.0.1</maven-source-plugin-version>
        <maven-assembly-plugin-version>3.1.0</maven-assembly-plugin-version>
        <maven-dependency-plugin-version>3.1.0</maven-dependency-plugin-version>
        <maven-resources-plugin-version>3.1.0</maven-resources-plugin-version>
    </properties>

    <dependencies>
        <!--spring boot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--log4j2-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!--http client-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.3</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <!--json-->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.2</version>
            <classifier>jdk15</classifier>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--configuration-->
        <!-- 通过资源文件注入属性配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <!-- 生成的项目压缩包的名字-->
        <finalName>client</finalName>
        <!--源代码路径-->
        <sourceDirectory>src/main/java</sourceDirectory>
        <!--maven-resources-plugin 插件打包resource文件时会参考此节点的配置-->

        <plugins>
            <!--编译插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin-version}</version>
                <configuration>
                    <encoding>${encoding}</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <!--将项目的源代码的class文件打包到一个jar包-->
            <!--jar包默认在target目录下-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${maven-jar-plugin-version}</version>
                <configuration>
                    <archive>
                        <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                        <addMavenDescriptor>true</addMavenDescriptor>
                        <manifest>
                            <!-- 是否要把第三方jar放到manifest的classpath中 -->
                            <addClasspath>true</addClasspath>
                            <!-- 生成的manifest中classpath的前缀,填写依赖jar包相对于项目jar包的路径-->
                            <!--我会把项目的jar包也打到lib目录下,所以这里使用当前目录-->
                            <classpathPrefix>./</classpathPrefix>
                            <!-- 应用的main class -->
                            <mainClass>com.skd.client.ClientApplication</mainClass>
                        </manifest>
                        <!--将资源文件目录添加到classpath中,打包后运行项目时则会在该目录下加载配置文件-->
                        <manifestEntries>
                            <!--填写配置文件相对于项目jar包的路径-->
                            <!--我的项目jar包在lib目录下,配置文件在和lib同级的conf目录下-->
                            <Class-Path>../conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--项目打包为jar包时排除这些文件,如果将配置文件打到jar包,则会优先读取jar包中的配置文件,不会读取conf目录下的配置文件-->
                    <!--注意这玩意从编译结果目录开始算目录结构-->
                    <excludes>
                        <exclude>/*.yaml</exclude>
                        <exclude>/*.yml</exclude>
                        <exclude>/*.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven-assembly-plugin-version}</version>

                <configuration>
                    <!--jar包名字是否在finalName后追加AssemblyId-->
                    <appendAssemblyId>true</appendAssemblyId>
                    <descriptors>
                        <!--xml文件中配置了打包的相关配置-->
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <!--名字任意-->
                        <id>make-assembly</id>
                        <!-- 绑定到package生命周期阶段上 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 只运行一次 -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

</project>

2. assembly的xml配置文件

<assembly >
    <!--此处是打包名称的设置,最后是会生成一个finalName-id.format 文件在目录 target下-->
    <!--appendAssemblyId为false时则压缩包名称不追加id,默认下追加-->
    <id>assembly</id>
    <!-- 最终打包成一个用于发布的zip文件 -->
    <formats>
        <format>tar.gz</format>
    </formats>

    <dependencySets>
        <dependencySet>
            <!--是否将项目jar包打包到指定目录-->
            <useProjectArtifact>true</useProjectArtifact>
            <!--将依赖jar包打到lib目录-->
            <outputDirectory>lib</outputDirectory>
            <!--将依赖jar包不解压直接打包到目录-->
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <!--通过fileSet节点可以将制定目录的指定文件打包到压缩文件的制定目录-->
        <fileSet>
            <!-- 把项目的配置文件,打包到压缩文件的conf目录 -->
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <!--打包脚本文件到根目录-->
        <fileSet>
            <directory>${project.basedir}/src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>

    </fileSets>
</assembly>

3. 打包后的 target 目录结构

解压后目录结构如下:

 项目jar包在lib目中中

4. 运行项目

启动脚本

执行启动脚本

原文地址:https://www.cnblogs.com/virgosnail/p/10139225.html