//
you're reading...

Development

Create your own custom maven archetype

Creating custom maven archetype

Maven provides many archetypes to generate various types of project skeletons. This blog post explains how to create a custom archetype that is tailor-made for your own situation. We will create an archetype for the Struts2 application.
My current development environment includes:

  • Mac OS X 10.6.4
  • Maven 2.2.1
  • Java 1.6.0_20

Create your skeleton project

You need to have a starter project from which new archetype can be created. We can start with the Struts2 application created in the blog post Starting Struts2 web application development (using Maven2 and Eclipse) to create a custom archetype for Struts2 based applications. You can download the zipped source code for this project here.

Generate your archetype

  • Go to your project directory (where pom.xml is) and run archetype:create-from-project target.
    create-from-project
  • The previous step creates the archetype in target/generated-sources/archetype directory.
    generated_sources_for_archetype

Install the archetype locally

The new archetype can easily be installed in your local maven repository by running mvn install in the target/generated-sources/archetype directory.
install_archetype_locally

This step creates a metadata file called archetype-catalog.xml under your local maven repository directory.
archetype-catalog-xml

Use this new archetype to create new Struts2 project

  • Now, you are ready to use this archetype using mvn archetype:generate command. Use
    -DarchetypeCatalog=local
    to use your local repository instead of getting all the archetypes from the maven central repository.
    generate-archetype
  • The above command generates the following tree structure.
    new-struts2-directory-structure
  • The following screen shot shows that the struts.xml reflects the new package structure for the HelloAction class.
    new-struts-xml
  • Test your new application

    At this point you are ready to test your new Struts2 application.
    Run mvn jetty:run from your new project directory.
    run-new-application

    And then in the browser, try these sample URLs to test the application:

    1. Hello World: http://localhost:8080/hello/hello.action
      Boring Hello World
    2. Hello World with the twist: http://localhost:8080/hello/hello.action?name=New%20John%20Doe&zip=02451&age=40&gender=M
      new_twisted_hello_world

Discussion

No comments yet.

Post a Comment


*

Categories