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


4 comments:

  1. Nice tutorial, but I can't run on the server - I don't get that option. It gives me Java Applet, Java Application - but not not on server. Any suggestions?

    Thanks
    Dave

    ReplyDelete
  2. Hi Dave, Thank you for the interest. do you have tomcat server running on your computer

    ReplyDelete
  3. Hi ....gud one but i am working on windows xp os ...can u pls explain me how to run mvn eclipse command on windows


    Thanx in advance

    ReplyDelete
  4. Hi,In modern website design and development, jQuery is the most popular JavaScript library in use for Web Design Cochin. jQuery extends the possibilities of the website and simplifies the entire website development process.Thanks..........

    ReplyDelete