Reputation: 62555
I'm using Spring Boot / JPA / Hibernate. I would like to use HSQLDB when testing on localhost and MySQL when deploying on server.
In pom.xml, I have this dependency for MySQL :
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
and this dependency for HSQLDB :
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
I probably can't put both dependencies in pom.xml due to Spring Boot autoconfiguration. But I would like to use HSQLDB when the local
Spring profile is active and use MySQL when the server
Spring profile is active. Ideally, I would like to generate only one war file and running the local version with mvn spring-boot:run
and deploy the server version from the war file.
Any idea?
P.S.: I'm using maven
Upvotes: 2
Views: 1187
Reputation: 1066
You need to create a Maven Profile not a spring profile and add the dependency accordily. Create a local profile with hsqldb dependency and activate it while in dev mode. Then create a server profile with the mysql dependency and activate it while packaging for deploy on server. I personally prefer dev and release as a naming convention ( but it is my humble opinion). Then you have to shortcut the maven profile with the spring profile
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>xyz</version>
<configuration>
<profiles>
<profile>local</profile>....
In local profile and
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
<configuration>
<profiles>
<profile>server</profile>...
in server profile and the run the desired configuration with this switch
mvn spring-boot:run -P[local or server]
Introduction to Build Profiles
Upvotes: 3
Reputation: 33111
I probably can't put both dependencies in pom.xml due to Spring Boot autoconfiguration.
Sure you can have both of them. There are several solutions.
The most basic one is to have both dependencies and create an application-server.properties
in your project with the settings of your mysql database. When you run the app with no particular profile, Spring Boot won't find any information to connect to MySQL so it'll fallback to HSQL. When you deploy your app on the server, make sure to enable the server
profile. The same artifact can be used for both use cases.
You could improve that solution a bit by excluding the hsql dependency when you generate the fat jar. This has the advantage that if you forget to enable the profile you'll get an exception on startup rather than silently use an in-memory databae. To exclude hsql, simply configure the maven plugin. You'll be able tot run the app locally (mvn spring-boot:run
) or in your IDE and the fat jar will only contain the mysql dependency. You'll still have to enable the server
profile though.
If you're using a war, you can't use that last solution: one more reason to switch to jar ;-)
Upvotes: 5