我正在尝试使用 maven 命令激活 AppEngine 应用程序的配置文件,如下所示:
mvn appengine:deploy -Dspring.profiles.active=prod
但它被忽略了。
是否可以使用 maven 激活配置文件?
最佳答案
我成功地将 Maven 配置文件链接到 Spring 配置文件。下面我解释一下我是如何做的:
1 - 创建 Maven 配置文件:
在pom.xml中,我确定了我的maven配置文件,稍后将它们存储在“spring.profiles.to.activate”属性中,将它们链接到spring配置文件:
<!-- 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
关于java - appengine 中的配置文件激活 :deploy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53466360/