Correct way to declare multiple scope for Maven dependency?

Not sure if this would still help someone who is still looking for a simple way to do this - https://howtodoinjava.com/maven/maven-dependency-scopes/ this link helped me add the correct scope. Here is the summary of mapping of scopes and the phases where we need the dependencies.

  1. compile - build, test and run
  2. provided - build and test
  3. runtime - test and run
  4. test - compile and test

So, when I needed the dependency during test and runtime, I gave the scope as "runtime" and it worked as expected.


You can only define one scope value per <scope/> tag.

I'm afraid what you'd like to do cannot be achieved by merely using a scope. If you define a scope of test, it will only be available during tests; if you define a scope of provided, that would mean that you would expect that dependency for your project to be resolved and used during both compilation and tests, but it will not be included in your WAR file. Either way, it's not what you would want.

Therefore, I would recommend you have a look at the maven-assembly-plugin, with which you can achieve it, but it will still require some playing around.


The runtime scope also makes the artifact available on the test classpath. Just use runtime. (See the Maven documentation.)

To avoid having the dependency resolved transitively, also make it optional with <optional>true</optional>:

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback</artifactId>
  <version>0.5</version>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>

Tags:

Maven

Maven 3