Maven 打包(资源文件和 jar 包分离)

前言

SpringBoot 项目默认将配置文件和代码打包到一起,但这样就失去了使用配置文件的作用,所以需要将配置文件和代码分开,可以使用

maven-assembly-plugin 插件打包。

效果

假设模块有 dao、service 和 controller,打包在 controller 的 target 下,压缩包包名为 controller-web.zip,解压后目录为 controller,目录树:

.
|-- bin
|   `-- start.sh
|   `-- README.md
|-- conf
|   `-- application.yml
|-- lib
|   |-- dao.jar
|   |-- service.jar
`-- controller.jar 

使用

在 controller 模块 pom.xml 添加如下:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
              	<!--编译需要的文件-->
                <include>mapper/**/*.xml</include>
                <include>*.yml</include>
                <include>*.json</include>
                <include>*.xml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
        		<!-- 当前模块的打包配置 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <excludes>
                		<!-- 去掉 controller 模块的不需要打进 jar 的配置文件 -->
                    <exclude>**/*.properties</exclude>
                    <exclude>*.yml</exclude>
                    <exclude>package.xml</exclude>
                </excludes>
                <archive>
                    <manifest>
                        <mainClass>com.example.controller.ControllerApplication</mainClass>
                        <!-- 最终所有 jar 都打包到压缩包解压后的 lib 下,所以添加 classpath 的前缀 -->
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
        		<!-- 该插件将工程打包成 zip 或者 tar.gz 等格式压缩包 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <descriptors>
                		<!-- 按照 package.xml 打包 -->
                    <descriptor>src/main/resources/package.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- controller 模块打包后的名字 -->
    <finalName>${project.artifactId}</finalName>
</build>

package.xml 的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>web</id>
    <formats>
      	<!-- 压缩格式 -->
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
          	<!-- 资源文件位置 -->
            <directory>${project.basedir}/src/main/resources</directory>
          	<!-- 输出的位置 -->
            <outputDirectory>conf</outputDirectory>
          	<!-- 导入的资源文件 -->
            <includes>
                <include>**/*.properties</include>
                <include>*.yml</include>
            </includes>
          	<!-- 不导入的资源文件 -->
            <excludes>
                <exclude>package.xml</exclude>
                <exclude>mapper/**/*.xml</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <!-- ../ 就是上一级目录 -->
            <directory>../bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.sh</include>
                <include>*.md</include>
                <include>*.txt</include>
            </includes>
          	<!-- 文件权限 -->
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
    <files>
        <file>
          	<!-- 把启动类所在的 jar 包放到压缩包的根目录下 -->
            <source>target/${project.artifactId}.jar</source>
            <outputDirectory>.</outputDirectory>
        </file>
    </files>
    <dependencySets>
        <dependencySet>
          	<!-- 没懂,默认为 true -->
            <useProjectArtifact>false</useProjectArtifact>
          	<!-- 依赖 jar 输出路径 -->
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

最后 idea 右上角 maven里 父工程 package 就好了(不知道对不对......)。

参考

maven-assembly-plugin 官方文档

maven工程打包jar以及java jar命令的classpath使用

Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境

posted @ 2020-12-26 21:57  hligy  阅读(1395)  评论(4编辑  收藏  举报