使用maven的profile区分本地环境和线上环境
多环境开发,使用maven-profile,就可以在打包的时候通过参数的调整,最终打的包也不同。
以区分本地数据库和线上数据库为例
比如测试环境,用的是本地测试数据库;生产环境用的是线上生产库
针对不同库的url,userName,password,就可以配两套,通过profile区分,然后打包的时候就可以区分了
具体使用:
1.在pom.xml中,配置如下:
<project XXXXXXXX>......其他配置<dependencies></dependencies><profiles><profile><id>local</id><properties><profiles.active>local</profiles.active></properties></profile><profile><id>production</id><properties><profiles.active>production</profiles.active></properties></profile></profiles>
</project>
2.在配置加载properties文件的时候,做区分
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"><property name="locations"><list><value>classpath:application-${profiles.active}.properties</value></list></property></bean>
3. 新建两个properties文件,分别配置:
application-local.properties(以后本地配置的信息都在这里)mysql.url=本地url;mysql.userName=admain;mysql.password=admain;application-production.properties(以后线上配置的信息都在这里)mysql.url=线上url;mysql.userName=线上用户名;mysql.password=线上密码;
这里的local和production与pom.xml中的profiles.active标签的值一一对应
4.配置dataSource
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="${mysql.url}" /><property name="username" value="${mysql.user}" /><property name="password" value="${mysql.password}" />
</bean>
5.打包
使用maven package -P local打包,那么最终数据源就会使用本地的
使用maven package -P production打包,那么最终数据源就会使用线上的
这里的local和production与pom.xml中的id标签的值一一对应