使用maven的profile切换项目各环境的参数

Java后端开发经常需要面对管理多套环境,一般有三种环境:开发,测试,生产

各个环境之间的参数各不相同,比如MySQL、Redis等不同环境的host不一样,若每个环境都手动替换环境很容易出错,Maven profile正是提供了配置多种环境的功能。

创建一个与resource文件夹同级的文件夹,我这里取名叫profiles

使用maven的profile切换项目各环境的参数

src/main/profiles/dev 目录对应开发环境的配置项目
src/main/profiles/beta 目录对应测试环境的配置项目
src/main/profiles/gray 目录对应灰度环境的配置项目
src/main/profiles/prd 目录对应生产环境的配置项目

接下来就是配置maven profile,如下,设定dev是默认激活的profile,此外根据具体的环境名称引入对应的资源文件夹。

<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<!-- 这里的属性名是随便取的,可以在后续配置中引用 -->
<profiles.dir>dev</profiles.dir>
</properties>
<!-- 是否默认 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile> <profile>
<!-- beta环境 -->
<id>beta</id>
<properties>
<profiles.dir>beta</profiles.dir>
</properties>
</profile> <profile>
<!-- 灰度环境 -->
<id>gray</id>
<properties>
<profiles.dir>gray</profiles.dir>
</properties>
</profile> <profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.dir>prod</profiles.dir>
</properties>
</profile>
</profiles>

activeByDefault标签的值为true的话表示默认的profile,使用mvn install命令起作用的就是它,这里为dev

resources标签定义要包含的资源,在下面的配置下package阶段会把resources文件夹里的 ${profiles.activation}/* 文件打包
这里的${profiles.activation}由命令maven的-P选项指定,例:mvn install -Pbeta 就是打包 beta/* 即beta目录下的所有文件 
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<!-- 这里的resource配置的是需要导入到项目的资源文件夹 -->
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- 这里的${profiles.dir}是你上面配置的属性值,用于动态替换,比如打包的时候输入的是-Pdev则这里就是的${profiles.dir}就是dev的值 -->
<resource>
<directory>src/main/profiles/${profiles.dir}</directory>
</resource>
</resources>
</build>

如果要把这些文件放在spring容器中呢?

在war包中这些文件就是在classpath中。

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:environment.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

使用maven的打包命令并且指定使用那个文件夹做为资源文件

mvn clean package -Dmaven.test.skip=true -Pbeta

注意最后一个-P beta,maven会激活项目下的pom.xml配置的<profiles>标签下id为test。

maven命令选项

[root@winner_0715 bin]# mvn --help

usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
-am,--also-make If project list is specified, also
build projects required by the
list
-amd,--also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
-B,--batch-mode Run in non-interactive (batch)
mode
-b,--builder <arg> The id of the build strategy to
use.
-C,--strict-checksums Fail the build if checksums don't
match
-c,--lax-checksums Warn if checksums don't match
-cpu,--check-plugin-updates Ineffective, only kept for
backward compatibility
-D,--define <arg> Define a system property
-e,--errors Produce execution error messages
-emp,--encrypt-master-password <arg> Encrypt master security password
-ep,--encrypt-password <arg> Encrypt server password
-f,--file <arg> Force the use of an alternate POM
file (or directory with pom.xml).
-fae,--fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
-ff,--fail-fast Stop at first failure in
reactorized builds
-fn,--fail-never NEVER fail the build, regardless
of project result
-gs,--global-settings <arg> Alternate path for the global
settings file
-h,--help Display help information
-l,--log-file <arg> Log file to where all build output
will go.
-llr,--legacy-local-repository Use Maven Legacy Local
Repository behaviour, ie no use of
_remote.repositories. Can also be
activated by using
-Dmaven.legacyLocalRepo=true
-N,--non-recursive Do not recurse into sub-projects
-npr,--no-plugin-registry Ineffective, only kept for
backward compatibility
-npu,--no-plugin-updates Ineffective, only kept for
backward compatibility
-nsu,--no-snapshot-updates Suppress SNAPSHOT updates
-o,--offline Work offline
-P,--activate-profiles <arg> Comma-delimited list of profiles
to activate
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.
-q,--quiet Quiet output - only show errors
-rf,--resume-from <arg> Resume reactor from specified
project
-s,--settings <arg> Alternate path for the user
settings file
-T,--threads <arg> Thread count, for instance .0C
where C is core multiplied
-t,--toolchains <arg> Alternate path for the user
toolchains file
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
-up,--update-plugins Ineffective, only kept for
backward compatibility
-V,--show-version Display version information
WITHOUT stopping build
-v,--version Display version information
-X,--debug Produce execution debug output
上一篇:Redis学习总结(五)--Redis集群创建


下一篇:Redis集群创建和配置