目錄
- 功能描述
- 具體配置及細節
- 打包過濾
功能描述
有些時候,一個項目須要適配多種開發環境,如數據庫不一樣(mysql、oracle、db2等)、如開發環境不一樣(dev、pro、test)等不一樣的環境須要指定不一樣的配置。這種狀況下,咱們就能夠採用配置Profiles來控制。在啓動的時候指定不一樣的配置組合,maven進行build時會自動選擇指定配置。 java
具體配置及細節
- 首先配置在pom中配置Profiles配置
<profiles> <profile> <id>mysql</id> <properties> <spring.profiles.active>mysql</spring.profiles.active> </properties> </profile> <profile> <id>oracle</id> <properties> <spring.profiles.active>oracle</spring.profiles.active> </properties> </profile> <profile> <id>db2</id> <properties> <spring.profiles.active>db2</spring.profiles.active> </properties> </profile> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> </profile> <profile> <id>prd</id> <properties> <profiles.active>prd</profiles.active> </properties> </profile> </profiles>
mybatis: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: [mybatis/**/**@spring.profiles.active@**/*Mapper.xml] type-aliases-package: com.*.*.domain.entity,com.*.*.system.entity
- 創建不一樣環境的配置文件,並以目錄的形式區分(固然也能夠以不一樣的開頭命名區分)
如下是多環境配置的目錄結構
mysql
如下是多數據庫的配置,在每一個環境application中配置 spring
@Configuration public class DatasourceConfig{ @Resource Environment env; @Bean @Profile(value="mysql") public DataSource mysql(){ return dataSources("mysql"); } @Bean @Profile(value="oracle") public DataSource oracle(){ return dataSources("oracle"); } @Bean @Profile(value="db2") public DataSource db2(){ return dataSources("db2"); } /** * 獲取數據源 * @param type (mysql,oracle,db2 ....) */ public DataSource dataSources(String type) { String driverClassName = env.getProperty("mydatasource."+type+".driver-class-name"); String url = env.getProperty("mydatasource."+type+".url"); String username = env.getProperty("mydatasource."+type+".username"); String password = env.getProperty("mydatasource."+type+".password"); DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setName(type); druidDataSource.setDriverClassName(driverClassName); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); //TODO ..... return druidDataSource; } }
打包過濾
- 資源過濾 pom.xml build標籤下
<resources> <resource> <directory>src/main/resources</directory><!-- 指定資源文件夾,src/main/resources 默認打到classes下--> <!-- 默認爲false,配置爲true,則會將改資源目錄下的xml和properties文件中的引用 @配置@ 和 ${} 轉換成真實值--> <filtering>true/false</filtering> <includes><include></include></includes><!-- 指定要打包的文件或目錄(只包含資源源文件,不包括class --> <excludes><exclude></exclude></excludes><!-- 指定要過濾的文件或目錄 (只包含資源源文件,不包括class--> </resource> </resources> <!-- demo 過濾config目錄--> <resource> <directory>src/main/resources</directory> <excludes> <exclude>config/</exclude> <exclude>mybatis/</exclude> </excludes> </resource> <resource> <directory>src/main/resources/config/${package.environment}</directory> <includes> <include>config.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>mybatis/**/${spring.profiles.active}/**</include> </includes> </resource> <!-- 過濾class文件的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <exclude>com/yuyi/imap/ServletInitializer.class</exclude> </excludes> </configuration> </plugin>
- 配置profiles 多環境打包過濾
<profiles> <profile> <id>mysql</id> <properties> <spring.profiles.active>mysql</spring.profiles.active> <project.packaging>jar</project.packaging> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency> </dependencies> <build> <plugins> <!-- 過濾class文件的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <!-- 過濾目錄下文件 --> <exclude>com/yuyi/imap/oracle/*</exclude> <exclude>com/yuyi/imap/db2/*</exclude> </excludes> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>oracle</id> <properties> <spring.profiles.active>oracle</spring.profiles.active> <project.packaging>war</project.packaging> </properties> <build> <plugins> <!-- 打成war包,過濾class文件的插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <packagingExcludes> WEB-INF/classes/com/yuyi/imap/mysql/, WEB-INF/classes/com/yuyi/imap/db2/ </packagingExcludes> </configuration> </plugin> <!-- 打成jar包時生效 過濾class文件的插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <excludes> <!-- 過濾目錄 --> <exclude>com/yuyi/imap/mysql/</exclude> <!-- 過濾文件--> <exclude>com/yuyi/imap/db2/*</exclude> </excludes> </configuration> </plugin> </plugins> </build> </profile> </profiles>
- 同一個接口,不一樣的實現能夠經過重名和註解實現
同一個接口有兩種實現,而且採用同一個命名,這樣在啓動的時候會報錯(別名重複)因此須要將不用的目錄給排除掉,不編譯成class,這裏採用idea
這麼作只會影響springboot啓動的時候,打包的時候不受此影響。sql
打包時指定profile 就能夠將不一樣環境的資源文件和java代碼以及jar排除掉,最小打包。數據庫