Friday, June 8, 2007

Build Struts 2 Application Using Maven 2

I really think that using Maven as tool to build Java Projects is simply great, but if there is one thing I hate is searching ibiblio for the dependencies I need for my project. So I will post below the path for the main components one will need to get started with Struts 2 on his project.

By default, Maven will look for the dependencies on its central repository http://repo1.maven.org/maven2. It is possible to configure the repositories where you want maven to look up for files. These two below are some alternative repositories where you can try to find components you need. In the apache repository below is possible to find the SNAPSHOT version of the components.

Just add the code below into your pom file.


<repositories>
  <repository>
    <id>ibiblio</id>
    <name>iBiblio Maven2 Repository</name>
    <url>http://www.ibiblio.org/maven2</url>
  </repository>
  <repository>
    <id>apache-repo</id>
    <name>Apache Repository</name>
    <url>http://people.apache.org/repo/m2-snapshot-repository</url>
  </repository>
</repositories>


Check the official Maven site about how to setup multiple repositories.

Now, we can reach for those components! :)
As advised by the struts team, the minimum set of components one might need is below.

  • struts2-core.jar

  • xwork.jar

  • ognl.jar

  • freemarker.jar

  • commons-logging.jar


Fortunately, to download all of them automagically, all you need to add to your pom files are the lines below:


  <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.0.6</version>
  </dependency>


Wanna do this even faster? Use maven archetypes to create a struts2-aware project! ;)


mvn archetype:create -DgroupId=tutorial \
    -DartifactId=tutorial \
    -DarchetypeGroupId=org.apache.struts \
    -DarchetypeArtifactId=struts2-archetype-starter \
    -DarchetypeVersion=2.0.5-SNAPSHOT \
    -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository