架构系列四:Maven实现动静分离打war包及zip包

目标:实现Maven动静分离打包,静态资源打成zip包,动态资源打成war包,方便独立部署

工程结构
打包前先看下工程结构
这里写图片描述
我的静态资源都是放在webapp目录下面,有css,fonts,html,images,js,根目录下的login.js,login.html,打包时需要将这些静态资源文件打成单独的zip包,动态资源打成单独的war包

打war包
pom.xml文件内容如下

<build>
    <finalName>dp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <useCache>false</useCache>
                <!--排除静态资源 ,静态资源单独打包-->
                <packagingExcludes>
                    css/**,fonts/**,html/**,images/**,js/**,login.html,login.js
                </packagingExcludes>
            </configuration>
        </plugin>
    <plugins>
<build>

这里使用maven-war-plugin打war包,打包时,在packagingExcludes标签中排除所有的静态资源文件,打出来的war包只有resource文件文件,jsp文件,class文件等,结构如下
这里写图片描述

打静态zip包
这里使用maven-assembly-plugin插件打zip包,打包之前,需要在src/main/assembly/路径下增加static-zip.xml文件,该文件主要用来指定zip包中要包含有哪些静态资源文件,及静态资源文件的输出目录,static-zip.xml文件内容如下:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"   
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 
                      http://maven.apache.org/xsd/assembly-1.1.0.xsd">          
    <id>dp-static</id>  
    <formats>  
        <!-- 输出的文件格式 -->
        <format>zip</format>  
    </formats>  
    <!-- 不包含根目录,如果为true,会在我们生成的zip文件结构中多出一层dp目录 -->
    <includeBaseDirectory>false</includeBaseDirectory>  
    <fileSets>  
        <!-- 把图样式文件输出到css目录下 -->
        <fileSet>  
            <directory>${project.basedir}/target/dp/css/</directory>  
            <outputDirectory>css</outputDirectory>  
        </fileSet>  
        <!-- 把字体文件输出到fonts目录下 -->
        <fileSet>
            <directory>${project.basedir}/target/dp/fonts</directory>
            <outputDirectory>fonts</outputDirectory>
        </fileSet>
        <!-- 把静态页面输出到html目录下 -->
        <fileSet>
            <directory>${project.basedir}/target/dp/html</directory>
            <outputDirectory>html</outputDirectory>
        </fileSet>
        <!-- 把图片输出到images目录下 -->
        <fileSet>
            <directory>${project.basedir}/target/dp/images</directory>
            <outputDirectory>images</outputDirectory>
        </fileSet>
        <!-- 把js文件输出到js目录下 -->
        <fileSet>
            <directory>${project.basedir}/target/dp/js</directory>
            <outputDirectory>js</outputDirectory>
        </fileSet>
        <!-- 把login.js,login.html文件输出到根目录下 -->
        <fileSet>
            <directory>${project.basedir}/target/dp/</directory>
            <includes>
                <include>login.js</include>
                <include>login.html</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

重点说下,为什么这里指定的是target目录下呢,编译完成后,在target目录下会生成dp目录(也就是你的工程名),在dp目录下有编译过后的所有文件,包括静态资源文件因此zip包时,因此从target/dp目录下取静态资源文件,target/dp目录如下
这里写图片描述

再来看下pom.xml文件的配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
            <id>>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <finalName>dp-static</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/static-zip.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

说明:
1.phase标签:值为package,在打包时执行,打包前会先编译并在target目录下生成dp目录,确保在打包时,能从target目录下获取到所有的静态资源文件
2.finalName标签:自定义zip包名称,打出来的zip文件名为dp-static
3.descriptor标签:指定了上面新增的static-zip.xml文件,打包时,会自动执行这个文件生成zip文件

执行maven install命令后,在target目录下,生成了dp.war包,也生成了dp-static.zip包,如下
这里写图片描述

最后附上完整的pom文件的打包代码,如下

<build>
    <finalName>dp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <useCache>false</useCache>
                <!--排除静态资源 ,静态资源单独打包-->
                <packagingExcludes>
                    css/**,fonts/**,html/**,images/**,js/**,login.html,login.js
                </packagingExcludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>dp-static</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/main/assembly/static-zip.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    <plugins>
<build>

猜你喜欢

转载自blog.csdn.net/kity9420/article/details/81039852