首页 >> 手游攻略

makexim,maven通过profile实现多环境

大家好,makexim相信很多的网友都不是很明白,包括maven通过profile实现多环境也是一样,不过没有关系,接下来就来为大家分享关于makexim和maven通过profile实现多环境的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

在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等配置文件。

像是c/c++使用make解决依赖和编译程序,java一开始使用的是自己的打包工具,从一开始的java命令,到后来的ant、maven、gradle,一点点进步。启动本文主要介绍的是maven,其他就不细说了。

maven最核心的改进就在于提出仓库这个概念。我可以把所有依赖的包,都放到仓库里去,在我的工程管理文件里,标明我需要什么什么包,什么什么版本。在构建的时候,maven就自动帮我把这些包打到我的包里来了。我们再也不用操心着自己去管理几十上百个jar文件了。

C:\home\apache-maven-3.5.3\bin>mvn--helpnnusage:mvn[options][<goal(s)>][<phase(s)>]nnOptions:n-am,--also-makeIfprojectlistisspecified,alsonbuildprojectsrequiredbythenlistn-amd,--also-make-dependentsIfprojectlistisspecified,alsonbuildprojectsthatdependonnprojectsonthelistn-B,--batch-modeRuninnon-interactive(batch)nmode(disablesoutputcolor)n-b,--builder<arg>Theidofthebuildstrategytonusen-C,--strict-checksumsFailthebuildifchecksumsdon'tnmatchn-c,--lax-checksumsWarnifchecksumsdon'tmatchn-cpu,--check-plugin-updatesIneffective,onlykeptfornbackwardcompatibilityn-D,--define<arg>Defineasystempropertyn-e,--errorsProduceexecutionerrormessagesn-emp,--encrypt-master-password<arg>Encryptmastersecuritypasswordn-ep,--encrypt-password<arg>Encryptserverpasswordn-f,--file<arg>ForcetheuseofanalternatePOMnfile(ordirectorywithpom.xml)n-fae,--fail-at-endOnlyfailthebuildafterwards;nallowallnon-impactedbuildstoncontinuen-ff,--fail-fastStopatfirstfailureinnreactorizedbuildsn-fn,--fail-neverNEVERfailthebuild,regardlessnofprojectresultn-gs,--global-settings<arg>Alternatepathfortheglobalnsettingsfilen-gt,--global-toolchains<arg>Alternatepathfortheglobalntoolchainsfilen-h,--helpDisplayhelpinformationn-l,--log-file<arg>Logfilewhereallbuildoutputnwillgo(disablesoutputcolor)n-llr,--legacy-local-repositoryUseMaven2LegacyLocalnRepositorybehaviour,ienouseofn_remote.repositories.Canalsobenactivatedbyusingn-Dmaven.legacyLocalRepo=truen-N,--non-recursiveDonotrecurseintosub-projectsn-npr,--no-plugin-registryIneffective,onlykeptfornbackwardcompatibilityn-npu,--no-plugin-updatesIneffective,onlykeptfornbackwardcompatibilityn-nsu,--no-snapshot-updatesSuppressSNAPSHOTupdatesn-o,--offlineWorkofflinen-P,--activate-profiles<arg>Comma-delimitedlistofprofilesntoactivaten-pl,--projects<arg>Comma-delimitedlistofspecifiednreactorprojectstobuildinsteadnofallprojects.Aprojectcanbenspecifiedby[groupId]:artifactIdnorbyitsrelativepathn-q,--quietQuietoutput-onlyshowerrorsn-rf,--resume-from<arg>Resumereactorfromspecifiednprojectn-s,--settings<arg>Alternatepathfortheusernsettingsfilen-t,--toolchains<arg>Alternatepathfortheuserntoolchainsfilen-T,--threads<arg>Threadcount,forinstance2.0CnwhereCiscoremultipliedn-U,--update-snapshotsForcesacheckformissingnreleasesandupdatedsnapshotsonnremoterepositoriesn-up,--update-pluginsIneffective,onlykeptfornbackwardcompatibilityn-v,--versionDisplayversioninformationn-V,--show-versionDisplayversioninformationnWITHOUTstoppingbuildn-X,--debugProduceexecutiondebugoutputnnC:\home\apache-maven-3.5.3\bin>实现方式

我们这里利用maven的profile方式实现多环境变量,maven常见的两种使用Profile的方法:占位符替换和文件复制。我们使用占位符替换方式,通过在项目顶层的pom.xml文件中定义多个profile,里面分别定义不同环境的一些参数值,在通过mavenpackage-PenvName的时候,使用envName对应的profile里面的值替换掉工程配置文件里面的${}变量的值。

