在项目改造中,把项目编码统一成utf-8,在改造的过程中遇到后台系统(bootsrap框架)列表的图标的显示乱码(口),经分析发现是因为 maven打包是filter配置问题,将bootstrap相关的占位符进行了替换所致,现在将maven--》build的配置做下记录,供自己参考:
在静态资源的过滤中,基本的元素有三种:
directory:指定资源所在的目录,这个目录的路径是相对于pom.xml文件;
includes:指定要包含哪些文件,其中包括inlcude子节点来指定匹配的模式;
excludes:指定要排除哪些文件,其中包括exclude子节点来指定匹配的模式;
filtering:指定哪些文件需要过滤,这个过滤的目的是为了替换其中的占位符${},其中的占位符属性在pom.xml文件中的<properties></properties>中指定;
1.profiles定义了各个环境的变量id
2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值
3.resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean等,里面用到的变量在打包时就会根据filter中的变量配置替换成固定值
在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产使用(product)时,需要不停的去修改这些配制文件,次数一多,相当麻烦。现在,利用maven的filter和profile功能,我们可实现在编译阶段简单的指定一个参数就能切换配制,提高效率,还不容易出错,详解如下。
一,原理:
利用filter实现对资源文件(resouces)过滤
maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的${key}进行替换,最终把你的资源文件中的username=${key}替换成username=value
利用profile来切换环境
maven profile可使用操作系统信息,jdk信息,文件是否存在,属性值等作为依据,来激活相应的profile,也可在编译阶段,通过mvn命令加参数 -PprofileId 来手工激活使用对应的profile
结合filter和profile,我们就可以方便的在不同环境下使用不同的配制
<profiles> <profile> <id>dev</id>
<!-- 默认激活开发配制,使用config-dev.properties来替换设置过虑的资源文件中的${key} --><activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>dev</env> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> </profile> </profiles>
<build> <resources> <resource> <!-- 先指定 src/main/resources下所有文件及文件夹为资源文件 --> <directory>src/main/resources</directory> <excludes> <exclude>dev/*</exclude> <exclude>prod/*</exclude> <exclude>static/**</exclude> <exclude>com/rt/**</exclude> <exclude>decorators/**</exclude> <exclude>page/**</exclude> </excludes> <filtering>true</filtering> </resource> <resource> <!-- 设置对.xml .properties文件进行过虑,即这些文件中的${key}会被替换掉为真正的值 --> <directory>src/main/resources/${env}</directory> <includes> <include>*.*</include> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>static/**</include> <include>com/rt/**</include> <include>decorators/**</include> <include>page/**</include> </includes> </resource> </resources> </build>