In the A simple tutorial of JSF 2.0, Eclipse, Weld and Maven 2 we explain how to install and setup maven in your development environment , and how to create and run a web application, but the essential for a developer is how to use the IDE to debug the project when things go as Unexpected and deep into the code to find what happen. In this post we proceed like  habitually, step by step to reach our  goal.

Prerequisites:

1. Eclipse (We use Helios)

2. Tomcat 6

3. Maven 2

Step 1 : Install & setup tomcat 6

1. Download tomcat 6 from here .

2. Extract the zipped file in your drive (c:\apache-tomcat-6.0.28)

3. Go to Eclipse and select Window–>Preferences from the Menu

4. Under the Server option select Runtime Envirenment

5.Click on Add, a window with contain the application servers will appears

6. Choose Apache Tomcat v6.0 and click Next

7. Fill in the Tomcat installation directory by the directory where put the Tomcat in your drive. Then click finish, automatically the server appears in the Servers view.

Step 2: Configure the project

We use the project created in the previous post , So if you right-click on the server and choose “Add and Remove” , the project named “Foo” will not appears in the available projects  to will be deployed in the server

Now the important stuffs start from here, What we will do? it’s simple , we just will hacking the Foo’s project settings to be a web application supported by Eclipse and deployed in tomcat.

1. In the project directory open the file .project , You have some thing like this:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Foo</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
</natures>
</projectDescription>

in the “buildSpec” tag add replace the previous configuration with this :

<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>

And in the “natures” tag with this:

<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>

Now right-click in the project and choose Properties –>  Project Facets then fill in options like in the following image

Click OK.

The “WebContent” directory is added and others file in the project’s “.settings” folder.  The WebContent folder is not important for us, because Maven have its own methodology to structure and build  the project that we profit to make the the work easy. In one hand Tomcat point to WebContent to collect jsp, html, classes  and libraries files to build and deploy correctly the project  and in the other hand Maven put all files and builds in the target folder, Now we make some changes to indicate tomcat to use the Maven philosophy and get required files from target or src directories to build and deploy the project, there is two way to makes this changes :

1. In the .setting folder open the org.eclipse.wst.common.component file and put this code:

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="Foo">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp"/>
<wb-resource deploy-path="/WEB-INF/lib" source-path="/target/Foo/WEB-INF/lib"/>
<property name="context-root" value="Foo"/>
<property name="java-output-path" value="/Foo/target/classes"/>
</wb-module>
</project-modules>

We just tell to tomcat where to find his food to build/deploy the project.

2. It’s the same but just we use UI for simplicity

2.1.  Right-click on the project, choose Run As–> Maven Package.  if some thing like [INFO] BUILD SUCCESS is displayed in the console so things are OK and

2.2 Right-Click on the project,choose Properties –> Deployment Assembly –> Add –> Folder and select for each source the deploy path, in the end

you have some thing like this:

Click Ok.

Step 3 : Deployment

1. Right-Click on the server Choose Add & Remove, as you see the Foo project is here

Click on  add and finish

Right-Click on the server and choose start, Launch an internet navigator and enter the following address :

http://localhost:8080/Foo

if the web page is displayed correctly, In the HelloWorld class make a break point and restart the server in debug mode, then refresh your web page , and your are in your home “The debug mode”.

Be free to leave comments if any thing is misunderstood.

Advertisements