0

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

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

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>

All other configuration is taken care of by Spring Boot with @SpringBootApplication annotation in my application class.

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

I thought I set 'hibernate.dialect' in application.properties with spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

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?

Community
  • 1
  • 1
ddd
  • 4,665
  • 14
  • 69
  • 125
  • In your db-prod.properties file, along with the default properties you have set already, try adding same properties in same file again with dev. and prod. prefixes. Like `prod.db.url=jdbc:oracle:thin:@ldap://dev.com/risstg3, prod.db.username=owner prod.db.password=godzilla` Then try if it works. – jarvo69 Aug 23 '16 at 04:17
  • Is there any reason you aren't leveraging Spring's ability to have profiles so you don't have to worry about doing the maven filtering? – Shawn Clark Aug 23 '16 at 04:26
  • @ShawnClark I had a different setup using Spring active profiles when I launched the application inside Eclipse using JVM argument `-Dspring.profiles.active=xxx` to switch between environment, if that is what you are referring to. However, I need to package it as a war, there is no way to ask different war to take different JVM options. See my other [post](http://stackoverflow.com/questions/39081945/how-to-specify-spring-profiles-active-when-i-run-mvn-install) – ddd Aug 23 '16 at 14:04

1 Answers1

1

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.

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

Shawn Clark
  • 3,330
  • 2
  • 18
  • 30
  • I had a different setup using Spring active profiles when I launched the application inside Eclipse using JVM argument -Dspring.profiles.active=xxx to switch between environments. However, now I need to package it as a war, there is no way to ask different war to take different JVM options unless you `export` it every time on the fly. See my other [post](http://stackoverflow.com/questions/39081945/how-to-specify-spring-profiles-active-when-i-run-mvn-install) – ddd Aug 23 '16 at 14:06