目标:实现Maven动静分离打包,静态资源打成zip包,动态资源打成war包,方便独立部署
工程结构
打包前先看下工程结构
我的静态资源都是放在webapp目录下面,有css,fonts,html,images,js,根目录下的login.js,login.html,打包时需要将这些静态资源文件打成单独的zip包,动态资源打成单独的war包
打war包
pom.xml文件内容如下
<build>
<finalName>dpfinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
<useCache>falseuseCache>
<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-staticid>
<formats>
<format>zipformat>
formats>
<includeBaseDirectory>falseincludeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/target/dp/css/directory>
<outputDirectory>cssoutputDirectory>
fileSet>
<fileSet>
<directory>${project.basedir}/target/dp/fontsdirectory>
<outputDirectory>fontsoutputDirectory>
fileSet>
<fileSet>
<directory>${project.basedir}/target/dp/htmldirectory>
<outputDirectory>htmloutputDirectory>
fileSet>
<fileSet>
<directory>${project.basedir}/target/dp/imagesdirectory>
<outputDirectory>imagesoutputDirectory>
fileSet>
<fileSet>
<directory>${project.basedir}/target/dp/jsdirectory>
<outputDirectory>jsoutputDirectory>
fileSet>
<fileSet>
<directory>${project.basedir}/target/dp/directory>
<includes>
<include>login.jsinclude>
<include>login.htmlinclude>
includes>
<outputDirectory>/outputDirectory>
fileSet>
fileSets>
assembly>
重点说下,为什么这里指定的是target目录下呢,编译完成后,在target目录下会生成dp目录(也就是你的工程名),在dp目录下有编译过后的所有文件,包括静态资源文件因此zip包时,因此从target/dp目录下取静态资源文件,target/dp目录如下
再来看下pom.xml文件的配置
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>2.2.1version>
<executions>
<execution>
<id>>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
<configuration>
<finalName>dp-staticfinalName>
<appendAssemblyId>falseappendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/static-zip.xmldescriptor>
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>dpfinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
<useCache>falseuseCache>
<packagingExcludes>
css/**,fonts/**,html/**,images/**,js/**,login.html,login.js
packagingExcludes>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>2.2.1version>
<executions>
<execution>
<id>>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
<configuration>
<finalName>dp-staticfinalName>
<appendAssemblyId>falseappendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/static-zip.xmldescriptor>
descriptors>
configuration>
execution>
executions>
plugin>
<plugins>
<build>