IntelliJ IDEA 结合 maven通过profile实现多环境
yuyutoo 2024-10-16 15:47 10 浏览 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打包。
相关推荐
- 深度解读Spring框架的核心原理
-
深度解读Spring框架的核心原理在Java开发的世界里,提到Spring框架,就像提起一位久经沙场的老将,它几乎成了企业级应用开发的代名词。那么,这个被无数开发者膜拜的框架究竟有何独特之处?今天,我...
- 「Spring认证」Spring 框架概述
-
Spring是最流行的企业Java应用程序开发框架。全球数以百万计的开发人员使用SpringFramework来创建高性能、易于测试和可重用的代码。Spring框架是一个开源的Java...
- 学习Spring框架 这一篇就够了
-
1.spring概述1.1Spring是什么(理解)...
- Spring框架双核解析:IOC与AOP的本质与实战
-
#Spring核心#IOC容器#AOP编程#Java框架设计...
- Spring Boot与传统Spring框架的对比:探索Java开发的新境界
-
SpringBoot与传统Spring框架的对比:探索Java开发的新境界在Java生态系统中,Spring框架无疑是一个里程碑式的存在。从最初的简单依赖注入容器,到如今覆盖企业级开发方方面面的庞大...
- Spring MVC框架源码深度剖析:从入门到精通
-
SpringMVC框架源码深度剖析:从入门到精通SpringMVC框架简介SpringMVC作为Spring框架的一部分,为构建Web应用程序提供了强大且灵活的支持。它遵循MVC(Model-V...
- Spring框架入门
-
一.spring是什么?Spring是分层...
- 程序员必知必会技能之Spring框架基础——面向切面编程!
-
面向切面编程AOP(AspectOrientedProgramming)与OOP(ObjectOrientedProgramming,面向对象编程)相辅相成。AOP提供了与OOP不同的抽象软件结...
- Spring Security安全框架深度解读:为你的应用穿上“钢铁铠甲”
-
SpringSecurity安全框架深度解读:为你的应用穿上“钢铁铠甲”在现代网络世界里,保护我们的应用程序免受各种威胁攻击至关重要。而在这个过程中,SpringSecurity框架无疑是我们最可...
- Spring框架的设计哲学与实现:打造轻量级的企业级Java应用
-
Spring框架的设计哲学与实现:打造轻量级的企业级Java应用Spring框架自2003年诞生以来,已成为企业级Java应用开发的代名词。它不仅仅是一个框架,更是一种设计理念和哲学的体现。本文将带你...
- Spring框架深度解析:从核心原理到底层实现的全方位避坑指南
-
一、Spring框架核心概念解析1.控制反转(IoC)与依赖注入(DI)Spring的核心思想是通过IoC容器管理对象的生命周期和依赖关系。传统开发中,对象通过new主动创建依赖对象,导致高耦合;而S...
- Java框架 —— Spring简介
-
简介一般来说,Spring指的是SpringFramework,它提供了很多功能,例如:控制反转(IOC)、依赖注入...
- Spring 框架概述,模块划分
-
Spring框架以控制反转(InversionofControl,IoC)和面向切面编程(Aspect-OrientedProgramming,AOP)为核心,旨在简化企业级应用开发,使开发者...
- spring框架怎么实现依赖注入?
-
依赖注入的作用就是在使用Spring框架创建对象时,动态的将其所依赖的对象注入到Bean组件中,其实现方式通常有两种,一种是属性setter方法注入,另一种是构造方法注入。具体介绍如下:●属性set...
- Spring框架详解
-
Spring是一种开放源码框架,旨在解决企业应用程序开发的复杂性。一个主要优点就是它的分层体系结构,层次结构让你可以选择要用的组件,同时也为J2EE应用程序开发提供了集成框架。 Spring特征...
你 发表评论:
欢迎- 一周热门
-
-
前端面试:iframe 的优缺点? iframe有那些缺点
-
带斜线的表头制作好了,如何填充内容?这几种方法你更喜欢哪个?
-
漫学笔记之PHP.ini常用的配置信息
-
推荐7个模板代码和其他游戏源码下载的网址
-
其实模版网站在开发工作中很重要,推荐几个参考站给大家
-
[干货] JAVA - JVM - 2 内存两分 [干货]+java+-+jvm+-+2+内存两分吗
-
正在学习使用python搭建自动化测试框架?这个系统包你可能会用到
-
织梦(Dedecms)建站教程 织梦建站详细步骤
-
【开源分享】2024PHP在线客服系统源码(搭建教程+终身使用)
-
2024PHP在线客服系统源码+完全开源 带详细搭建教程
-
- 最近发表
- 标签列表
-
- 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)