Showing posts with label webapplication. Show all posts
Showing posts with label webapplication. Show all posts

Monday, September 12, 2011

Maven Web Application Development


You need to have Java 1.6 or higher version, eclipse IDE for J2EE developers , Maven 2.2.1 or higher version and Apache Tomcat 6.0 or higher version installed on your system to follow this tutorial.

Step1 : Integrate m2eclipse plugin into eclipse 

Get the m2eclipse url from the following site (figure 1)
http://eclipse.org/m2e/download/

Figure 1

Open up your eclipse ->  Help -> Install New Software -> Click Add

Put m2eclipse under Name field and Copy the url to Location box (figure 2)
Figure 2

Click Ok and it will  list m2e under plugins to be installed (figure3). 

Figure 3

Select it then click next -> next -> finish.

wait till it is finished downloading required dependencies in to your eclipse instance.

When it is done restart your eclipse IDE. 

Now you have completed installing m2e on your eclipse instance...!


Step2 : Create New Maven Project

Open up your eclipse instance.

File  ->  New -> Other ->  Expand Maven -> Select Maven Project -> Click Next ->
Select Workspace Location (figure 4) Click Next

Figure 4

Select maven-archetype-webapp (figure 5) Click Next 
Figure 5

Set GroupID, ArtifactID and Package name as given below (Figure 6), Leave Version to the default value given.

Figure 6

Click Finish

Step 3: Add maven-compiler-plugin and maven-eclipse plugin in to pom.xml


Figure 7
Add following configuration under <build></build> section of the project pom.xml


<plugins>
     <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-eclipse-plugin</artifactId>
     <configuration>
     <wtpversion>1.5</wtpversion>
     </configuration>
     </plugin>
     <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
     </plugin>
    </plugins>

After adding above configuration it will look like as follows... ( figure 8 )


Figure 8


Step 5: Applying the changes made to the project and download the dependencies [This step is important]

Following Guide is for Windows Users...

Windows7 ships with a command prompt named as Windows PowerShell in which you get the freedom to use linux shell commands.

use that command prompt change to the directory where your project is located using

cd 

command.

then follow the Linux and Mac Users guide where you can use same commands on Windows power shell and even in windows command prompt.


Following Guide is for Linux, Mac Users...


Open up a Terminal and change to your project directory.



Type  $ mvn  clean install


Then Type  $ mvn eclipse:eclipse



This will download required libraries in to maven repository.

Go back to eclipse and refresh your project

Then your project structure will be changed as shown below ( figure 9 )


Figure 9



Step 6 : Add source directory to your project

right click on main directory and create new  folder named java as shown below ( figure 10 )

Figure 10



Get back to your Terminal (for mac and linux users) or command prompt (for windows users) and type 

$ mvn clean install 

and then

$ mvn eclipse:eclipse

as earlier

this will change the project structure to the following look ( figure 11 )

Figure 11

At this stage you won't understand why we need src/main/java folder, but when you follow struts2 and tiles examples , you will understand the necessity of this source folder.

Step 7 : Run your project

Right click on your project

Select Run As option

Select Run on Server ( figure 12 )

Figure 12


Select Apache Tomcat Server version ( i user Tomcat 7 since i have downloaded it, you can use 6 if you have downloaded it) click finish.

This will take you to the HelloWorld Message on eclipse browser ( figure 13 )

Figure 13

Congratulations, you have successfully created your simple application with maven


Tuesday, May 24, 2011

Maven + Eclipse + JSP How To and How NOT To create web application



Points to observe during this tutorial

Directory Structure that eclipse creates (Figure 1) when dynamic web project is created without using maven build tool is different from that of created using Maven.It is true that different IDE's use different structure, but at last when war is build they follow same structure.This is main point that i came across when developing web applications with maven and eclipse. And, when you are making web application using maven, you have to walk through following steps carefully to become successful. but i would like to discuss some wrong paths that you may be following if you are trying to get families with maven and eclipse.

You should have following Software installed on your system to follow this tutorial.

First you should have following downloaded in order to follow this tutorial. You should have java1.6 installed o,Eclipse WTP and Maven2.2 installed on your computer. in addition to that, if you are expecting your eclipse to provide facility run maven commands , then you need to have m2eclipse plugin installed on your system. I am not going to discuss how to set these configurations on your system.

Now , you have it all :). Oky, if not , i would assume you have it all.

So, Since your goal is to use Maven as a build tool for your project. i believe you might have this doubt that, what should be the first stop.

if you are using Maven, you should follow below step first , no matter what type of project you are developing.

There are two ways to generate web project with maven automated build tool.

1. Using Command Prompt
2. Using IDE like eclipse or Ideas with maven plugin integrated in to it.

Using Command Prompt

On your command line, execute the following Maven goal:

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

You will notice that the generate goal created a directory with the same name given as the artifactId. Change into that directory.
cd my-app 
Under this directory you will notice the following standard project structure.
Now , you have created maven web application, Very important point to notice here is that, web application project structure created by maven is different form project structure created for a web application using eclipse IDE without using Maven. Keep this point in mind, i will later come to this point and explain how this difference could affect development activities.
Same project structure can be created if you are using m2eclipse plugin.   .