Override maven dependency scope in profile
Asked Answered
M

3

6

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?

Maighdiln answered 2/7, 2018 at 12:3 Comment(0)
M
4

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)

Maighdiln answered 3/7, 2018 at 7:54 Comment(1)
This one --spring.profiles.active=emb is good by the way! :)Conjunct
C
13

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>
Clarino answered 2/7, 2018 at 12:33 Comment(3)
It looks to big gun for just one exception of "standard" properties, that's way I am finding some solution to override the scope.Proprietor
thanks, looks much better. I will try it tommorow and let you know.Proprietor
If I only have emb profile then it works but if I have both prod and emb profiles then the emb profile have the error that h2 driver cannot be loadedSmelter
M
4

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)

Maighdiln answered 3/7, 2018 at 7:54 Comment(1)
This one --spring.profiles.active=emb is good by the way! :)Conjunct
S
1

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 enter image description here

Smelter answered 2/9, 2022 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.