springboot multi-environment configuration (pom configures Profiles variables to control the packaging environment)

https://wenku.baidu.com/view/2f531c3c68d97f192279168884868762caaebbd3.html

1. Functional description

Sometimes, a project needs to adapt to multiple development environments, such as different databases (mysql, oracle, db2, etc.), such as different development environments (dev, pro, test), etc. Different environments need to specify different configurations. In this case, we can use configuration profiles to control. Specify a different configuration combination at startup, and maven will automatically select the specified configuration when building.
insert image description here

2. Specific configuration and details

1. First configure the Profiles configuration in the pom

	<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>

The variable properties attribute in pom can be referenced in application.yml in springboot, and the reference method is @variable@

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

Create configuration files for different environments and distinguish them in the form of directories (of course, they can also be named and distinguished with different beginnings)

The following is the directory structure of multi-environment configuration
insert image description here
The following is the configuration of multi-database, which is configured in each environment applicationinsert image description here

@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;
    }
}

3. Packing and filtering

Resource filtering under the pom.xml build tag

<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>

Configure profiles multi-environment package filtering

<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>

The same interface, different implementations can be realized through the same name and annotationinsert image description here

There are two implementations of the same interface, and the same name is used, so that an error will be reported at startup (duplicate aliases), so it is necessary to exclude unused directories and not compile them into classes. Using idea here will only affect springboot
insert image description here
startup When it is packaged, it is not affected by this.

Specifying a profile when packaging can exclude resource files, java code, and jars in different environments, and minimize packaging.

Guess you like

Origin blog.csdn.net/munangs/article/details/126671396