我成功地将 Maven 配置文件链接到 Spring 配置文件。下面我解释一下我是怎么做的:
1 - 创建 Maven 配置文件:
在 pom.xml 中,我确定了我的 maven 配置文件,稍后会将它们链接到 spring 配置文件,方法是将它们存储在“spring.profiles.to.activate”属性中:
<!-- PROFILES -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.to.active>dev</spring.profiles.to.active>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<spring.profiles.to.active>uat</spring.profiles.to.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.to.active>prod</spring.profiles.to.active>
</properties>
</profile>
</profiles>
2 - 激活 Maven 过滤:
我通过添加 maven-war-plugin 来构建文件夹 ${basedir}/src/main/webapp 中的过滤功能。
这将允许我们在提到的文件夹中解析占位符 ${...}(在这种特殊情况下为 ${spring.profiles.to.activate})。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resources>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resources>
</webResources>
</configuration>
</plugin>
3- 激活配置文件 Spring
在 appengine-web.xml 中声明系统属性:“spring.profiles.active”作为 maven 属性 ${spring.profiles.to.activate}
<appengine-web-app
xmlns="http://appengine.google.com/ns/1.0">
<version>1</version>
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
<system-properties>
<property name="spring.profiles.active" value="${spring.profiles.to.active}" />
</system-properties>
</appengine-web-app>
4 - 部署到 Appengine
# Dev
mvn appengine:deploy -Pdev
# UAT
mvn appengine:deploy -Puat
#PROD
mvn appengine:deploy -Pprod