1

is it possible to have a dependency in my Maven pom, which will only be used locally in my development environment?

I need a dependency to PostgreSQL when developing locally in my IDE, since I am using Postgres only locally. On T, A and P environments we are using a different data source, so we do not want the dependency to postgres in the build there.

It is a Spring Boot application.

Looking for a solution,

Cheers! Kjeld

Kjeld
  • 444
  • 4
  • 14
  • 4
    You could use a separate [profile](https://maven.apache.org/guides/introduction/introduction-to-profiles.html). – Turing85 Dec 31 '20 at 14:47
  • 1
    @Turing85 you can make it into an answer, add an example of enabling different Spring Profiles via Maven profiles and using `application-local.properties`, or just link to https://stackoverflow.com/questions/32196451/environment-specific-application-properties-file-in-spring-boot-application – Pavlus Dec 31 '20 at 14:57
  • Usually you define such things as test scoped dependency... – khmarbaise Jan 01 '21 at 19:48
  • @khmarbaise No I need it in my regular build run locally, not in my test scope. I think the profile hint by Turing85 is what I was looking for. Thanks! – Kjeld Jan 02 '21 at 12:26
  • If you don't use it in your tests why having it at all? If you don't test it does not exist... locally different than tests is a smell.. – khmarbaise Jan 02 '21 at 13:21
  • @khmarbaise On DTAP we run against SAP HANA database. Locally we are forced to run against PostgreSQL instead. Spring Boot needs the dependency to work with PostgreSQL, only locally. A dependency is not part of the application, and never a unit we have to test. We are also not implementing a integration test for it exactly because of the db difference on different environments. The dependencies that are used in test scope are those of the tools we need for testing (like JUnit, Mockito, ...). I do not need the Postgres dep to live in test scope, but in main scope, only locally. – Kjeld Jan 02 '21 at 18:34
  • 1
    Now it makes even less sense...why in main scope? Local development should be done by tests and CI should use tests as well... so what are you testing with Postgres if you don't use it? Maybe you can try to define it as scope:runtime... – khmarbaise Jan 02 '21 at 18:36
  • @khmarbaise We develop by running the main Spring Boot code locally with Postgres, since HANA is not available on our local machines. There is no golden rule that says local dev should be done by tests? I might choose to do that if I was developing some service that won't be run on my local dev environment. We use unit tests to secure the quality of our application, also during the CI builds. I will also try the runtime scope, I have not tried any solution yet. – Kjeld Jan 02 '21 at 23:13

1 Answers1

1

I have solved this by using the runtime scope for this particular dependency (thanks @khmarbaise for pointing that out). I think I was asking for the sake of asking, and I could have thought of this if I had thought about it a little more before asking the question... Then again, this might also help others. The profile suggestion might have been even better, but I have not looked into that anymore.

    <!-- FOR LOCAL IDE DEVELOPMENT ONLY -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
Kjeld
  • 444
  • 4
  • 14
  • Runtime scope dependencies are not limited to local IDE development. Runtime dependencies will be included in your build artifact (JAR, WAR, etc.), so they will make it to production. Runtime dependencies are just only available at runtime and not during compilation. See Maven's Dependency Scope documentation for more details: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope – Nils Breunese Aug 11 '23 at 13:17