Maven插件 maven-assembly-plugin

maven-assembly-plugin 插件可以将项目打包成zip压缩文件格式 发布应用时直接解压压缩包部署,启动时可以通过脚本的方式运行,优点:依赖和配置文件可直接编辑修改或替换。

Maven 配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <finalName>${project.artifactId}-${project.version}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/assembly/assembly-descriptor.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

插件除pom配置外还需添加descriptor描述文件(示例路径src/assembly/assembly-descriptor.xml)可以通过<fileSet>添加打包时的内容

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>assembly</id>
<formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
    <!-- scripts  -->
    <fileSet>
        <directory>src/main/scripts</directory>
        <outputDirectory>scripts</outputDirectory>
        <includes>
            <include>*.sh</include>
        </includes>
        <fileMode>0755</fileMode>
        <lineEnding>unix</lineEnding>
    </fileSet>
    <fileSet>
        <directory>src/main/config</directory>
        <outputDirectory>config</outputDirectory>
        <lineEnding>unix</lineEnding>
    </fileSet>
    <!--  classes  -->
    <fileSet>
        <directory>${project.build.directory}/classes</directory>
        <outputDirectory>classes</outputDirectory>
    </fileSet>
</fileSets>
<!--  依赖jar包  -->
<dependencySets>
    <dependencySet>
        <outputDirectory>lib</outputDirectory>
        <excludes>
            <!--  排除依赖  -->
            <exclude>com.owothink:xxxxx</exclude>
        </excludes>
    </dependencySet>
</dependencySets>
</assembly>