Developing Struts2 Applications Using Eclipse, M2Eclipse Plugin and Maven

In this step by step tutorial I will show you how Eclipse IDE, M2Eclipse Plugin and Maven can be used to develop Struts 2 Web applications. In order to create and run Struts 2 Maven projects in Eclipse, you need to download the following,

  • Eclipse IDE – Eclipse Indigo 3.7.1 or higher for Java EE Developers
  • M2Eclipse Plugin – Maven integration plugin from Eclipse repository
  • M2E-WTP Connector – Connector plugin for tight integration between M2Eclipse plugin and Eclipse WTP
  • Tomcat Server – Apache Tomcat 7 or above

Pre-requisites

Oracle JDK 6 is required in your machine. Also ensure that the environment variable JAVA_HOME points to your JDK installation. You can download JDK 6 from here.

Installing Eclipse IDE

Download Eclipse Indigo 3.7.1 or higher. Download Eclipse IDE for Java EE Developers (32 bit) zip file and extract the zip file to a folder of your choice.

Start Eclipse IDE and configure the Java JDK as the default JRE as shown below (Preferences => Java => Installed JREs). Note that JDK 6 is selected as the default JRE.

Configuring JDK in Eclipse

Installing M2Eclipse Plugin

M2Eclipse plugin enables Maven build tool integration in Eclipse. Maven is a build and project management tool and can substantially simplify dependency management and project creation. Using Maven, it is possible to create a Struts 2 application compatible with a specific version of Struts 2 without any manual intervention.

M2Eclipse plugin is now part of Eclipse project and is packaged in Eclipse IDE for Java Developers. However it is not included in Eclipse IDE for Java EE Developers. Hence you need to manually install it from Eclipse IDE. Also note that current version of M2Eclipse contains embedded Maven 3 (Maven 3.0.2) and hence there is no need to download Maven separately (However if you want to use a later version you can configure external Maven installation).

From Eclipse IDE, Click on "Help" => "Install New Software". Click on Add to add the M2Eclipse Plugin repository as shown below. The M2Eclipse plugin URL is – http://download.eclipse.org/technology/m2e/releases.

Adding M2Eclipse in Eclipse

Click on OK. Select "Maven Integration for Eclipse" as shown below and click on Next to complete plugin installation. Restart Eclipse after installation.

Installing M2Eclipse in Eclipse

After restart, Eclipse preferences will have a new section for Maven as shown below,

Maven Configuration in Eclipse

Maven – A Quick Introduction

Maven is a project management/build manager tool for Java projects. Using Maven you can capture library dependencies and other project properties in a file named pom.xml. POM stands for Project Object Model and it contains project dependencies, various project build phases and repository information for the project and its dependencies. For building large solutions containing  multiple Java and Web projects, Maven is an indispensable tool.
Another major concept in Maven is the idea of archetypes. Archetypes represents a model or a template for a project. For example, there is an archetype available for Struts2 in Maven (created by Struts 2 team and published to Maven repository). Using this archetype we can build a sample Struts2 project without manually copying dependent jar files or creating configuration files. The created Struts2 project will also contain a well defined pom.xml using which we can compile the Struts2 application or run it on the application server.

Configuring Maven in Eclipse

By default, Maven sets up two repositories for all the artifacts. The local repository and the remote maven repository. You can view the contents of these by accessing Window => Show View => Other => Maven => Maven Repositories from Eclipse.

Maven repository view in Eclipse

Initially local repository is blank and when Maven downloads dependent jars and archetypes from remote repositories, it caches them in local repository.

The central Maven repository index can be updated by right clicking the central and selecting "Update Index". This might take a while depending on your internet connection. Once the index is updated, ideally you should be able to select one of the Struts 2 archetypes in Eclipse "New Project" to create a Struts application. However I could never get this to work. "New Project" did list a large number of archetypes from remote, but for some reason refused to show Struts 2 archetypes.

So I recommend the following work around in which we will download the central Maven archetype catalog locally and would configure it as a local catalog.

