根據網上的介紹可以使用maven 的profile功能實現不同環境properties文件的配置,
這裏我使用filters方式,具體就是:
在application-test.properties文件中添加
- #這是test環境配置
- jdbc_url=jdbc:mysql://192.168.1.180:3306/abcdev
- jdbc_user=test_user
- jdbc_password=test_password
在application.properties中添加
username=${db_username}
password=${db_password}
然後在pom文件中配置
<profiles> <profile> <id>test</id> <properties> <env>sit</env> </properties> </profile> </profiles> <build> <filters> <filter>src/main/resources/application/application-${env}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>application/**</exclude> <exclude>config/**</exclude> </excludes> <filtering>true</filtering> </resource> </resources> </build>
最後執行 mvn clean package -P test,就可以修改application.properties中的屬性,實現多環境打包。
但是在使用時application.properties中的屬性值死活改不了,然後我新建了一個maven項目在新環境中可以執行成功,真實奇了怪了。我就開始比較兩個pom文件的差別,發現老項目使用了spring boot,在pom文件中配置了parent標籤
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> </parent>
而點開parent的pom文件可以看到已經配置了resource標籤
感覺這裏可能有衝突,於是使用決定去掉parent標籤,改用
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.10.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
再次執行mvn clean package -P test,屬性設置成功。
看來parent項目中的resource配置會影響到子項目resource的配置,多加註意。