天天看點

maven-assembly-plugin打包-springboot 配置打在包外 動态配置

pom.xml

<build>
    <plugins>
        <!-- 使用spring boot的maven插件進行打包 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- 是否打出可執行的jar包(僅支援Linux格式) -->
                <executable>false</executable>
            </configuration>
        </plugin>
        <!-- 将jar包和外部配置等檔案整體打包(zip,tar,tar.gz等) -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <!--jar包名稱-->
                <finalName>${project.artifactId}-${project.version}</finalName>
            </configuration>
            <executions>
                <execution>
                    <id>full</id>
                    <!-- 綁定到package生命周期階段上 -->
                    <phase>package</phase>
                    <goals>
                        <!-- 隻運作一次 -->
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <!--描述檔案路徑-->
                        <descriptor>${basedir}/assembly/assembly.xml</descriptor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
           

配置比較多而且比較固定,是以可以在parent工程中聲明,在要打包的項目中引用。

assembly.xml 配置

<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會跟在打封包件後面,現還不清楚怎麼取消-->
    <id>jar</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <!--打包時把/resources/config 目錄内的檔案放在jar包外的/目錄下-->
            <directory>${project.build.directory}/classes/config</directory>
            <outputDirectory>/</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
    <files>
        <!--打包時把README.md檔案放在jar包外-->
        <file>
            <source>README.md</source>
            <outputDirectory></outputDirectory>
        </file>
        <file>
            <!--打好的jar包名稱和放置目錄-->
            <source>${project.build.directory}/${project.build.finalName}.jar</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>