How to manage dependency on servlet-api?
We have a web maven module that is compiled to a war, and thus doesn't need to include the servlet-api jar directly. We use <scope>provided</scope>
for this.
The problem arises when I try to write a small main() to test one of my classes. This is not a unit test, but rather resides with the code itself.
This fails on java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
:
public static void main(String[] args开发者_StackOverflow社区) {
final Injector injector = Guice.createInjector(new StandardModule());
// StandardModule is our standard module, with bindings to
// something that rightfully depends on servlet API
...
}
Besides splitting my module to two or three different modules, is there an easy workaround I haven't thought of?
Perhaps the best solution is to move this tester to the test code.
You can use <scope>test</scope>
if you want to have it for tests. However provided
works fine for me in Eclipse - it is added to the classpath.
If you use eclipse with m2eclipse, <scope>provided</scope>
will be resolved correctly and your main class will compile and run properly.
精彩评论