Friday, July 15, 2011

Creating a Struts 2 Project Using Maven on Tomcat With IntelliJ IDEA

This post describes how to create a Java project using Struts 2 as the web framework, Maven as the build system to automatically deploy to Tomcat using IntelliJ IDEA 10 as the IDE.

Setting up Maven:
  • Download the latest version of Maven from http://maven.apache.org/download.html.
  • Install Maven by following the instructions at http://maven.apache.org/download.html#Installation.
Creating a Blank Struts 2 Project Using Maven
  • Change directory to the directory you want the project created in.
  • mvn archetype:generate -B -DgroupId=com.mycompany.myapp -DartifactId=my-app -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-blank -DarchetypeVersion=<struts version ex. 2.2.3>
Creating the IDEA Project
  • File->New Project
  • Select the option "Create Java project from existing sources".  Then click "Next"
  • Enter a name and location for your project.  Then click "Next" 
  • Select "Do not create source directory" as the source directory was already created by Maven.
  • Click "Next" twice, then "Finish".
Associate Maven Project with IDEA
  • Go to"Maven" in the IDEA preferences.
  • For the Maven home directory value enter the location where you installed Maven ex. /usr/local/apache-maven/apache-maven-3.0.3
  • Click "Maven Projects" on the right side of the IDE.
  • Click  the "+" button to add a new Maven project.
  • Select the pom.xml that was created by Maven.
Add Struts and Dependencies Libraries
  • In IDEA go to the "Project Structure" menu item, click "Libraries" under "Project Settings".
  • Click the button "Attach Classes from Repository".
  • Enter org.apache.struts:struts-core:<struts version> ex. org.apache.struts:struts-core:2.2.3
  • Click "OK".
  • This will add the libraries to a lib folder in your IDEA project.
Add the Mojo Tomcat Deployment Plugin to the pom.xml
  • Add the following code snippet to the plugins section of the pom.xml.
  •             <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>tomcat-maven-plugin</artifactId>
                    <version>1.1</version>
                    <configuration>
                        <url>http://localhost:8080/manager/html</url>
                        <warFile>${project.build.directory}/${project.build.finalName}.war</warFile>
                        <server>tomcat-localhost</server>
                    </configuration>
                </plugin>
  • Note that the url and server values may need to be updated to reflect your installation.
  • In the Maven settings.xml add the following snippet to the "servers" section.
  •     <server>
          <id>tomcat-localhost</id>
          <username>username</username>
          <password>password</password>
        </server>
  • Note that the id must match the server name in the pom.xml and the username/password must be for the user that has the role "manager-gui".
  • To deploy to tomcat, in IDEA under the "Maven Projects" tab you should now see tomcat under "Plugins".  There you can select "tomcat:deploy" or "tomcat:redeploy" to deploy your application.

4 comments: