Issue
I'm trying to make a servlet application with gradle as build system. The problem is that gradle cannot find javax.servlet, I'm getting this error:
Could not find javax.servlet:javax.servlet-api:4.0
I downloaded Tomcat9 from the official site and unpacked it to ~/opt/tomcat9. There is ~/opt/tomcat9/lib folder, where servlet-api.jar is located. I guess that jar file is what gradle looking for, but how do I specify the path to it?
Solution
We usually add the Servlet API as an compileOnly dependency to our projects. This way gradle will use the JAR for compiliing, but not include it in deployments. At deployment, your web container will provide the Servlet API.
Add something like this to your dependencies declaration:
compileOnly 'org.apache.tomcat:tomcat-servlet-api:9.0.37'
Using the Jar from your local Tomcat would be theoretically possible, but I'd avoid non-metadata dependencies as it is usually simpler to have everything "in one place".
That said, adding a local directory that contains JARs to your build would look something like
repositories {
flatDir {
dirs 'path/to/tomcat/libs'
}
}
Answered By - Roland Kreuzer
Answer Checked By - Timothy Miller (JavaFixing Admin)