【问题标题】:maven application with multi environment configuration can't deploy on tomcat具有多环境配置的maven应用程序无法在tomcat上部署
【发布时间】:2016-12-29 17:38:23
【问题描述】:

我正在尝试使用带有 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 中使用 spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect 设置了“hibernate.dialect”

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

【问题讨论】:

  • 在您的 db-prod.properties 文件中,连同您已经设置的默认属性,尝试使用 dev 再次在同一文件中添加相同的属性。和产品。前缀。点赞prod.db.url=jdbc:oracle:thin:@ldap://dev.com/risstg3, prod.db.username=owner prod.db.password=godzilla 那就试试看吧。
  • 您是否有任何理由不利用 Spring 拥有配置文件的能力,这样您就不必担心进行 maven 过滤?
  • @ShawnClark 当我在 Eclipse 中使用 JVM 参数 -Dspring.profiles.active=xxx 在环境之间切换时启动应用程序时,我使用 Spring 活动配置文件进行了不同的设置,如果这就是您所指的。但是我需要把它打包成一个war,没办法要求不同的war取不同的JVM选项。见我的另一个post

标签: hibernate tomcat spring-boot war maven-profiles


【解决方案1】:

这与您的设置略有不同,但我认为这将有助于以更适合 Spring 的方式解决问题。 Spring有profile的概念。您可以创建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

【讨论】:

  • 当我使用 JVM 参数 -Dspring.profiles.active=xxx 在 Eclipse 中启动应用程序以在环境之间切换时,我使用 Spring 活动配置文件进行了不同的设置。但是,现在我需要将它打包为一个战争,没有办法要求不同的战争采取不同的 JVM 选项,除非你每次都在运行中 export 它。见我的另一个post
猜你喜欢
  • 2021-12-16
  • 1970-01-01
  • 2013-11-03
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
相关资源
最近更新 更多