Maven替換sql中的schema解決不同環境schema不同的問題

問題:不同環境的sql腳本schema不同,導致每次都要修改sql文件。

解決方案:使用Maven命令含+環境參數,在Maven打包過程中,使用${}佔位符替換sql腳本中的schema佔位符即可。

mvn clean package -Dmaven.test.skip=true -P dev

mvn clean package -Dmaven.test.skip=true -P uat

0. 項目結構:

1. Pom 文件

<?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.2.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.test.demo</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</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.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.17</version>
		</dependency>
		
		<dependency>
		    <groupId>com.squareup.okhttp3</groupId>
		    <artifactId>okhttp</artifactId>
		    <version>4.5.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.17</version>
		</dependency>
		
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-lang3</artifactId>
		</dependency>
		
		<dependency>
		    <groupId>commons-beanutils</groupId>
		    <artifactId>commons-beanutils</artifactId>
		    <version>1.9.3</version>
		</dependency>
		
		<dependency>
		  	<groupId>commons-io</groupId>
		    <artifactId>commons-io</artifactId>
		    <version>2.4</version>
		</dependency>
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- 1. 添加maven resourcse插件,定義佔位符 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<useDefaultDelimiters>false</useDefaultDelimiters>
					<delimiters>
						<delimiter>${*}</delimiter>
					</delimiters>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
		
		<!-- 2. 定義環境對應的properties文件 -->
		<filters>
            <filter>src/main/resources/config/${env}.properties</filter>
        </filters> 
        <!-- 3. 需要打包處理的文件-->
         <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.sql</include>
                    <include>**/*.properties</include>
                    <include>**/**/*.xls</include>
                </includes>
            </resource>
        </resources>
	</build>
	<!-- 4. 添加環境profile-->
	<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <env>uat</env>
            </properties>
        </profile> 
    </profiles>
    
</project>

2. 環境properties文件

 

3. 要替換佔位符的目標文件

4. 運行命令 mvn clean package -Dmaven.test.skip=true -P dev

 

5. 查看打包編譯結果  note\target\classes\db  的db.sql文件中的佔位符已經被替換好了。

 

6. 運行命令 mvn clean package -Dmaven.test.skip=true -P uat 然後再去查看 db.sql打包的結果。

 

參考鏈接:

源碼Git地址:https://github.com/xiaowanziwuha2/note

https://www.cnblogs.com/Leechg/p/10280911.html 借鑑方案

https://www.cnblogs.com/zhaoyan001/p/8735196.html Maven命令參數文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章