2

I want to run the servlet testing example available here using maven. Javaee web api should be declared as provided:

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>httpunit</groupId>
        <artifactId>httpunit</artifactId>
        <version>1.7</version>
        <scope>test</scope>
    </dependency>

However, one of the tests in the example throws ServletException. NetBeans complains that java ee api is missing on project classpath. How does one solve this issue?

EDIT

It is not a NetBeans issue, it is a maven issue.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • I don't do Netbeans, but in Eclipse you'd need to reference the target runtime container (e.g. Glassfish, Tomcat, etc) in the project's properties. The container is namely a concrete (partial) Java EE implementation and the IDE should be smart enough to take over its libraries in build path. Similar configuration should exist in Netbeans. – BalusC Sep 29 '11 at 21:05
  • @BalusC I don't think it is the issue, but I did try your suggestion. I have set the target container to Tomcat, but it did not solve the issue. – Jérôme Verstrynge Sep 29 '11 at 21:09
  • Ah well. Sorry, I can't assist you further as I don't do Netbeans. – BalusC Sep 29 '11 at 21:10
  • @Balus I don't think this is NetBeans related. See my comment to Augusto's answer. – Jérôme Verstrynge Sep 29 '11 at 21:16
  • Are you actually trying to run it in netbeans or maven? – Matthew Farwell Sep 29 '11 at 21:16
  • @Matthew When you compile a maven project from NetBeans, it calls maven and runs the Junit tests. I am not running the test step-by-step with the NetbBeans debugger if that is what you are asking + I do get an error message from the output screen: java.lang.reflect.UndeclaredThrowableException – Jérôme Verstrynge Sep 29 '11 at 21:19
  • Does it happen if you run maven from the command line? – Matthew Farwell Sep 29 '11 at 21:23
  • @Matthew I just tried mvn compiler:testCompile in command line and I get the same error as in NetBeans. – Jérôme Verstrynge Sep 29 '11 at 21:35
  • So it's a maven problem, not NetBeans – Matthew Farwell Sep 29 '11 at 21:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3913/discussion-between-jverstry-and-matthew-farwell) – Jérôme Verstrynge Sep 29 '11 at 21:40

2 Answers2

9

Now this is the most debilitating issue I have ever faced in my Java days. And it is followed by the most ridiculous workaround I have ever seen, ever:

<dependency>
    <groupId>httpunit</groupId>
    <artifactId>httpunit</artifactId>
    <version>1.7</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

Yes, permute the declaration of dependencies in the pom.xml (see here for "why") and make javaee-web-api last.

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • 1
    +1 ouch, I had similar problems with maven in the past, but blamed the computer gnomes. Thanks for sharing the why of the underlying issue. – Augusto Sep 30 '11 at 09:12
  • Thankss for the solution! In all other places, the solution were about including API implementations or sort of. You were the only one who could explain the "root of all evil" !!! – Ramon Chiara Sep 26 '14 at 21:29
0

It means that maven (or netbeans...I haven't used netbeans in 10 years), couldn't download or find that artifact in the local repository.

The scope provided means: I need the jar to compile my source code, but don't bundle the jar in the final package.

Augusto
  • 28,839
  • 5
  • 58
  • 88
  • I know what the 'provided' scope is, and, I could compile this project before I started using httpunit. I believe the issue is in the fact that 'provided' does not allow using in test classes. Yet, when I set the 'test' or 'compile' scope, it does not work too. – Jérôme Verstrynge Sep 29 '11 at 21:15
  • And I am wrong, http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html says "This scope is only available on the compilation and test classpath, and is not transitive". WTF? – Jérôme Verstrynge Sep 29 '11 at 22:05