Multi-environment
The project is divided into three kinds of environment:local、dev、pro。不同的环境配置不同,如果只拥有一套配置的话,会导致在不同的环境上需要反复的修改相同配置进行打包,比如说现在本地经过测试,需要发布到开发环境,则需要修改配置文件,改为开发环境的配置参数,进行打包,然后又调整为本地环境进行开发与测试。
Filtering
Filtering是maven的resource插件提供的功能,作用是对资源文件中的占位符进行替换。称为maven resources filter。替换的便来来自system.properties,project proerties(pom.xml的<properties>)、filter resources(filter配置文件)和command line。替换指定目录*.properties文件里的占位符(${jdbc.url}),具体使用如下:
在src/main/resources/conf目录有个配置文件jdbc.properties,内容如下:jdbc.url=${jdbc.url}jdbc.username=${jdbc.username}jdbc.password=${jdbc.password}
maven default生命周期,process-resources阶段执行maven-resources-plugin的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤
src/main/resources/conf true
maven resource plugin开启filtering进行占位符替换处理。
例如从pom.xml的project proerties元素获取变量replace placeholder variables。
4.0.0 com.panda.pack bird jar 1.0-SNAPSHOT jdbc:mysql://127.0.0.1:3306/panda root panda src/main/resources true
execute "mvn clean package -Dmaven.test.skip=true",编译后target directory的jdbc.properties内容为:
jdbc.url=jdbc:mysql://127.0.0.1:3306/pandajdbc.username=rootjdbc.password=panda
or filter resouces 通过将变量定义在配置文件中比如filters.properties,pom.xml:
src/main/filters.properties src/main/resources true
filters.properties content is as follows:
jdbc.url=jdbc:mysql://127.0.0.1:3306/pandajdbc.username=pandajdbc.password=panda
Profile
Define profile
pom.xm中的profile元素,可以在两个文件中配置:maven install directory/conf/settings.xml 和 pom.xml file of the project。
-
settings.xml中define的profile是全局的,对maven项目有效,只能定义<repositories>、<pluginRepositories>、 <properties>元素。
- pom.xml中define的profile是局部的,只对当前pom.xml所在的项目有效。
Using a profile
maven 的-P参数指定profile,参数的值是profile的id,多个profile以逗号分割,如果不想激活某个默认的profile,在id前加个!。
mvn clean package -Dmaven.test.skip=true -Plocal,dev,!pro (ignore production profile)
IDEA里则可以在 Maven Projects 里直接勾选想要激活的profile
Or Define profile时, <profile> 的 <activation> 元素下配置默认的profile。
pom.xml:
local local true
settings.xml文件通过<activeProfiles>配置default use的profile列表。
profile ids
Example
pom.xml
dev local jdbc:mysql://127.0.0.1:3306/local root local true pro dev jdbc:mysql://127.0.0.1:3306/dev root dev pro pro jdbc:mysql://127.0.0.1:3306/pro root pro src/main/resources true
使用不同Profile 的<properties> variables进行占位符替换。以上方式是将占位符变量值定义在profile的<properties>元素中进行替换,这样不方便维护,建议设置在相应配置文件中。
dev local true dev dev pro pro src/main/filters-${active.profile}.properties src/main/resources true
上图中的<filter>的"${ative.profile}"占位符指向的是<profile>元素中<properties> 的<active.profile>元素,此值可以任意定义。
在Project路径src/main中Create三个文件:filters-local.properties、filters-dev.properties、filters-pro.properties。
filters-local.properties:
jdbc.url=jdbc:mysql://127.0.0.1:3306/localjdbc.username=rootjdbc.password=local
其余两个配置文件filters-local.properties和filters-pro.properties与之类似。
配置在配置文件中方便维护,使用不同的Profile,则替换相应的Profile的{active.profile}.properties的变量。
execute
mvn clean package -Dmaven.test.skip=true -Plocal
执行以上命令,将会使用local profile的配置文件filters-local.properties的变量值进行打包,如果默认不传递-P参数,则会使用default profile。
上面的方式是定义配置文件时,使用占位符,然后再定义相应的几种环境的正式配置文件,对占位符文件进行替换。例如上面的例子,需要使用的配置文件由如下几个:
filters.properties:占位符定义文件
filters-local.properties、filters-dev.properties、filters-pro.properties三种环境的配置文件。使用占位符进行替换,有些不方便。对于大型复杂项目来说容易出现错误。
推荐使用Resources
Resources
此种方式不进行占位符文件替换,同时不需要定义占位符文件。通过定义不同的文件夹目录放置不同环境需要的配置文件,在打包或发布时指定环境,解压指定环境目录的配置文件至指定目录,此处是WEB/classes。
src/main/resources目录结构 as follow:
pom.xml as follows:
dev dev true test test prod prod src/main/resources true test/* dev/* prod/* src/main/resources/${profiles.active} org.apache.maven.plugins maven-compiler-plugin 3.5.1 1.7 1.7 UTF-8
上面的配置中关键的在于<resources>元素中的<directory>的占位符src/main/resoruces/${profiles.active}。此处的profiles.active是<profile>中定义的元素。
不同环境的配置文件在不同的文件夹中,也就是不同的<profile>中,方便单独维护,使用不同的Profile,则会将相对应目录的Profile的profiles.active变量,传递给<resources>的directory元素。例如如下:
mvn clean package -Dmaven.test.skip=true -Pprod
执行以上命令,将会使用profile为prod对应的文件夹目录的配置文件进行打包。变量的值同时如下所示:
src/main/resources/prod
换句话说就是打包prod环境的配置文件至WEB-INF/classes(如不指定<target>元素,则默认为此目录)。