• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Trying to get servlets to compile...Arrgh!

 
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using this site's example to build simple Maven webapp project:

https://maven.apache.org/archetypes/maven-archetype-webapp/

Now the above works but I want to add a servlet off the WEB-INF/classes folder and have that included in the war file. It doesn't compile the servlet and I have no idea why.

Here's my dir tree after I call mvn package



As you can see the MyHelloWorld.java did not compile and its the same in the Demo.war. Any pointers?
 
Bartender
Posts: 15737
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Don't put Java source code in the webapp folder. Put it underneath the src/main/java folder.

The Maven compiler plugin will compile your classes and put them in the target/classes folder by default. From there, the Maven WAR plugin will put them in the WEB-INF/classes folder of the final WAR by default.

Don't try to do anything fancy. Just use Maven's source folders as they were intended and let Maven do the rest.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now its just a pile of errors when I call mvn package.



dir tree


What's wrong now? Do I have to add a class path or does this maven project know how to compile a servlet?
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's likely you haven't added the correct dependencies to your project. Please show us your POM.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the the pom.xml

 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you're missing a dependency on the Servlet API. Add this to your dependencies:
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hate to be that guy but....

Isn't the whole purpose of this web-app to compile and generate Java servlets and jsp's? You'd think that dependency would be a default.

Sorry for the gripes. Thanks for the help.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what the purpose of the archetype is. When I looked at the directory structure it generates it seems pretty useless to me.

I never really use archetypes. I suggest you learn the Maven POM reference and set up your projects manually. It's not very difficult.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I don't know what the purpose of the archetype is. When I looked at the directory structure it generates it seems pretty useless to me.

I never really use archetypes. I suggest you learn the Maven POM reference and set up your projects manually. It's not very difficult.



Well I'm very new to Maven and its a little overwhelming right now... Maybe when I get a little traction in the topic.

BTW. The posted dependency worked. I dropped the war file into my tomcat server and it worked.
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I don't know what the purpose of the archetype is. When I looked at the directory structure it generates it seems pretty useless to me.

I never really use archetypes. I suggest you learn the Maven POM reference and set up your projects manually. It's not very difficult.



I started doing some introductory reading on how Maven expects directories and files to be set up... And you are right. It's not very difficult to set up your own projects but it is very tedious and takes a lot of reading.

I'm surprised they(Apache Maven) don't have a Maven tutorial called from the ground up -> Building your own Maven projects <-.  
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with the Maven in 5 Minutes guide and the Getting Started guide?
 
Gerard Gauthier
Ranch Hand
Posts: 127
2
Monad Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What's wrong with the Maven in 5 Minutes guide and the Getting Started guide?



There is nothing wring with them. I learned plenty but they start with mvn archetype:generate.... and as you said you can start you projects from scratch or without calling a prebuilt archetype.
 
Saloon Keeper
Posts: 28483
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Incidentally, you defined your servlet in the Java default package and that's not recommended. It's better to define a package like com.coderanch.mywebapp and then create the file /src/main/java/com/coderanch/mywebapp/MyHelloWorld.java. Or probably more informatively as "MyHelloWorldServlet.java". But that's secondary.

The universal archetypes for Maven have been for me almost universally worthless. They're basically just template POMs that people have posted and are often out of date. Although by not including the JEE libraries at least you didn't have to choose between legacy and Jakarta JEE.

If I was defining prototype projects that incorporated local shop standards, I'd likely set up some archetypes in the shop's local repository for convenience. But, as I said, the universal ones aren't so great. Aside from general quality, for a lot of projects there are multiple candidates and no good documents on why you'd prefer one or the other.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic