在实际的开发中,如果需要
开发的时候,一套配置、测试的时候,一套配置、上线的时候又是一套配置。彼此修改,互不干涉
此时,就可以使用maven的profile
具体做法如下:
一:新建一个maven项目
二:在resources目录里面,新建dev、test、production三个目录,代表三种环境的配置文件,项目结构图如下:
三:分别在dev, test, production里面新建文件:application.properties,分别配置好各自的环境
比如说我这里的配置为:
dev/application.properties
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.0.0.1/ods_sdp_development
test/application.properties
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://10.4.18.100/ods_sdp_test
production/application.properties
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://10.4.18.4/ods_sdp_production
四:配置pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lala</groupId> <artifactId>profile</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>profile</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> <profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <!-- 测服 --> <profile> <id>test</id> <properties> <env>test</env> </properties> </profile> <!-- 生产 --> <profile> <id>production</id> <properties> <env>production</env> </properties> </profile> </profiles> <build> <resources> <resource> <directory>${project.basedir}/src/main/resources/${env}</directory> <includes> <include>*.*</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>${project.basedir}/src/main/resources/</directory> <includes> <include>logback.xml</include> </includes> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
com.lala.profile.App
package com.lala.profile; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class App { public static void main( String[] args )throws Exception { ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream input = cl.getResourceAsStream("application.properties"); InputStreamReader isr = new InputStreamReader(input); BufferedReader br = new BufferedReader(isr); String str = null; while( (str = br.readLine()) != null) { System.out.println(str); } br.close(); isr.close(); input.close(); } }
如果要测试 测试环境,则
mvn clean
mvn package -Ptest
[ly@lypc profile]$ java -cp target/profile-0.0.1-SNAPSHOT.jar com.lala.profile.App
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://10.4.18.100/ods_sdp_test
生产环境测试
mvn clean
mvn package -Pproduction
[ly@lypc profile]$ java -cp target/profile-0.0.1-SNAPSHOT.jar com.lala.profile.App
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://10.4.18.4/ods_sdp_production