欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

maven使用assembly插件打zip包

程序员文章站 2024-03-20 20:13:16
...

一,pom引入插件

<build>
<plugins>
        <!-- 使用assembly插件打成zip -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
            <!-- 指定assembly配置文件路径 -->
                <descriptor>src/assembly/assembly.xml</descriptor>
                <finalName>demo</finalName>
            </configuration>
            <executions>
                <execution>
                    <!-- 在执行package打包时,执行assembly:single -->
                    <phase>package</phase>
                    <goals>
         <!-- 在执行assembly一次 -->
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

二,pom定义zip包日期后缀

<properties>
    <!-- 文件拷贝时的编码 -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!-- 编译时的编码 -->
    <maven.compiler.encoding>UTF8</maven.compiler.encoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
    <!-- maven获取日期 -->
    <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
    <timestamp>${maven.build.timestamp}</timestamp>
</properties>

三,创建描述文件

目录src/assembly/assembly.xml

<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>${timestamp}</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}${file.separator}classes</directory>
            <outputDirectory>${file.separator}WEB-INF${file.separator}classes</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}${file.separator}src${file.separator}main${file.separator}bin</directory>
            <outputDirectory>${file.separator}</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>WEB-INF${file.separator}lib</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <scope>runtime</scope>
            <!-- 过滤排除自身生成的jar -->
            <excludes>
                <exclude>*war</exclude>
                <exclude>demo*jar</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

四,执行 maven命令

mvn clean package
在target目录下生成zip包:demo-20180803.zip

转载于:https://blog.51cto.com/jtech/2153966