Oleg Shankovskyi Oleg Shankovskyi - 19 days ago 13
Java Question

Problems with defining profiles in pom.xml (Spring Boot)

I have a Spring Boot application with two profiles: 'dev' and 'prod'. In 'dev' profile should be used H2 database, in 'prod' — PostgreSQL.

They are defined in pom.xml as following:

<profiles>
<profile>
<id>prod</id>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1211.jre7</version>
</dependency>

</dependencies>
</profile>

<profile>
<id>dev</id>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>


No other databases are in pom.xml defined.

In my application.properties file 'prod' profile is defined as active:

spring.profiles.active = prod


Also I have two specific properties files for each profile:

application-dev.properties:

spring.jpa.show-sql = true


and application-prod.properties (I deploy my app to AWS):

spring.datasource.url = jdbc:postgresql://${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME}
spring.datasource.username = ${RDS_USERNAME}
spring.datasource.password = ${RDS_PASSWORD}
spring.jpa.hibernate.ddl-auto = update


I start my IntelliJ IDEA with following VM-option:

-Dspring.profiles.active="dev"


When I start the program locally, I see that 'dev' profile is used (there is message in console "The following profiles are active: dev"), but H2 doesn't start. I become following error message:

***************************
APPLICATION FAILED TO START
***************************

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath.
If you have database settings to be loaded from a particular profile you may need
to active it (the profiles "dev" are currently active).


Please help me to define these profiles correctly.

Answer

Cannot determine embedded database driver class for database type NONE

spring.datasource.url = jdbc:h2:mem:yourDB
spring.datasource.driverClassName = org.h2.Driver

added in application-dev.properties file should solve your problem.

Edit

Maven profile is not Spring Boot profile.
when you run your project, enable also the maven profile with -P arg:

  mvn spring-boot:run -Pdev -Dspring.profiles.active="dev"