How to Run Struts 2 Maven Projects in NetBeans

In an earlier tutorial I covered how Struts 2 projects can be created using Maven. In this tutorial I will show you how you can run Struts 2 Maven projects in NetBeans IDE. For this tutorial, I will be using Maven 3, Struts 2 2.2.3.1 and NetBeans 7.0.1.

Pre-requisites

This tutorial requires Oracle JDK 1.6 and NetBeans 7 installed in your machine. Check out NetBeans installation guide for details on installing NetBeans.

How to Import an Existing Struts 2 Maven Project in NetBeans

NetBeans can automatically recognize a Maven project. From NetBeans 7 onwards complete integrated support for Maven 3 is provided. Maven 3 is also bundled along with NetBeans 7 and if you already installed Maven 3 in your system, you have 2 instances of Maven 3. However both point to the same local repository.

From NetBeans click on File => Open Project. Navigate to the folder which contains your Struts 2 Maven Project. See this article if you don’t know how to create a Struts 2 Maven project from command line. NetBeans will automatically identify the "tutorial" project as shown below. Select tutorial project and click on open project. Note that the name of the project is automatically taken from the name tag in pom.xml.

Opening Maven Struts 2 project in NetBeans

The project will be imported as NetBeans project. Click on the Run icon in NetBeans. Select the Tomcat server as the default server for the project as shown below,

Selecting default server in NetBeans

This will run the imported Maven Struts 2 project on the configured Tomcat server. The context path for the web application is of the form <artifactid-version number> and hence invoke the following URL for accessing the Welcome.action in the sample project,

http://localhost:8084/tutorial-1.0/example/Welcome.action

NetBeans provides automatic binding between IDE actions and various Maven goals. You can view the default binding by right clicking the project and selecting Properties => Actions. The following screenshot shows that when you select “Build Project” from NetBeans IDE, Maven’s install goal is executed.

NetBeans IDE to Maven goals binding

The default settings for Run Project is to invoke the "package" goal and then leave the deployment to NetBeans IDE (netbeans.deploy=true property indicates this to the IDE). You can override this behavior so that when you invoke “Run Project”, the jetty:run goal is called. Change the goal from "package" to "jetty:run" and then remove the attribute “netbeans.deploy=true” as shown below,

configuring Maven goals in NetBeans

Now Run the Project. The application will be hosted on Jetty server and can be accessed from the following URL,

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

How to Create a Struts 2 Maven Project in NetBeans

Since NetBeans has built in support for Maven 3, it is easy to create a Struts 2 Maven application. This is also the preferred way of creating a new Struts 2 application in NetBeans.

From the NetBeans IDE click on New Project. From the New Project window, Select Maven/Project from archetype as shown below,

Maven project from archetype

Click on next. You will be provided with an option to select a Maven archetype from local or remote repositories. If you have already run a Struts 2 Maven archetype from the command line, you will see the archetype under local repository. Otherwise you will need to find Struts 2 archetype from the remote repository and add it to local repository.

Creating a Struts 2 Project from Maven in NetBeans

If you cannot find the Struts 2 blank archetype from the above screen, cancel new project wizard and go to  Window => Other => Maven Repository Browser => Right click on Central => Update Index in NetBeans IDE.

Updating Maven Index in NetBeans

Depending on your internet connection, this might take while, but is a one time process. The Struts 2 archetypes are available under org.apache.struts folder in the repository. Expand this folder to the struts2-archetype-blank and then right click the version you want and click on download. This will download the archetype to your local repository.

Downloading Struts2 archetypes to local Maven repository in Netbeans

After downloading the Struts 2 archetype to local repository, return to New Project wizard. In the second page of the wizard, select the Strut 2 blank archetype from local repository,

Creating a Struts 2 Project from Maven in NetBeans

In the next page enter the name of the project, group id, version number and optionally the main package of your project. In Maven, versions which are still under development will have the suffix of SNAPSHOT.

Maven project settings in NetBeans

Click on finish to create the Struts 2 Maven project. Some of the Struts 2 Maven archetypes in repository have bugs and as of writing this article, version 2.2.3.1 also has bugs. This means that by default, you will see the project as "misconfigured Maven project in tutorial" in NetBeans.

The solution is to open the pom.xml under Project Files and then modify the ${project.version} to 1.0-SNAPSHOT and ${struts2.version} to 2.2.3.1. If you are using 2.2.3.1 version of Struts2, you can replace pom.xml with the following file,

<?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>
        </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 can Run your Struts 2 application on NetBeans Tomcat by pressing F6!

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 No Comments »

Leave a Comment