具有多环境配置的Maven应用程序无法在tomcat上部署 [英] maven application with multi environment configuration can't deploy on tomcat
问题描述
我正在尝试使用带有Maven的Spring Boot为我的Web应用程序配置多部署环境.在src/main/resources/config下创建了几个.properties文件. db-dev.properties和db-prod.properties由db特定信息组成:
I am trying to configure multi deployment environment for my web application using Spring Boot with Maven. Created several .properties files under src/main/resources/config. db-dev.properties and db-prod.properties consist of db specific information:
db.url=jdbc:oracle:thin:@ldap://dev.com/risstg3,
db.username=owner
db.password=godzilla
在同一目录中,我还具有application.properties,该属性读取这些数据库属性文件中定义的变量
In the same directory I also have application.properties which reads in the variables defined in these db property files
#database info
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=${db.url}
spring.datasource.username=${db.username}
spring.datasource.password=${db.password}
#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
在我的pom中设置了多个配置文件:
Multiple profiles are set up in my pom:
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>src/main/resources/config/db-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources/config</directory>
<filtering>true</filtering>
</resource>
</resources>
Spring Boot会在我的应用程序类中使用@SpringBootApplication批注来处理所有其他配置.
All other configuration is taken care of by Spring Boot with @SpringBootApplication annotation in my application class.
然后我通过使用-P选项指定配置文件来建立战争mvn install -Pprod
.但是由于以下错误,我无法使用tomcat在本地计算机上部署它:
Then I built the war by specifying profile using -P option, e.g. mvn install -Pprod
. However I failed to deploy it with tomcat on local machine due to the following error:
SEVERE: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ristoreService]]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
我以为我使用spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
基于此线程,此错误可能不一定与休眠方言有关.如果数据库连接不成功,您可能会看到它.我错过了什么?有没有办法判断战争是否使用正确的配置文件创建,以及是否获取了db- {dev} .properties中定义的变量?
Based on this thread, this error may not necessarily have to do with hibernate dialect. You may see it if the db connection is not successful. What did I miss? Is there a way to tell whether the war is created with the correct profile and whether the variables defined in db-{dev}.properties are picked up?
推荐答案
这与您的设置略有不同,但我认为这将以更友好的Spring方式帮助解决问题. Spring具有配置文件的概念.您可以将application.properties
文件创建为默认设置,然后将application-${profile}.properties
用于您的配置文件特定设置.如果创建application-dev.properties
和application-prod.properties
,则只需指定一个名为spring.profiles.active=dev
的环境变量,它将使用这些变量.这样,您无需为每种部署类型创建单独的jar文件.
This is a slight variation to your setup but I think would help solve the problem in a more Spring friendly way. Spring has the concept of profiles. You can create application.properties
files as your default settings and then have application-${profile}.properties
for your profile specific settings. If you create an application-dev.properties
and an application-prod.properties
all you need to do is then specify an environment variable called spring.profiles.active=dev
and that would use those. This way you don't need to create separate jar files for each type of deployment.