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 a project in Eclipse
- Start the block as a web application and access it from your browser
Creating a Cocoon blockCreate a new directory which will be the root directory of your Cocoon application and change into it. For this tutorial, let's name it getting-started-app.The next step is to create a Cocoon block to contain your custom application. The development of any Cocoon web application should be done within one or more blocks. Cocoon blocks are little different to other Maven blocks except they have a particular expected directory and file structure.
mvn archetype:generate -DarchetypeCatalog=http://cocoon.apache.org
Maven will generate some output and then interactively ask you for some information:
Choose archetype: 1: remote -> cocoon-22-archetype-block-plain (Creates an empty Cocoon block; useful if you want to add another block to a Cocoon application) 2: remote -> cocoon-22-archetype-block (Creates a Cocoon block containing some small samples) 3: remote -> cocoon-22-archetype-webapp (Creates a web application configured to host Cocoon blocks. Just add the block dependencies) Choose a number: (1/2/3): 2
Maven first asks which archetype you want to create. Item 2 in this list is the standard Cocoon block with some small sample files already in place.
Define value for groupId: : com.mycompany Define value for artifactId: : myBlock1 Define value for version: 1.0-SNAPSHOT: : 1.0.0 Define value for package: : com.mycompany.myBlock1
Maven then asks a few more questions. Just enter the details as shown above. The groupId is a label that you can use to group all the blocks in your application. The artifactId is the unique label that will identify this block from all others. It is also the name of the directory that will be created to contain the block's files. The package is the Java package that any Java files in the block will be assigned to.
Looking at the filesystem, you should find following directory structure:
getting-started-app +-myBlock1 +-pom.xml +-src +-[...]
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
This will create the necessary project descriptors for Eclipse. In Eclipse you can import the project into your workspace: File > Import... > Existing Projects into Workspace
If that's the first time you use Eclipse together with a project created by Maven 2, we recommend you read the Maven Guide using Eclipse with Maven 2.x.
Run the block as Java web application
After creating the Cocoon 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 the necessary settings.
and point your browser at http://localhost:8888/myBlock1/ and get a hello world page.
Conclusion and further information
So far you have created a Cocoon block and been able to run it in Jetty. This is only the first step to creating a full-blown Cocoon application. If you would like to know more read the following tutorials:
- Your first XML pipeline
Cocoon is well known for its XML pipelines. In this tutorial you will learn how to setup your first pipeline and discover the power of Cocoon sitemaps.
- Adding a second block
This tutorial creates a web application that has a dependency on another 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 enables the reloading of Cocoon resources too if they change.
- Deploying a Cocoon application
Eventually, you will probably need to create a Java web application (war file) in order to deploy your application to a servlet container such as Tomcat or Jetty, etc. This tutorial shows you how.