Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Dynamic Web project with Maven and Eclipse

I have several questions about creating a Java Web application with Maven and Eclipse:

  • How do I create a Java web project with servlets, jsp, and other classes with Maven?
  • It creates a simple directory structure, src->main->java. Where and how do I put the web-inf folder?
  • Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?
  • Is there a way to test the servlets with junit?
like image 852
Artem Moskalev Avatar asked Jun 04 '12 20:06

Artem Moskalev


People also ask

How do I add pom files to dynamic web project?

Right click on the project > Properties > MyEclipse > Project Facets and select the one you want to install to your project. The Maven Support – Dependencies Only Facet allows you to benefit from Maven's dependency resolution, without having to move your projects to a fully Maven based build.

How do I convert an existing dynamic Web project to Maven in Eclipse?

Right-click the project and select Configure > Convert to Maven Project. Complete the Maven POM dialog. Enter a Group Id, Artifact Id, and Version or accept the defaults.


2 Answers

Wow that's a lot of questions at once. I admit that setting up a webapp project with Maven and Eclipse can be tricky, so I'll try to answer them all.

Creating a Web application project with Maven

How do I create a java web project with servlets jsp and other classes with maven? It creates a simple directory structure, src->main->java.

When you are creating a Java web project, the final product should be a WAR or EAR file. WAR and EAR files are JAR files with a specific structure that can be deployed in an application server or servlet container.

As mentioned, the easiest way to set up a Maven project for web applications is to use archetypes:

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

If we create a project with this archetype then a simple directory structure and pom.xml are generated. This project follows the standard Maven directory layout you mention, with /src/main/java/, /src/test/java, etc. Maven generates the WAR file from this structure with the war:war goal.

Note that this archetype is for a very simple (outdated) web application, but it's the best available starting point. You probably want to discard the web.xml file and create a new one that supports Servlet 3.0.

WEB-INF location

Where and how do I put the web-inf folder?

By default, Maven expects resources that should go in the root of the WAR file -- such as images, html pages and the WEB-INF directory -- to reside in /src/main/webapp/. So the WEB-INF folder should be located at /src/main/webapp/WEB-INF/. If you use the maven-archetype-webapp this directory is automatically created, along with a sample web.xml file.

Eclipse integration

You mentioned Eclipse in the question title, and it is indeed possible to develop Mavenized web applications in Eclipse with the m2eclipse plugin. Eclipse has good support for Web applications through WTP (Web Tools Platform).

Although many guides on the internet (wrongly) recommend it, you should not use the mvn eclipse:eclipse command to create the Eclipse project. This plugin can only generate WTP projects for very old Eclipse versions (WTP 2.0 is the maximum). Instead, use the m2eclipse plugin as described here.

Dependencies

Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?

There is no need to do this manually, since one of the key strengths of Maven is dependency management. If you add a dependency in the pom.xml with a scope of compile or runtime, the JAR file will be automatically included in the WEB-INF/lib/ directory of the WAR file. For example to add the Postgresql JDBC driver dependency, add this to your pom.xml:

<dependency>     <groupId>postgresql</groupId>     <artifactId>postgresql</artifactId>     <version>9.1-901.jdbc4</version> </dependency> 

Since the scope is unspecified Maven will assume it is in the the default scope compile. The result is that Maven will include WEB-INF/lib/postgresql-9.1-901.jdbc4.jar in the WAR file.

Testing

Is there a way to test the servlets with junit?

This question has been asked (and answered) on Stackoverflow:

  • Unit testing a Java servlet
  • Unit testing servlets

References

  • Hello World with JSF 2.0, Glassfish 3, Maven, SVN and Eclipse.
like image 169
tboerman Avatar answered Oct 22 '22 06:10

tboerman


You should create a project based on the webapp Maven archetype, not the default one you're using.

I'm using SpringSource Tool Suite, which, for this exercise, is the same as Eclipse with m2e. Create a new Maven project and make sure you select the following archetype:

enter image description here

The Maven the command-line way of doing this is:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp 

This archetype will put the WEB-INF folder in the correct location (under src/main/webapp).

You can find more information at http://maven.apache.org/guides/mini/guide-webapp.html

like image 45
Jonathan W Avatar answered Oct 22 '22 07:10

Jonathan W