Tim Driven Development | Test until the fear goes away
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Suhaas Parekh wrote:4. When exactly would I use a compile scope, which is also the default scope? Please explain with example.
Scope | When to use |
---|---|
provided | Like Tim said, when the dependency is provided by the platform or server. His JEE example is one of the most common cases. |
runtime | This is the opposite of provided. Where provided dependencies can be used to compile against but are not part of your application, runtime dependencies are part of your application but you cannot use them to compile. So when do you use it? Almost never. The only example I can think of is database drivers; you use JDBC (or a layer on top of it like JPA) to compile against, not the driver itself. Note that you should only do this for self-contained applications, like Spring Boot. For JEE containers, the container should be configured with the data source, not the application itself. |
test | Again, like Tim said, just for testing. The most common test dependencies are JUnit, Assert4J, Mockito, and other dependencies that just allow you to build your tests. Other test dependencies can be implementations of specifications. For instance, you have a provided dependency on the JPA specification, and a test dependency on an implementation like Hibernate. |
system | When you have an external JAR file that cannot be downloaded as a different type of dependency. This is like provided (as in, it doesn't get automatically bundled in your application) but you have to link to the JAR file manually. You should only use this if you cannot upload the JAR file to a Maven repository like Nexus. |
import | When you have a different POM and you want to include all of its dependencies. A good example is spring-boot-dependencies. If you have a dependencyManagement block, and in it add a dependency with type import, all of the Spring Boot dependencies can be used without having to specify the versions. That's because these come from the imported POM. I would (almost) never use this scope outside of dependencyManagement, because it will really import everything. If you add spring-boot-dependencies in the regular dependencies, your application will get everything that Spring Boot offers. That's a lot, and your application will be unnecessarily large. |
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:
...You should only use this if you cannot upload the JAR file to a Maven repository like Nexus.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Rob Spoor wrote:This is the opposite of provided. Where provided dependencies can be used to compile against but are not part of your application, runtime dependencies are part of your application but you cannot use them to compile. So when do you use it? Almost never. The only example I can think of is database drivers; you use JDBC (or a layer on top of it like JPA) to compile against, not the driver itself.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Consider Paul's rocket mass heater. |