小编典典

具有多环境配置的Maven应用程序无法在tomcat上部署

tomcat

我正在尝试使用带有Maven的Spring Boot为我的Web应用程序配置多部署环境。在src / main / resources /
config下创建了几个.properties文件。db-dev.properties和db-prod.properties由db特定信息组成:

db.url=jdbc:oracle:thin:@ldap://dev.com/risstg3, 
db.username=owner
db.password=godzilla

在同一目录中,我还有application.properties,它读取这些db属性文件中定义的变量

#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中设置了多个配置文件:

 <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批注来处理所有其他配置。

然后,我通过使用-P选项指定配置文件来建立战争mvn install -Pprod。但是由于以下错误,我无法使用tomcat在本地计算机上部署它:

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

我以为我在application.properties中设置了“ hibernate.dialect” spring.jpa.database- platform=org.hibernate.dialect.Oracle10gDialect

基于此线程,此错误可能不一定与hibernate方言有关。如果数据库连接不成功,您可能会看到它。我错过了什么?有没有办法判断战争是否使用正确的配置文件创建,以及是否获取了db-{dev} .properties中定义的变量?


阅读 260

收藏
2020-06-16

共1个答案

小编典典

这与您的设置略有不同,但我认为这将以更友好的Spring方式帮助解决问题。Spring具有配置文件的概念。您可以创建application.properties文件作为默认设置,然后具有application-${profile}.properties针对您的配置文件的特定设置。如果创建application- dev.properties和,则application- prod.properties只需指定一个名为的环境变量spring.profiles.active=dev,它将使用这些变量。这样,您无需为每种部署类型创建单独的jar文件。

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-
properties-and-configuration.html#howto-change-configuration-depending-on-the-
environment

2020-06-16