When you write a Gradle build file, you are actually writing Groovy code. We don't need to get too deep into Groovy and the way it implements Domain Specific Languages (DSLs), but when you declare a dependency, you are actually calling a method:
Code:
providedCompile([group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'])
The argument there is a Map (just like a Java Map) with three keys "group, name and version". You can declare your dependency like this and it will work.
Groovy has a few shortcuts that you can use to make the syntax more readable. Let's look at these step by step:
1. Leave out the square brackets for the map. This is how Groovy provides named parameters:
Code:
providedCompile(group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0')
2. Leave out the parentheses for the function call
Code:
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
3. Gradle then allows you to just pass a string, where the group, name and version are delimited by ':'. I couldn't find the relevant source code but I assume this string is broken up using a string tokenizer and converted to a map.
Code:
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
When you look for dependencies on mvnrepository.com, it will give you the form in (2) and make some assumptions about how you would declare the dependency, i.e. "provided".
Code:
provided group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
Provided means the JARs are provided by something outside your project, e.g. Tomcat. So they won't be packaged into your WAR file. Because some APIs, e.g. the servlet API have compile-time dependencies (the Java interfaces) and runtime dependencies (the implementations of those interfaces), the Gradle WAR plugin distinguishes them using "providedCompile" and "providedRuntime". The "providedCompile" dependencies are needed to compile your project. The "providedRuntime" are needed when Gradle runs your project.
It is confusing, I know. The suggestion on mvnrepository.com is not how it works with the Gradle WAR plugin. You need "providedCompile".
In your case, you want the servlet API to be used during compilation, so you can use either:
Code:
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
or
Code:
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
You don't need a "providedRuntime" for the servlet API because that is handled by your Tomcat plugin.
You figured out what you needed to do. The above explanation is just there to fill in a few gaps and you don't need to understand it to use Gradle.
Bookmarks