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環境


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM