Java教程

Idea编译器Spring Profile与Maven Profile联动

本文主要是介绍Idea编译器Spring Profile与Maven Profile联动,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Idea工具使用Maven多Profile运行

示例如下:

SpringBoot在两个Profile下分别连接两个不同的数据库PostgreSQL和MySQL

Maven在两个Profile下分别依赖PostgreSQL和MySQL的驱动

数据库及数据准备见:https://blog.csdn.net/liwenyang1992/article/details/120311490

application.properties文件如下:使用@变量@作为配置占位符

#业务变量,观察打包是否被替换
business.environment=@project.environment@

server.port=8080
server.servlet.context-path=/maven

#MyBatis配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*.xml

#数据库连接配置
spring.datasource.driver-class-name=@datasource.driver@
spring.datasource.url= @datasource.url@
spring.datasource.username=@datasource.username@
spring.datasource.password=@datasource.password@

VO、Dao及Mapper文件准备见:https://blog.csdn.net/liwenyang1992/article/details/120311490

BookController文件:

package com.lwy.it.book.controller;

import com.lwy.it.book.dao.BookDao;
import com.lwy.it.book.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BookController {

    @Autowired
    private BookDao bookDao;

    @GetMapping("/list")
    public List<BookVO> findAllBook() {
        List<BookVO> list = bookDao.findAllBook();
        System.out.println(bookDao.findAllBook());
        return list;
    }
}

多profile的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.11</version>
        <relativePath/>
    </parent>
    <groupId>com.lwy.it</groupId>
    <artifactId>maven-profile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-profile</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>mysql</id>
            <properties>
                <project.environment>mysql</project.environment>
                <datasource.url>jdbc:mysql://localhost:3306/goodsdb?allowMultiQueries=true&amp;serverTimezone=GMT&amp;characterEncoding=UTF-8</datasource.url>
                <datasource.username>root</datasource.username>
                <datasource.password>123456</datasource.password>
                <datasource.driver>com.mysql.cj.jdbc.Driver</datasource.driver>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
        </profile>
      
        <profile>
            <id>pgsql</id>
            <properties>
                <project.environment>pgsql</project.environment>
                <datasource.url>jdbc:postgresql://localhost:5432/goodsdb?sslmode=disable</datasource.url>
                <datasource.username>postgres</datasource.username>
                <datasource.password>123456</datasource.password>
                <datasource.driver>org.postgresql.Driver</datasource.driver>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
    <build>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

占位符变量替换生效,则必须添加:

        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

若使用Maven插件切换Profile,则无需添加默认profile,否则插件切换不生效。

            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

测试验证:

profile选择mysql,Reimport Maven,访问http://localhost:8080/maven/list

输出:

[BookVO(bookId=1, bookName=安徒生童话, bookPrice=99.99), BookVO(bookId=2, bookName=MySQL实战教程, bookPrice=88.88)]

profile选择pgsql,Reimport Maven,访问http://localhost:8080/maven/list

输出:

[BookVO(bookId=1, bookName=格林童话, bookPrice=100.01), BookVO(bookId=2, bookName=PostgreSQL实战, bookPrice=66.66)]

切换操作如下图:
在这里插入图片描述

这篇关于Idea编译器Spring Profile与Maven Profile联动的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!