Java Dynamic Web project with Maven and Eclipse -


i have several questions creating java web application maven , eclipse:

  • how create java web project servlets, jsp, , other classes maven?
  • it creates simple directory structure, src->main->java. , how put web-inf folder?
  • do need add jdbc-drivers manually folder inside web-inf/lib, or ok point out dependency?
  • is there way test servlets junit?

wow that's lot of questions @ once. admit setting webapp project maven , eclipse can tricky, i'll try answer them all.

creating web application project maven

how create java web project servlets jsp , other classes maven? creates simple directory structure, src->main->java.

when creating java web project, final product should war or ear file. war , ear files jar files specific structure can deployed in application server or servlet container.

as mentioned, easiest way set maven project web applications use archetypes:

mvn archetype:generate -darchetypeartifactid=maven-archetype-webapp 

if create project archetype simple directory structure , pom.xml generated. project follows standard maven directory layout mention, /src/main/java/, /src/test/java, etc. maven generates war file structure war:war goal.

note archetype simple (outdated) web application, it's best available starting point. want discard web.xml file , create new 1 supports servlet 3.0.

web-inf location

where , how put web-inf folder?

by default, maven expects resources should go in root of war file -- such images, html pages , web-inf directory -- reside in /src/main/webapp/. so web-inf folder should located @ /src/main/webapp/web-inf/. if use maven-archetype-webapp directory automatically created, along sample web.xml file.

eclipse integration

you mentioned eclipse in question title, , indeed possible develop mavenized web applications in eclipse m2eclipse plugin. eclipse has support web applications through wtp (web tools platform).

although many guides on internet (wrongly) recommend it, you should not use mvn eclipse:eclipse command create eclipse project. plugin can generate wtp projects old eclipse versions (wtp 2.0 maximum). instead, use m2eclipse plugin described here.

dependencies

do need add jdbc-drivers manually folder inside web-inf/lib, or ok point out dependency?

there no need manually, since 1 of key strengths of maven dependency management. if add dependency in pom.xml scope of compile or runtime, jar file automatically included in web-inf/lib/ directory of war file. example add postgresql jdbc driver dependency, add pom.xml:

<dependency>     <groupid>postgresql</groupid>     <artifactid>postgresql</artifactid>     <version>9.1-901.jdbc4</version> </dependency> 

since scope unspecified maven assume in the default scope compile. result maven include web-inf/lib/postgresql-9.1-901.jdbc4.jar in war file.

testing

is there way test servlets junit?

this question has been asked (and answered) on stackoverflow:

references


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -