Idea工具使用Maven多Profile运行
示例如下:
SpringBoot在两个Profile下分别连接两个不同的数据库PostgreSQL和MySQL
Maven在两个Profile下分别依赖PostgreSQL和MySQL的驱动
数据库及数据准备见:https://blog.csdn.net/liwenyang1992/article/details/120311490
application.properties文件如下:使用@变量@作为配置占位符
#业务变量,观察打包是否被替换
[email protected]@
server.port=8080
server.servlet.context-path=/maven
#MyBatis配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*.xml
#数据库连接配置
[email protected]@
spring.datasource.url= @datasource.url@
[email protected]@
[email protected]@
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文件如下:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.11version>
<relativePath/>
parent>
<groupId>com.lwy.itgroupId>
<artifactId>maven-profileartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>maven-profilename>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<profiles>
<profile>
<id>mysqlid>
<properties>
<project.environment>mysqlproject.environment>
<datasource.url>jdbc:mysql://localhost:3306/goodsdb?allowMultiQueries=true&serverTimezone=GMT&characterEncoding=UTF-8datasource.url>
<datasource.username>rootdatasource.username>
<datasource.password>123456datasource.password>
<datasource.driver>com.mysql.cj.jdbc.Driverdatasource.driver>
properties>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
dependencies>
profile>
<profile>
<id>pgsqlid>
<properties>
<project.environment>pgsqlproject.environment>
<datasource.url>jdbc:postgresql://localhost:5432/goodsdb?sslmode=disabledatasource.url>
<datasource.username>postgresdatasource.username>
<datasource.password>123456datasource.password>
<datasource.driver>org.postgresql.Driverdatasource.driver>
properties>
<dependencies>
<dependency>
<groupId>org.postgresqlgroupId>
<artifactId>postgresqlartifactId>
<scope>runtimescope>
dependency>
dependencies>
profile>
profiles>
<build>
<resources>
<resource>
<filtering>truefiltering>
<directory>src/main/resourcesdirectory>
resource>
resources>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
占位符变量替换生效,则必须添加:
<resources>
<resource>
<filtering>truefiltering>
<directory>src/main/resourcesdirectory>
resource>
resources>
若使用Maven插件切换Profile,则无需添加默认profile,否则插件切换不生效。
<activation>
<activeByDefault>trueactiveByDefault>
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)]
改进点
由于数据库配置信息密级等级较高,即使是本地测试也不适合写在pom.xml文件中,所以通过启动时传递jvm参数的方式进行修改:
application.properties
#业务变量,观察打包是否被替换
[email protected]@
server.port=8080
server.servlet.context-path=/maven
#MyBatis配置
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:mapper/*.xml
#数据库连接配置
[email protected]@
spring.datasource.url= @datasource.url@
[email protected]@
spring.datasource.password=
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.11version>
<relativePath/>
parent>
<groupId>com.lwy.itgroupId>
<artifactId>maven-profileartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>maven-profilename>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<profiles>
<profile>
<id>mysqlid>
<properties>
<project.environment>mysqlproject.environment>
<datasource.url>jdbc:mysql://localhost:3306/goodsdb?allowMultiQueries=true&serverTimezone=GMT&characterEncoding=UTF-8datasource.url>
<datasource.username>rootdatasource.username>
<datasource.driver>com.mysql.cj.jdbc.Driverdatasource.driver>
properties>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
dependencies>
profile>
<profile>
<id>pgsqlid>
<properties>
<project.environment>pgsqlproject.environment>
<datasource.url>jdbc:postgresql://localhost:5432/goodsdb?sslmode=disabledatasource.url>
<datasource.username>postgresdatasource.username>
<datasource.driver>org.postgresql.Driverdatasource.driver>
properties>
<dependencies>
<dependency>
<groupId>org.postgresqlgroupId>
<artifactId>postgresqlartifactId>
<scope>runtimescope>
dependency>
dependencies>
profile>
profiles>
<build>
<resources>
<resource>
<filtering>truefiltering>
<directory>src/main/resourcesdirectory>
resource>
resources>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
编辑启动项,添加spring.datasource.password的值,如图
重新clean compile ,运行
访问http://localhost:8080/maven/list
输出:
[BookVO(bookId=1, bookName=格林童话, bookPrice=100.01), BookVO(bookId=2, bookName=PostgreSQL实战, bookPrice=66.66)]