Developing Struts2 Applications Using Maven 3

Introduction to Maven

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 a archetype available for Struts2 in Maven. 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 Jetty Java application server. The various steps involved in Maven based Struts2 development are,

  • Build a sample Struts 2 application using Maven archetype,
    • Maven downloads the archetype definition and related libraries from centralized maven repository hosted online
    • Maven creates a sample Struts 2 application containing various dependent jar files and creates a default pom.xml for the project
  • Compile and run Struts 2 application using Maven,
    • Maven reads the pom.xml in the project and builds the Struts 2 project
    • Maven starts the Jetty servlet container and deploys the Struts 2 project

Check out the following step by step tutorial on developing Struts 2 applications using Maven 3. This example assumes that you are working on a Windows machine. The steps are same if you use Maven 2.

Pre-requisites

For running Maven and Struts 2 applications Java SDK is required. I recommend you download and install Oracle JDK 6 from here.

Step 1 : Download and Install Maven 3 Tool

Download the Maven binary zip file from the Maven download page. For this tutorial, I used  apache-maven-3.0.3-bin.zip. Extract the zip file to a folder of your choice. Add the bin folder of the extracted file in the PATH environment variable of Windows. This ensures that you can access Maven from any folder.

I extracted Maven zip file to the folder W:\programming-final\tools. Added the path to the bin folder (W:\programming-final\tools\apache-maven-3.0.3\bin) to the Windows PATH environment variable. You can set the PATH variable in Windows 7 from Control Panel => System => Advanced System Settings => Environment Variables => Create a new user variable. Add the PATH variable as shown below,

Modifying PATH environment variable in Windows 7

You also need to set the JAVA_HOME environment variable in a similar manner if it is not already set. In my case JAVA_HOME points to

Now open a command prompt and check the correct installation of Maven by running the following command,

mvn -v

Testing Maven Installation

Step 2 : Create a Sample Struts 2 Application Using Maven 3

Before you proceed further, ensure that you have an active internet connection. This is required since Maven will be downloading Struts2 archetype files and Struts2 jars directly from the online Maven repository.

Struts 2 project provides more than one Maven archetype for creating a sample Struts 2 application. These are,

  • The Blank Archetype – Use this to create a simple Struts 2 application built using XML configuration files. It is a complete example.
  • The Starter Archetype – This is more advanced than the blank type and contains sitemesh and spring integration. It also demonstrates validation, conversion and resource bundles.
  • The Portlet Blank Archetype – Use this if you want a JSR 168 portlet based Struts 2 application.
  • The Portlet Database Archetype – This shows how to build a portlet application with database support
  • The Blank Convention Archetype – This is same as blank archetype, but uses convention based approach than XML files for configuration.
  • The Plugin Archetype – This archetype can be used to create a Struts 2 plugin.

We will use the blank archetype for this tutorial. Run the following command to create a simple Struts 2 application using Maven. Note that I am using "tutorial" as the group id and the artifact id of the new project. You can change it to your application name.

mvn archetype:generate -B  -DgroupId=tutorial -DartifactId=tutorial -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-blank -DarchetypeVersion=2.2.3.1

Sit back and relax, it might take a while to download and install everything.  Note that for this tutorial I use Struts2 version 2.2.3.1. You might want to use the latest production release for your project.

All the jars and artifacts downloaded are cached in your machine. This means that from next time onwards, maven will use the local cached version instead of the online repository. The default location of the repository in a Windows machine is C:\Users\<your user name>\.m2\repository. Check out this folder to see what Maven has downloaded and cached during the archetype build.

Once the archetype command is finished, you will see a new project "tutorial" created under the current folder,

Sample Struts 2 tutorial app using Maven 3

Note that in a Maven project, non Java files are kept in a separate resources folder. For example, in the tutorial application, struts.xml is now under resources folder instead of the src folder.

Step 3 : Verify and Edit Generated pom.xml

It seems there are bugs with some of the archetypes for Struts 2. When I used the 2.2.3.1 archetype, the variables ${project.version} and ${struts2.version} was not replaced with their actual values in pom.xml. I had to manually change them in pom.xml as 1.0 and 2.2.3.1 respectively. The updated pom.xml for Struts 2.2.3.1 blank application 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>tutorial</groupId>
    <artifactId>tutorial</artifactId>
    <version>1.0</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>
        </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>

 

Step 4 : Build and Run Sample Struts 2 Application Using Maven 3

Change the current directory of the command prompt to the tutorial folder. This is required since the following Maven commands can be run only from the folder which contains the pom.xml. Compile the sample Struts 2 application,

mvn compile

To run the unit test cases, run the following command,

mvn test

To deploy and run the sample web application on Jetty web application server, run the following command,

mvn jetty:run

Test your sample application by accessing the following URL. Please ensure that no other application (for example, tomcat) is listening on port 8080,

http://localhost:8080/tutorial/example/Welcome.action

Running Struts 2 application on Jetty using Maven

You might see an error in Jetty – "Could not find action or result – /tutorial/css/examplecss". This is due to a typo in the blank application (another bug in Maven archetype). You can correct it by adding the missing dot in \tutorial\src\main\webapp\example\Welcome.jsp.

When you work on any large scale projects, the command line use soon becomes cumbersome. It is also possible to install and use Maven 3 in Eclipse IDE along with Tomcat or JBoss AS. I will cover that in the next article.

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 26, 2011 | Posted in Tutorials 1 Comment »

One Comment to “Developing Struts2 Applications Using Maven 3”

  1. How to Run Struts 2 Maven Projects in NetBeans Says:

    [...] an earlier tutorial I covered how Struts 2 projects can be created using Maven. In this tutorial I will show you how you can use the Struts 2 Maven project in NetBeans IDE. For [...]

Leave a Comment