Download central Maven archetype catalog file (http://repo1.maven.org/maven2/archetype-catalog.xml) to a local folder in your system. From Eclipse go to Preferences => Maven => Archetypes and click on "Add Local Catalog". Enter the location of the catalog file downloaded in the previous step,

Adding local archetype catalog in Eclipse

Click OK. We have now configured a local Maven catalog which contains details about Struts 2 archetypes. Note that if there is a future release of a newer Struts 2 version, you will need to download the updated catalog and repeat the above procedure. In the next step we will see how this catalog can be used to create a Struts 2 Maven project.

Creating a Struts 2 Application in Eclipse

From Eclipse, click on File => New Project =>Maven => Maven Project as shown below.

Creating a new Maven project in Eclipse

Click on Next and then in the next screen, click Next again. From the catalog field select the local Maven archetype catalog created earlier. Type in struts2 in the filter field to see available Struts 2 archetypes. Select Struts 2 blank archetype with version 2.2.3.1 as shown below,

Selecting Struts2 archetype in Eclipse

Click on Next. Enter your project details as shown below. Group id represents a group id for a set of projects. Artifact id is the unique id for your project. In version number, SNAPSHOT indicates that the project is in development. The package field contains the root java package of the project.

Maven project settings in Eclipse

Click on Finish to create a Struts 2 Maven Project.

The pom.xml in the project will show errors since there is a bug in the Struts 2 archetype for Struts 2.2.3.1. To correct this you need to change the version number variable names in pom.xml to actual version numbers. Change ${project.version} to 1.0-SNAPSHOT and ${struts2.version} to 2.2.3.1 (there is also a bug of using ${project.version} instead of ${struts2.version} under properties tag). The corrected pom.xml is given below,

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.struts2</groupId>
	<artifactId>tutorial</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Struts 2 Blank Webapp</name>

	<properties>
		<struts2.version>2.2.3.1</struts2.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.2.3.1</version>
			<exclusions>
				<exclusion>
					<artifactId>tools</artifactId>
					<groupId>com.sun</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-config-browser-plugin</artifactId>
			<version>2.2.3.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-junit-plugin</artifactId>
			<version>2.2.3.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.5</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-mock</artifactId>
			<version>2.0.8</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>2.5</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.21</version>
				<configuration>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<scanTargets>
						<scanTarget>src/main/webapp/WEB-INF</scanTarget>
						<scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
						<scanTarget>src/main/resources/struts.xml</scanTarget>
						<scanTarget>src/main/resources/example.xml</scanTarget>
					</scanTargets>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Now you will get another error – "Missing artifact com.sun:tools:jar:1.5.0". This is due to a dependency on the struts2-core artifact. You can fix the problem by replacing the dependency section of struts2-core in pom.xml with the following,

<dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-core</artifactId>
	<version>2.2.3.1</version>
	<exclusions>
		<exclusion>
			<artifactId>tools</artifactId>
			<groupId>com.sun</groupId>
		</exclusion>
	</exclusions>
</dependency>

Lastly you will see an error that project configuration is not up-to-date with pom.xml. Fix this by right clicking the project and selecting Maven => Update Project Configuration. This will download all the dependent jars from remote machine via local repository cache and will also build the project. If this is the first time you are using Maven, it will take a while to download everything. Obviously your machine needs to have an active internet connection.

Now you can build your Struts 2 Maven project by right clicking the project and selecting Run As => Maven build.

However what we would like to do is to be able to integrate Maven with Eclipse WTP so that we can directly build and run the project on Tomcat or JBoss server. You need M2E-WTP connector for this.

Installing M2E-WTP Connector for WTP Support

From Eclipse IDE, select Preferences => Maven => Discovery. Click on Open Catalog button. Select the M2e-WTP connector from the following screen,

Downloading m2e-wtp connector in Eclipse

Click on Finish. In the next screen select Maven Integration for WTP and click on Next and Finish. Restart Eclipse. Now Maven and Eclipse WTP integration is complete. This enables dynamic web facet for Struts 2 maven project. In order to run our Struts 2 application, we need to install and configure Tomcat in Eclipse.

Installing and Configuring Tomcat 7 Server on Eclipse

Download Tomcat 7 zip file and extract it to a folder of your choice. From Eclipse, select Window => Show View => Other => Server => Servers.  Right click on Servers window and select New => Server.

Adding a new server in Tomcat

Select Tomcat 7 from Apache folder as shown below.

image

Click on Next. Enter the location of the Tomcat folder and then choose JRE as the JDK installed in Eclipse.

Configuring Tomcat in Eclipse

Click on Finish to create Tomcat 7 server configuration in Eclipse.

Running Struts 2 Application in Eclipse

From Eclipse, right click on the Struts 2 Maven project and select Run As => Run on Server. Select Tomcat 7 server in the next screen and voila! your first Struts 2 application is up and running on Tomcat 7. Oh Boy! it was a long journey to get here.

Struts 2 application running on Tomcat

Adding Additional Libraries to Struts 2 Application

If you need additional libraries in your Struts 2 application, you need to add the dependency in pom.xml. Maven will automatically download the jar and will package it as part of the war file created.

Click here to get latest site updates delivered to your email. You also need to click on the link sent to your email from feedburner to confirm your subscription.


November 27, 2011 | Posted in Tutorials 1 Comment »

One Comment to “Developing Struts2 Applications Using Eclipse, M2Eclipse Plugin and Maven”

  1. Struts2 Tiles Integration Says:

    [...] a simple Struts 2 application using Maven archetypes. Follow the Struts 2 Maven Eclipse tutorial or Struts 2 Maven NetBeans tutorial to setup a Struts 2 project using Maven. Delete all the files [...]

Leave a Comment