在it项目开发过程中,会有开发、测试、生产等多套环境, 所以一般在项目中使用配置文件来区分环境变量参数,比如:开发数据库服务器Ip:192.168.1.33,生产服务器Ip是:biz_dbserver001,测试服务器上的数据库是:test_dbserver001, 不同的环境参数也是不同的,如果我们在系统开发的时候不考虑多环境管理的话,等我们从开发迁移到测试的时候,需要改动的参数会随着系统规模的增大而变得多起来, 如果漏掉任何一个参数,都会造成系统运行出来的结果不正确,或者系统不能正常工作等问题 因此会有多环境管理,通过把系统一些参数抽象出来,然后定义多个环境参数文件, 文件定义环境特有的一些数据值 然后在打包过程中,使用目标环境变量参数替换预定义的系统参数 打包后,直接部署到目标环境即可保证正确运行减少了脑力、人力工作,现在小编就来说说关于idea 使用本地maven仓库?下面内容希望能帮助到你,我们来一起看看吧!

idea 使用本地maven仓库(IntelliJIDEA结合)

idea 使用本地maven仓库

为什么需要多环境

在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打包。