Reputation: 918
I have spring-boot application based on maven.
I want to have h2 database as dependency only for tests so I have it as follows:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
then I need one maven profile for development which needs h2 as compile or runtime dependency:
<profile>
<id>emb</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<!-- Using embedded database in development -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>
But it still failing on "Cannot load driver class: org.h2.Driver" as it is using test scope only.
When I removed test scope specification from dependency it works but it is not what I want since I don't want to have in production.
Any possibility how to rewrite dependency scope based on profile?
Upvotes: 6
Views: 10658
Reputation: 2931
Instead of having 2 profiles, you can just add default value directly in property section.
<properties>
<h2.scope>test</h2.scope>
</properties>
Dependency:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>${h2.scope}</scope>
</dependency>
And in a custom profile called h2
I change the dependency scope.:
<profile>
<id>h2</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<h2.scope>compile</h2.scope>
</properties>
</profile>
For people who are using Intellij IDEA IDE:
You need to tick the profile in maven tab as well and not just in edit configuration
Upvotes: 1
Reputation: 918
Finally I found out that dependency scope override is working correctly.
Its enough to run maven build with selected profile like this:
mvn clean install -P emb
You can also check dependency scopes with:
mvn -P emb dependency:analyze
Problem was with running spring-boot application:
When I used eclipse/idea or
mvn spring-boot:run
it fails on missing h2 driver - probably because of running another build without proper profile.
Solution is to run spring-boot app after maven build with simple java:
java -jar target/your-app.jar --spring.profiles.active=emb
(spring profile in this case is another story - adding just for full info)
Upvotes: 4
Reputation: 7988
The profile tag can be at any level in you pom, Just set 2 sets of properties.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.greg</groupId>
<artifactId>profile-boot-example</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<profiles>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<h2.scope>test</h2.scope>
</properties>
</profile>
<profile>
<id>emb</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<h2.scope>compile</h2.scope>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>${h2.scope}</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Upvotes: 13