Trisummit Technologies

Build a Struts Application with Maven

Posted on | March 24, 2010 | No Comments

Maven is a build tool for Java based web apps – plain and simple.

Before learning about Maven I did my builds using Ant. And when it came to building webapps, I was always frustrated about getting things in source control in such a way that all of the developers could build their apps no matter what IDE they were using. For example, you get some developers using Eclipse and others using Net Beans, and the different IDEs wants things set up their own way.

By using Maven (instead of ANT or batch files), you do your builds independent of the IDE using the command line; nevertheless, the build can still be done using the IDE if you choose to do so. However, by using the command line, you ensure that your IDE isn’t playing tricks on you.

Follow the instructions of the documentation for getting Maven installed on your development machine.

Next download struts and extract the war file by renaming it to the zip extensions and extracting it.

Next, create your directory structure following the instructions on Maven Directory Structure.

/
+- src/
|  +- main/
|  |  +- java/
|  |  |  +- ...
|  |  +- resources/
|  |     +- ...
|  +- test/
|  |  +- java/
|  |  |  +- ...
|  |  +- resources/
|  |     +- ...
|  +- site/
|     +- xdoc/
|        +- ...
+- target/
|  +- ...
+- project.xml
+- README.txt
+- LICENSE.txt

Directory 1:
src -> main -> java
In this directory, place your java class files. In the basic struts template, you will have the example package stored here.

src -> main -> java -> example -> ExampleSupport.java
src -> main -> java -> example -> Login.java
src -> main -> java -> example -> HelloWorlds.java

Directory 2:
src -> main -> resources
In this directory, place your resource files.

src -> main -> resources -> example.xml
src -> main -> resources -> struts.xml
src -> main -> resources -> NOTICE.txt
src -> main -> resources -> LICENSE.txt
src -> main -> resources -> example -> Login-validation.xml
src -> main -> resources -> example -> package.properties
src -> main -> resources -> example -> package_es.properties

Directory 3
src -> main -> webapp
Location for the jsp files

src -> main -> webapp -> example -> HelloWorld.jsp
src -> main -> webapp -> example -> Login.jsp
src -> main -> webapp -> example -> Menu.jsp
src -> main -> webapp -> example -> Missing.jsp
src -> main -> webapp -> example -> Register.jsp
src -> main -> webapp -> example -> Welcome.jsp
src -> main -> webapp -> WEB-INF -> web.xml

Directory 4
Now set up a place for your unit tests. This is actually not part of the basic struts application but could be if test cases are added by the developers.

src -> test -> java -> example -> ExampleSupportTest.java
src -> test -> java -> example -> LoginTest.java
src -> test -> java -> example -> HelloWorldsTest.java

POM File
A lot of additional configurations are placed in the POM file. Follow the Maven documentation for specifics. However, for beginners, you will want to use Eclipse or some other tool to build your base pom.xml file held at the root to the app. I added the jar files used by struts in the maven dependencies sections and it automatically filled out the appropriate sections of the POM.xml file.

My beginning POM file looks like this.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>trisummit</groupId>
  <artifactId>trisummit</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>trisummit</name>
  <issueManagement>
    <system>Jira</system>
    <url>http://applications.trisummit.net/jira/</url>
  </issueManagement>
  <ciManagement>
    <system>Bamboo</system>
    <url>http://applications.trisummit.net/bamboo/</url>
  </ciManagement>
  <scm>
   <developerConnection>Brian Nettles</developerConnection>
      <connection>svn://trisummit.net:3690/trisummit/</connection>
  </scm>
  <organization>
    <name>Trismmit Technologies</name>
    <url>http://trisummit.net</url>
  </organization>
<build> 

    <plugins> 
       <plugin> 
          <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-compiler-plugin</artifactId> 
         <version>2.1</version> 
                 <configuration> <source>1.6</source> <target>1.6</target> </configuration>
       </plugin> 
    </plugins> 
</build> 

  <dependencies>
    <dependency>
      <groupId>com.opensymphony</groupId>
      <artifactId>xwork-core</artifactId>
      <version>2.1.6</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>2.7.3</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.9</version>
      <scope>compile</scope>
    </dependency>
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.8.1</version>
        </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>20030203.000550</version>
    </dependency>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.1.8.1</version>
    </dependency>
  </dependencies>
  <distributionManagement>
    <repository>
        <id>ip-97-74-115-5.ip.secureserver.net</id>
        <name>ip-97-74-115-5.ip.secureserver.net-releases</name>
        <url>http://applications.trisummit.net/artifactory/libs-releases-local</url>
    </repository>
</distributionManagement>

</project>

Now open the command line and from the root of the application, type:

mvn compile

This will compile your application

Now type

mvn war:war

This will create your war file complete with the jar files included and place it in the target directory. Your jar files are managed from the repositories and not stored in your source control.

Now go deploy your war file on Tomcat.

Comments

Leave a Reply