WARNING: you are not looking at the live version but at an older version.

Your first Cocoon application using Maven 2

In this tutorial, you will:

  1. Create a Cocoon block (the application resources and logic)
  2. Import the block as project in Eclipse
  3. Start the block as web application and access it from your browser

Creating a block

First, make sure that you have Maven 2.0.6 or above installed. You can check this by calling mvn --version from command line. If this doesn't work for you, read the Maven in 5 Minutes tutorial.

Create a new directory which will be the root directory of your Cocoon application. For this tutorial, let's name it getting-started-app.

The next step is creating a Cocoon block which will contain your custom application. The development of any Cocoon web application should be done within a block. Again, for this purpose there is a Maven archetype:

mvn archetype:create
  -DarchetypeGroupId=org.apache.cocoon
  -DarchetypeArtifactId=cocoon-22-archetype-block
  -DarchetypeVersion=1.0.0
  -DgroupId=com.mycompany
  -DartifactId=myBlock1 

Once again for copy and paste without line feeds:
mvn archetype:create -DarchetypeGroupId=org.apache.cocoon -DarchetypeArtifactId=cocoon-22-archetype-block -DarchetypeVersion=1.0.0 -DgroupId=com.mycompany -DartifactId=myBlock1

If you are using a cocoon binary build from svn trunk then you probably want to change the archetypeVersion to the most recent one, matching the svn trunk. (see [cocoon-svn]/tools/archetypes/cocoon-22-archetype-block/pom.xml)

Looking at the filesystem, you should find following directory structure:

getting-started-app
 +-myBlock1
    +-pom.xml
    +-src
       +-[...]

Import the block in Eclipse

Cocoon is not tied to Eclipse IDE by any means. This step only describes what can be done to avoid tedious work of setting up project in Eclipse manually.
If you don't use Eclipse, you can either skip this step or find a similar procedure to load the block in the IDE of your choice.

Change into the myBlock1 directory and call

mvn eclipse:eclipse

This will create the necessary project descriptors for Eclipse. In Eclipse you can import the project into your workspace: File - Import - General - Existing Projects.

If that's the first time you use Eclipse together with a project created by Maven 2, we recomment to read the Maven Guide using Eclipse with Maven 2.x.

Run the block as Java web application

After creating the block you probably want to run it. For this purpose there is a Maven plugin, that generates a minimal web application that loads your block. The pom.xml of your block already contains all necessary configurations.

You can then run

mvn jetty:run

and point your browser at http://localhost:8888/myBlock1/ and get a hello world page.

The mentioned minimal web application is automatically created, when mvn jetty:run is invoked. This happens because the rcl goal of the Cocoon plugin is bound to the Maven build lifecycle which is invoked too, when the jetty:run goal is executed. See the block's pom.xml for details.

This process can be manually triggered with mvn cocoon:rcl too.

Conclusion and further information

So far you have created a block. You have also been able to run the block in Jetty. Of course, that was only the first step in order to get a  "Hello world" as result. If you wonder how you can do some useful stuff like writing your own Cocoon pipeline or some Java code, there are more tutorials:

  • Your first XML pipeline
    Cocoon has become famous for XML pipelines. At this tutorial you will learn how to setup your first pipeline and will learn the most important things about Cocoon sitemaps.
  • Adding a second block
    While following this tutorial, you will create a web application that has a dependency on one block. There are use cases that require more than one block, e.g. you want to have all style specific resources within a single block that can be easily exchanged at deployment time (aka skinning).
  • Usage of the reloading classloader plugin
    The reloading classloader plugin enables rapid development of Cocoon applications since you don't have to restart the servlet container whenever one of your Java classes changes. Additionally it provides all settings to enable the reload of Cocoon resources too.
  • Deploying a Cocoon application
    Although you have already been able to run this block, you most probably need a Java web application (war file) in order to deploy it to the servlet container (Tomcat, Jetty, etc.) of your choice.

For the time being, we recommend the usage of Maven 2 as build system (though there is no hard dependency on it). This has the advantage that the build system is standardized and Cocoon web applications can reuse the toolset (create Eclipse configuration files, release, produce documentation, etc.) that Maven offers. More information about Maven 2 can be found at the project website. Especially we recommend reading

Comments (4)