Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify dependency on tomcat libraries with gradle

A bit of background about my knowledge level: I'm currently trying to learn how to build a project with gradle. So far I don't have much experience with build tools (almost none). I do understand the general idea and have seen ant and maven files before but never written them myself. Until now I just used other peoples build scripts or configured my builds using Eclipse. So I might be on a completely wrong track here, if so please point me in the correct direction. Also, I sadly don't have much experience building jars by hand.

I have an Eclipse project which I want to compile into a jar. Required library jars are on my local file system. I managed to make gradle use these via

dependencies {
    compile fileTree(dir:'lib', include:'*.jar')
}

in addition I have my source files in src/main/java and just use apply plugin: 'java' in the build.gradle file. Trying to build the project with gradle build seems to do the right thing, as far as I can tell.

However, the library is supposed to be used in a web project running on a tomcat and makes use of some libraries that are supplied by tomcat, as far as I understand. E.g. I'm using javax.servlet.http.HttpServletRequest. The project works fine in Eclipse, but there I have the tomcat library added to my Eclipse build path. When I check in Eclipse I can see that javax.servlet.http.HttpServletRequest is part of the servlet-api.jar which is part of the Tomcat library.

Now, when I build the project I get build errors because the java compiler cannot find the class because I didn't specify the servlet-api.jar in the dependencies. I guess I could download it somehow (or learn how to specify it as an external dependency to make gradle download it from some repository) but I am not sure whether that would be correct.

Is there a way to tell gradle to use the same library that Eclipse uses? Or some other general way to tell it about all the tomcat jars, the same way I can simply add the complete Tomcat library in Eclipse? Or do I actually need another copy of these jars somehow and have to specify each one individually?

Also, do I need to add these library jars to my build-result library jar? As far as I know I need to add any jar I depend on to the resulting jar as well. But then again, I have read somewhere that some libraries are supplied by tomcat itself so they would have to be part of any war deployed on it. I'm afraid, I'm confused by the combination of how to build a jar-file to be used in a war-file to be deployed on a tomcat using gradle and I don't know from which of these parts my problems originate. I hope someone reading this can help me untangle my thoughts and point me in the right direction or at least tell me how to add the jars included in the Tomcat library to my gradle dependencies.

like image 792
Joachim Kurz Avatar asked Dec 26 '22 16:12

Joachim Kurz


1 Answers

With Gradle, whenever you add files or directories as dependencies, they are not treated as full-fledged artifacts (with group, name and version), but rather as simple files containing classes. It means that Gradle will not perform any conflicts resolutions on them, or pull transitive dependencies.

For you, just to get started, I recommend just to add tomcat dependency. Make sure it is the same version as the one in Eclipse.

apply plugin: 'war'

repositories {
   mavenCentral()
}

dependencies {
    providedCompile 'org.apache.tomcat:tomcat-catalina:7.0.47'
}

Also, look into Eclipse Integration Gradle project as a long-term solution.

like image 67
kukido Avatar answered Dec 28 '22 07:12

kukido