IntelliJ IDEA 结合 maven通过profile实现多环境
yuyutoo 2024-10-16 15:47 6 浏览 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打包。
相关推荐
- 网站建设:从新手到高手
-
现代化网站应用领域非常广泛,从个人形象网站展示、企业商业网站运作、到政府公益等服务网站,各行各业都需要网站建设。大体上可以归结四类:宣传型网站设计、产品型网站制作、电子商务型网站建设、定制型功能网站开...
- JetBrains 推出全新 AI 编程工具 Junie,助力高效开发
-
JetBrains宣布推出名为Junie的全新AI编程工具。这款工具不仅能执行简单的代码生成与检查任务,还能应对编写测试、验证结果等复杂项目,为开发者提供全方位支持。根据SWEBench...
- AI也能写代码!代码生成、代码补全、注释生成、代码翻译轻松搞定
-
清华GLM技术团队打造的多语言代码生成模型CodeGeeX近期更新了新的开源版本「CodeGeeX2-6B」。CodeGeeX2是多语言代码生成模型CodeGeeX的第二代模型,不同于一代CodeG...
- 一键生成前后端代码,一个36k星的企业级低代码平台
-
「企业级低代码平台」前后端分离架构SpringBoot2.x,SpringCloud,AntDesign&Vue,Mybatis,Shiro,JWT。强大的代码生成器让前后端代码一键生成,无需写任...
- Gitee 代码托管实战指南:5 步完成本地项目云端同步(附避坑要点)
-
核心流程拆解:远程仓库的搭建登录Gitee官网(注册账号比较简单,大家自行操作),点击“新建仓库”,建议勾选“初始化仓库”和“设置模板文件”(如.gitignore),避免上传临时文件。...
- jeecg-boot 源码项目-强烈推荐使用
-
JEECGBOOT低代码开发平台...
- JetBrains推出全新AI编程工具Junie,强调以开发者为中心
-
IT之家2月1日消息,JetBrains发文,宣布推出一款名为Junie的全新AI编程工具,官方声称这款AI工具既能执行简单的代码生成与检查等基础任务,也能应对“编写测试、验证结...
- JetBrains旗下WebStorm和Rider现已加入“非商用免费”阵营
-
IT之家10月25日消息,软件开发商JetBrains今日宣布,旗下WebStorm(JavaScript开发工具)和Rider(.NET开发工具)现已加入“非商用免费”阵营。如果...
- 谈谈websocket跨域
-
了解websocketwebsocket是HTML5的新特性,在客户端和服务端提供了一个基于TCP连接的双向通道。...
- websocket调试工具
-
...
- 利用webSocket实现消息的实时推送
-
1.什么是webSocketwebSocket实现实现推送消息WebSocket是HTML5开始提供的一种在单个TCP连接上进行全双工通讯的协议。以前的推送技术使用Ajax轮询,浏览器需...
- 为 Go 开发的 WebSocket 库
-
#记录我的2024#...
- 「Java基础」Springboot+Websocket的实现后端数据实时推送
-
这篇文章主要就是实现这个功能,只演示一个基本的案例。使用的是websocket技术。...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 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)