Your first Cocoon application using Maven 2
In this tutorial, you will:
- Create a Cocoon block (the application resources and logic)
- Import the block as project in Eclipse
- Start the block as web application and access it from your browser
Creating a block
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-M5 -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-M5
-DgroupId=com.mycompany -DartifactId=myBlock1
Change into the myBlock1 directory and call
mvn install
from there. This installs the block into your local Maven repository in ~/.m2/repository.
Looking at the filesystem, you should find following directory structure:
getting-started-app
+-myCocoonWebapp
| +-pom.xml
| +-src
| +-[...]
+-myBlock1
+-pom.xml
+-src
+-[...]
As you can see, the web application and the block are at the same level.
Import the block in Eclipse
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, 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 run
mvn compile
and then
mvn jetty:run
and point your browser at http://localhost:8888/block1/ and get a hello world page.
The mentioned minimal web application is automatically created, when mvn compile is invoked. This happens because the rcl goal of the Cocoon plugin is baund to the Maven build lifecycle. See the block's pom.xml for details.
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 creat 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

http://localhost:8888/spring-bean
instead of
http://localhost:8888/myBlock1/spring-bean
Starting from version 7.0.1 Maven is supported. You can import the pom.xml directly into Idea. For a quick start: import the pom file and create a run/debug configuration and enter as goal "jetty:run" and hit the "run" button.
NOT work because a lot of dependencies haven't been
downloaded yet. When using the command: "mvn org.mortbay.jetty:maven-jetty-plugin:run", maven will
download all necessery dependencies first. After that,
the simple command will suffice.
But if I browse to http://localhost:8888/myBlock1/, with a trailing slash, the spring-bean link is http://localhost:8888/myBlock1/spring-bean (OK).