Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle dependency scopes

I was experimenting a little bit with Gradle but as mainly being a Maven user, some dependency scopes are confusing me …

Some are identical to Maven:

compile -> compile
runtime -> runtime
compileOnly -> provided

but I also encounter implementation which sounds like the parent element of Maven POM but then again also not.

Can somebody explain me what is implementation and some other if they exist and I didn’t mention here (test versions of above are clear no need to explain)?

And if implementation is not like parent on Maven, how can we have the parent POM effect of Maven in Gradle?

like image 965
posthumecaver Avatar asked Apr 05 '19 06:04

posthumecaver


People also ask

What is scope of a dependency configuration?

Dependency Scope. Dependency scope is used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath. This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project.

What is the difference between api and implementation scopes in Gradle?

The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.

What is runtimeOnly in Gradle?

runtime and runtimeOnly are for declaring the dependencies. To use the dependencies you should use the configuration runtimeClasspath as per the docs at https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph.


1 Answers

As commented, please have a look at the documentation or even at this recent webcast (disclaimer: I am co-presenting that webcast)

As for the Maven comparison, view migrating / learning from Maven to Gradle the same as moving from subversion to git: while some vocabulary is the same, understanding the model of the later helps more than comparing.

In short:

  • Do not use compile or runtime in Gradle, they are deprecated.
  • implementation relates to dependencies that are required to compile and run your application.
  • compileOnly and runtimeOnly should be self-explanatory in the context of the above
  • The java-library plugin adds the api configuration which is reserved for dependencies that consumers of your library will need to compile.

There is no direct equivalent to a Maven parent pom. Whether you are talking about plugins, build config or dependencies, the way to centralize are different. Have a look at the documentation on multi-project authoring.

like image 67
Louis Jacomet Avatar answered Oct 24 '22 23:10

Louis Jacomet