pom.xml文件里面定义多个profile,如下:

<profiles>n<profile>n<id>生产环境</id>n<activation>n<activeByDefault>false</activeByDefault>n</activation>n<properties>n<_env.name>生产环境</_env.name>n<_datasource.url>jdbc:oracle:thin:@40.17.13.150:1521:biz</_datasource.url>n<_datasource.username>biz_product</_datasource.username>n<_datasource.publickey>MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIa7nwUBBMpredF5d5XmbY6RTmDbgwQWzKRvqN9AfoYIMuWbVNmEnaXImJEsVNCpG1uCj5JdoxnNhyDMDrgVT7kCAwEAAQ==</_datasource.publickey>n<_datasource.password>PCf2UyzwD4ynCrm/NiyepRtp//iMRhTNUkPM+GiVIzwXVHJiQABk1P41+yMwj5VIYJECk45aXVjuvuXOEFshQA==</_datasource.password>nn<!--log4j参数begin-->n<_log4j.path>/home/lehoon/apache-tomcat-8.0.50/temp/lehoon/logs</_log4j.path>n<!--log4j参数end-->nn<!--ehcache参数begin-->n<_ehcache.path>/home/lehoon/apache-tomcat-8.0.50/temp/lehoon/ehcache</_ehcache.path>n<!--ehcache参数end-->nn<_redis.host>localhost</_redis.host>n<_redis.port>6379</_redis.port>n<_redis.password>smartweb</_redis.password>n</properties>n</profile>n<profile>n<id>测试环境</id>n<activation>n<activeByDefault>false</activeByDefault>n</activation>n<properties>n<_env.name>测试环境</_env.name>n<_datasource.url>jdbc:oracle:thin:@147.3.156.134:1521:ORCL</_datasource.url>n<_datasource.username>biz_develop_test</_datasource.username>n<_datasource.publickey>MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIa7nwUBBMpredF5d5XmbY6RTmDbgwQWzKRvqN9AfoYIMuWbVNmEnaXImJEsVNCpG1uCj5JdoxnNhyDMDrgVT7kCAwEAAQ==</_datasource.publickey>n<_datasource.password>PCf2UyzwD4ynCrm/NiyepRtp//iMRhTNUkPM+GiVIzwXVHJiQABk1P41+yMwj5VIYJECk45aXVjuvuXOEFshQA==</_datasource.password>nn<!--log4j参数begin-->n<_log4j.path>/home/lehoon/tomcat/temp/lehoon/logs</_log4j.path>n<!--log4j参数end-->nn<!--ehcache参数begin-->n<_ehcache.path>/home/lehoon/tomcat/temp/lehoon/ehcache</_ehcache.path>n<!--ehcache参数end-->nn<_redis.host>localhost</_redis.host>n<_redis.port>6379</_redis.port>n<_redis.password>smartweb</_redis.password>n</properties>n</profile>nn<profile>n<id>开发环境</id>n<activation>n<activeByDefault>true</activeByDefault>n</activation>n<properties>n<_env.name>开发环境</_env.name>n<_datasource.url>jdbc:oracle:thin:@localhost:1521:ORCL</_datasource.url>n<_datasource.username>biz_develop</_datasource.username>n<_datasource.publickey>MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIa7nwUBBMpredF5d5XmbY6RTmDbgwQWzKRvqN9AfoYIMuWbVNmEnaXImJEsVNCpG1uCj5JdoxnNhyDMDrgVT7kCAwEAAQ==</_datasource.publickey>n<_datasource.password>PCf2UyzwD4ynCrm/NiyepRtp//iMRhTNUkPM+GiVIzwXVHJiQABk1P41+yMwj5VIYJECk45aXVjuvuXOEFshQA==</_datasource.password>nn<!--log4j参数begin-->n<_log4j.path>../temp/lehoon/logs</_log4j.path>n<!--log4j参数end-->nn<!--ehcache参数begin-->n<_ehcache.path>../temp/lehoon/ehcache</_ehcache.path>n<!--ehcache参数end-->nn<_redis.host>localhost</_redis.host>n<_redis.port>6379</_redis.port>n<_redis.password>smartweb</_redis.password>n</properties>n</profile>n</profiles>

公共配置文件common.properties定义如下:

