IntelliJ IDEA 结合 maven通过profile实现多环境
yuyutoo 2024-10-16 15:47 16 浏览 0 评论
为什么需要多环境
在it项目开发过程中,会有开发、测试、生产等多套环境, 所以一般在项目中使用配置文件来区分环境变量参数,比如:开发数据库服务器Ip:192.168.1.33,生产服务器Ip是:biz_dbserver001,测试服务器上的数据库是:test_dbserver001, 不同的环境参数也是不同的,如果我们在系统开发的时候不考虑多环境管理的话,等我们从开发迁移到测试的时候,需要改动的参数会随着系统规模的增大而变得多起来, 如果漏掉任何一个参数,都会造成系统运行出来的结果不正确,或者系统不能正常工作等问题。 因此会有多环境管理,通过把系统一些参数抽象出来,然后定义多个环境参数文件, 文件定义环境特有的一些数据值。 然后在打包过程中,使用目标环境变量参数替换预定义的系统参数。 打包后,直接部署到目标环境即可保证正确运行。减少了脑力、人力工作。
在c/c++语言开发的系统中,有些参数是通过环境变量获取env,window上是注册表,有些是通过ini、conf、etc、yaml文件通过在安装过程中,把参数值写入到env或者window的注册表中,以后每次启动的时候,首先把参数都读出来,放在内存里,方便在程序运行过程中直接拿到期望的参数值。
在java系统开发中,一般都是通过配置文件properties、yaml、json文件获取。比如在springmvc项目中,一般都是application.properties、xml等配置文件。
java打包工具
像是c/c++使用make解决依赖和编译程序,java一开始使用的是自己的打包工具,从一开始的java命令,到后来的ant、maven、gradle,一点点进步。 启动本文主要介绍的是maven,其他就不细说了。
maven最核心的改进就在于提出仓库这个概念。我可以把所有依赖的包,都放到仓库里去,在我的工程管理文件里,标明我需要什么什么包,什么什么版本。在构建的时候,maven就自动帮我把这些包打到我的包里来了。我们再也不用操心着自己去管理几十上百个jar文件了。
maven使用如下:
C:\home\apache-maven-3.5.3\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 (disables output color)
-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
-gt,--global-toolchains <arg> Alternate path for the global
toolchains file
-h,--help Display help information
-l,--log-file <arg> Log file where all build output
will go (disables output color)
-llr,--legacy-local-repository Use Maven 2 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,--toolchains <arg> Alternate path for the user
toolchains file
-T,--threads <arg> Thread count, for instance 2.0C
where C is core multiplied
-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,--version Display version information
-V,--show-version Display version information
WITHOUT stopping build
-X,--debug Produce execution debug output
C:\home\apache-maven-3.5.3\bin>
实现方式
我们这里利用maven的profile方式实现多环境变量,maven常见的两种使用Profile的方法:占位符替换和文件复制。我们使用占位符替换方式,通过在项目顶层的pom.xml文件中定义多个profile,里面分别定义不同环境的一些参数值,在通过maven package -PenvName的时候,使用envName对应的profile里面的值替换掉工程配置文件里面的${}变量的值。
- 3.1 profile定义
pom.xml文件里面定义多个profile,如下:
<profiles>
<profile>
<id>生产环境</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<_env.name>生产环境</_env.name>
<_datasource.url>jdbc:oracle:thin:@40.17.13.150:1521:biz</_datasource.url>
<_datasource.username>biz_product</_datasource.username>
<_datasource.publickey>MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIa7nwUBBMpredF5d5XmbY6RTmDbgwQWzKRvqN9AfoYIMuWbVNmEnaXImJEsVNCpG1uCj5JdoxnNhyDMDrgVT7kCAwEAAQ==</_datasource.publickey>
<_datasource.password>PCf2UyzwD4ynCrm/NiyepRtp//iMRhTNUkPM+GiVIzwXVHJiQABk1P41+yMwj5VIYJECk45aXVjuvuXOEFshQA==</_datasource.password>
<!-- log4j参数 begin-->
<_log4j.path>/home/lehoon/apache-tomcat-8.0.50/temp/lehoon/logs</_log4j.path>
<!-- log4j参数 end-->
<!-- ehcache参数 begin-->
<_ehcache.path>/home/lehoon/apache-tomcat-8.0.50/temp/lehoon/ehcache</_ehcache.path>
<!-- ehcache参数 end-->
<_redis.host>localhost</_redis.host>
<_redis.port>6379</_redis.port>
<_redis.password>smartweb</_redis.password>
</properties>
</profile>
<profile>
<id>测试环境</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<_env.name>测试环境</_env.name>
<_datasource.url>jdbc:oracle:thin:@147.3.156.134:1521:ORCL</_datasource.url>
<_datasource.username>biz_develop_test</_datasource.username>
<_datasource.publickey>MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIa7nwUBBMpredF5d5XmbY6RTmDbgwQWzKRvqN9AfoYIMuWbVNmEnaXImJEsVNCpG1uCj5JdoxnNhyDMDrgVT7kCAwEAAQ==</_datasource.publickey>
<_datasource.password>PCf2UyzwD4ynCrm/NiyepRtp//iMRhTNUkPM+GiVIzwXVHJiQABk1P41+yMwj5VIYJECk45aXVjuvuXOEFshQA==</_datasource.password>
<!-- log4j参数 begin-->
<_log4j.path>/home/lehoon/tomcat/temp/lehoon/logs</_log4j.path>
<!-- log4j参数 end-->
<!-- ehcache参数 begin-->
<_ehcache.path>/home/lehoon/tomcat/temp/lehoon/ehcache</_ehcache.path>
<!-- ehcache参数 end-->
<_redis.host>localhost</_redis.host>
<_redis.port>6379</_redis.port>
<_redis.password>smartweb</_redis.password>
</properties>
</profile>
<profile>
<id>开发环境</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<_env.name>开发环境</_env.name>
<_datasource.url>jdbc:oracle:thin:@localhost:1521:ORCL</_datasource.url>
<_datasource.username>biz_develop</_datasource.username>
<_datasource.publickey>MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIa7nwUBBMpredF5d5XmbY6RTmDbgwQWzKRvqN9AfoYIMuWbVNmEnaXImJEsVNCpG1uCj5JdoxnNhyDMDrgVT7kCAwEAAQ==</_datasource.publickey>
<_datasource.password>PCf2UyzwD4ynCrm/NiyepRtp//iMRhTNUkPM+GiVIzwXVHJiQABk1P41+yMwj5VIYJECk45aXVjuvuXOEFshQA==</_datasource.password>
<!-- log4j参数 begin-->
<_log4j.path>../temp/lehoon/logs</_log4j.path>
<!-- log4j参数 end-->
<!-- ehcache参数 begin-->
<_ehcache.path>../temp/lehoon/ehcache</_ehcache.path>
<!-- ehcache参数 end-->
<_redis.host>localhost</_redis.host>
<_redis.port>6379</_redis.port>
<_redis.password>smartweb</_redis.password>
</properties>
</profile>
</profiles>
公共配置文件common.properties定义如下:
#============================#
#===== Database sttings =====#
#============================#
#oracle database setting
jdbc.type=oracle
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=${_datasource.url}
jdbc.username=${_datasource.username}
jdbc.password=${_datasource.password}
jdbc.publickey=${_datasource.publickey}
#pool settings
jdbc.pool.init=5
jdbc.pool.minIdle=10
jdbc.pool.maxActive=20
#jdbc.testSql=SELECT 'x'
jdbc.testSql=SELECT 'x' FROM DUAL
#redis
redis.host=${_redis.host}
redis.port=${_redis.port}
redis.password=${_redis.password}
#============================#
#===== System settings ======#
#============================#
#\u4ea7\u54c1\u4fe1\u606f\u8bbe\u7f6e
productName=webgis
copyrightYear=2018
version=V1.2.6
envName=${_env.name}
#log file path
log.path=${_log4j.path}
#\u7ba1\u7406\u57fa\u7840\u8def\u5f84, \u9700\u540c\u6b65\u4fee\u6539\uff1aweb.xml
adminPath=/a
#\u524d\u7aef\u57fa\u7840\u8def\u5f84
frontPath=/f
#\u7f51\u7ad9URL\u540e\u7f00
urlSuffix=.html
notAllowRefreshIndex=false
user.multiAccountLogin=true
#\u5206\u9875\u914d\u7f6e
page.pageSize=10
session.sessionTimeout=3600000
session.sessionTimeoutClean=3600000
#\u7f13\u5b58\u8bbe\u7f6e
ehcache.configFile=cache/ehcache-local.xml
#ehcache.configFile=cache/ehcache-rmi.xml
通过在common.properties文件中使用${}占位符,引用profile定义的节点值,就可以在maven打包过程中使用maven profile中定义的值插入到properties文件中来。
- 3.2 定制化profile替换
如果在我们文件中,有二次配置文件,第二层引用第一层的配置文件中的变量,我们在打包的时候不想使用maven的pforile值替换第二层配置文件的话,需要使用resource去个性化定制maven打包对资源文件的处理逻辑。 在resource中可以忽略指定类型文件、指定名称文件、引入需要处理的文件等。配置如下:
<resources>
<!-- 定制化项目各个模块下的资源配置文件及处理方式--->
<resource>
<directory>../webgis-system/src/main/resources</directory>
<includes>
<include>**/*.xml</include> <!-- 包含指定xml类型的文件进行处理--->
</includes>
<filtering>true</filtering> <!-- 使用profile占位符替换具体的值 -->
</resource>
<resource>
<directory>../webgis-PreciousMetal/src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>../webgis-common/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<excludes> <!-- 排除掉指定的文件 最终打包不会包含这些文件 -->
<exclude>cache/ehcache-rmi.xml</exclude>
<exclude>editor.html</exclude>
<exclude>mybatis.xml</exclude>
<exclude>properties/redis.properties</exclude>
<exclude>mybatis-generator-config.xml</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>mybatis.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!-- 配置使用变量的配置文件-->
<filters>
<filter>../smartweb-common/src/main/resources/common.properties</filter>
</filters>
配置好了后, idea 集成了maven后,点击右侧的maven栏,可以看到maven读取到了profile列表,
可以在这里切换环境,然后启动工程,就可以使用选择的环境启动、调试项目。 打包的话,直接使用build Artifacts打包即可。或者切换到控制台下使用maven package -p envName打包。
相关推荐
- 墨尔本一华裔男子与亚裔男子分别失踪数日 警方寻人
-
中新网5月15日电据澳洲新快网报道,据澳大利亚维州警察局网站消息,22岁的华裔男子邓跃(Yue‘Peter’Deng,音译)失踪已6天,维州警方于当地时间13日发布寻人通告,寻求公众协助寻找邓跃。华...
- 网络交友须谨慎!美国犹他州一男子因涉嫌杀害女网友被捕
-
伊森·洪克斯克(图源网络,侵删)据美国广播公司(ABC)25日报道,美国犹他州一名男子于24日因涉嫌谋杀被捕。警方表示,这名男子主动告知警局,称其杀害了一名在网络交友软件上认识的25岁女子。雷顿警...
- 一课译词:来龙去脉(来龙去脉 的意思解释)
-
Mountainranges[Photo/SIPA]“来龙去脉”,汉语成语,本指山脉的走势和去向,现比喻一件事的前因后果(causeandeffectofanevent),可以翻译为“i...
- 高考重要考点:range(range高考用法)
-
range可以用作动词,也可以用作名词,含义特别多,在阅读理解中出现的频率很高,还经常作为完形填空的选项,而且在作文中使用是非常好的高级词汇。...
- C++20 Ranges:现代范围操作(现代c++白皮书)
-
1.引言:C++20Ranges库简介C++20引入的Ranges库是C++标准库的重要更新,旨在提供更现代化、表达力更强的方式来处理数据序列(范围,range)。Ranges库基于...
- 学习VBA,报表做到飞 第二章 数组 2.4 Filter函数
-
第二章数组2.4Filter函数Filter函数功能与autofilter函数类似,它对一个一维数组进行筛选,返回一个从0开始的数组。...
- VBA学习笔记:数组:数组相关函数—Split,Join
-
Split拆分字符串函数,语法Split(expression,字符,Limit,compare),第1参数为必写,后面3个参数都是可选项。Expression为需要拆分的数据,“字符”就是以哪个字...
- VBA如何自定义序列,学会这些方法,让你工作更轻松
-
No.1在Excel中,自定义序列是一种快速填表机制,如何有效地利用这个方法,可以大大增加工作效率。通常在操作工作表的时候,可能会输入一些很有序的序列,如果一一录入就显得十分笨拙。Excel给出了一种...
- Excel VBA入门教程1.3 数组基础(vba数组详解)
-
1.3数组使用数组和对象时,也要声明,这里说下数组的声明:'确定范围的数组,可以存储b-a+1个数,a、b为整数Dim数组名称(aTob)As数据类型Dimarr...
- 远程网络调试工具百宝箱-MobaXterm
-
MobaXterm是一个功能强大的远程网络工具百宝箱,它将所有重要的远程网络工具(SSH、Telnet、X11、RDP、VNC、FTP、MOSH、Serial等)和Unix命令(bash、ls、cat...
- AREX:携程新一代自动化回归测试工具的设计与实现
-
一、背景随着携程机票BU业务规模的不断提高,业务系统日趋复杂,各种问题和挑战也随之而来。对于研发测试团队,面临着各种效能困境,包括业务复杂度高、数据构造工作量大、回归测试全量回归、沟通成本高、测试用例...
- Windows、Android、IOS、Web自动化工具选择策略
-
Windows平台中应用UI自动化测试解决方案AutoIT是开源工具,该工具识别windows的标准控件效果不错,但是当它遇到应用中非标准控件定义的UI元素时往往就无能为力了,这个时候选择silkte...
- python自动化工具:pywinauto(python快速上手 自动化)
-
简介Pywinauto是完全由Python构建的一个模块,可以用于自动化Windows上的GUI应用程序。同时,它支持鼠标、键盘操作,在元素控件树较复杂的界面,可以辅助我们完成自动化操作。我在...
- 时下最火的 Airtest 如何测试手机 APP?
-
引言Airtest是网易出品的一款基于图像识别的自动化测试工具,主要应用在手机APP和游戏的测试。一旦使用了这个工具进行APP的自动化,你就会发现自动化测试原来是如此简单!!连接手机要进行...
- 【推荐】7个最强Appium替代工具,移动App自动化测试必备!
-
在移动应用开发日益火爆的今天,自动化测试成为了确保应用质量和用户体验的关键环节。Appium作为一款广泛应用的移动应用自动化测试工具,为测试人员所熟知。然而,在不同的测试场景和需求下,还有许多其他优...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)