#============================#n#=====Databasesttings=====#n#============================#nn#oracledatabasesettingnjdbc.type=oraclenjdbc.driver=oracle.jdbc.OracleDrivernjdbc.url=${_datasource.url}njdbc.username=${_datasource.username}njdbc.password=${_datasource.password}njdbc.publickey=${_datasource.publickey}nn#poolsettingsnjdbc.pool.init=5njdbc.pool.minIdle=10njdbc.pool.maxActive=20nn#jdbc.testSql=SELECT'x'njdbc.testSql=SELECT'x'FROMDUALnn#redisnredis.host=${_redis.host}nredis.port=${_redis.port}nredis.password=${_redis.password}nn#============================#n#=====Systemsettings======#n#============================#nn#\u4ea7\u54c1\u4fe1\u606f\u8bbe\u7f6enproductName=webgisncopyrightYear=2018nversion=V1.2.6nenvName=${_env.name}nn#logfilepathnlog.path=${_log4j.path}nn#\u7ba1\u7406\u57fa\u7840\u8def\u5f84,\u9700\u540c\u6b65\u4fee\u6539\uff1aweb.xmlnadminPath=/ann#\u524d\u7aef\u57fa\u7840\u8def\u5f84nfrontPath=/fnn#\u7f51\u7ad9URL\u540e\u7f00nurlSuffix=.htmlnnnotAllowRefreshIndex=falsennuser.multiAccountLogin=truenn#\u5206\u9875\u914d\u7f6enpage.pageSize=10nnnsession.sessionTimeout=3600000nsession.sessionTimeoutClean=3600000nn#\u7f13\u5b58\u8bbe\u7f6enehcache.configFile=cache/ehcache-local.xmln#ehcache.configFile=cache/ehcache-rmi.xml

通过在common.properties文件中使用${}占位符,引用profile定义的节点值,就可以在maven打包过程中使用mavenprofile中定义的值插入到properties文件中来。

如果在我们文件中,有二次配置文件,第二层引用第一层的配置文件中的变量,我们在打包的时候不想使用maven的pforile值替换第二层配置文件的话,需要使用resource去个性化定制maven打包对资源文件的处理逻辑。在resource中可以忽略指定类型文件、指定名称文件、引入需要处理的文件等。配置如下:

<resources>n<!--定制化项目各个模块下的资源配置文件及处理方式--->nttt<resource>ntttt<directory>../webgis-system/src/main/resources</directory>ntttt<includes>nttttt<include>**/*.xml</include><!--包含指定xml类型的文件进行处理--->ntttt</includes>ntttt<filtering>true</filtering><!--使用profile占位符替换具体的值-->nttt</resource>nttt<resource>ntttt<directory>../webgis-PreciousMetal/src/main/resources</directory>ntttt<includes>nttttt<include>**/*.xml</include>ntttt</includes>ntttt<filtering>true</filtering>nttt</resource>nttt<resource>ntttt<directory>../webgis-common/src/main/resources</directory>ntttt<filtering>true</filtering>nttt</resource>nttt<resource>ntttt<directory>src/main/resources</directory>ntttt<includes>nttttt<include>**/*.xml</include>nttttt<include>**/*.properties</include>ntttt</includes>n<excludes><!--排除掉指定的文件最终打包不会包含这些文件-->n<exclude>cache/ehcache-rmi.xml</exclude>n<exclude>editor.html</exclude>n<exclude>mybatis.xml</exclude>n<exclude>properties/redis.properties</exclude>n<exclude>mybatis-generator-config.xml</exclude>n</excludes>ntttt<filtering>true</filtering>nttt</resource>n<resource>n<directory>src/main/resources</directory>n<includes>n<include>mybatis.xml</include>n</includes>n<filtering>false</filtering>n</resource>ntt</resources>tt

<!--配置使用变量的配置文件-->ntt<filters>nttt<filter>../smartweb-common/src/main/resources/common.properties</filter>ntt</filters>

配置好了后,idea集成了maven后,点击右侧的maven栏,可以看到maven读取到了profile列表,

可以在这里切换环境,然后启动工程,就可以使用选择的环境启动、调试项目。打包的话,直接使用buildArtifacts打包即可。或者切换到控制台下使用mavenpackage-penvName打包。

END,本文到此结束,如果可以帮助到大家,还望关注本站哦!



本文由欣欣吧手游攻略栏目发布,感谢您对欣欣吧的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“makexim,maven通过profile实现多环境

标签:
madonna归来?阿朵
« 上一篇 2024-01-08