Tapestry Training -- From The Source

Let me help you get your team up to speed in Tapestry ... fast. Visit howardlewisship.com for details on training, mentoring and support!

Saturday, January 31, 2009

Using Maven to create a new Tapestry 5.1 project

I've been digging deep into the (revised) Maven archetype plugin: the tool used to generate new projects from a template. The existing quickstart archetype had some glaring omissions, partially related to limitations of the old plugin. The new plugin makes many more things possible.

In the past, you needed to specify the new project's groupId, artifactId and other data on the command line. This process was so tedious and error prone that I advised wrapping it up in a Ruby script.

Now it's a bit easier, as Maven will ask you for any necessary properties:

$ mvn archetype:generate -DarchetypeCatalog=http://tapestry.formos.com/maven-snapshot-repository
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: http://tapestry.formos.com/maven-snapshot-repository -> quickstart (Tapestry 5.1.0.0-SNAPSHOT Quickstart Project)
Choose a number:  (1): 1
[INFO] snapshot org.apache.tapestry:quickstart:5.1.0.0-SNAPSHOT: checking for updates from quickstart-repo
Define value for groupId: : com.formos 
Define value for artifactId: : demo1
Define value for version:  1.0-SNAPSHOT: : 
Define value for package:  com.formos: : com.formos.demo1.web
Confirm properties configuration:
groupId: com.formos
artifactId: demo1
version: 1.0-SNAPSHOT
package: com.formos.demo1.web
 Y: : 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24 seconds
[INFO] Finished at: Sat Jan 31 13:12:31 PST 2009
[INFO] Final Memory: 8M/14M
[INFO] ------------------------------------------------------------------------
~/work
$ 

The key part is the -DarchetypeCatalog=http://tapestry.formos.com/maven-snapshot-repository, which points Maven at Tapestry's own nightly snapshot repository. It picks up a catalog file from there which points to the quickstart archetype stored inside the snapshot repository. No muss, no fuss.

Of course, the first time you run this, Maven has to download about 34MB of modules!

In any case, it works nicely. I put some extra effort in so that the archetype description clearly identifies the exact version of Tapestry.

The new quickstart archetype is much improved; it now includes a Layout component that wraps the page content using a free CSS layout, "concrete" (which has a Creative Commons license). In other words, your application will look good from day 1, though (of course) I'd expect you to throw away the layout subsequently. Anyway, this will answer some questions such as: "how do I use a Layout component?" and "how do I access a message catalog?"

I can see replacing the "concrete" layout with something more general, less blog-oriented (but that's beyond my CSS/HTML design skills!).

All of this is all usable right now. Have fun!

6 comments:

Kevin Menard said...

This is definitely a lot easier to use than the old archetype. It would be nice if default values were actually selected. I shouldn't have to type "1" for the catalog if it's the only one and it's the default value. Is that just the way maven works?

Unknown said...

Yep, it's Maven. The right solution is to provide more archetypes (i.e., one based on quickstart that includes Hibernate support ... or Cayenne once that's ready!).

Unknown said...

For those of us unlucky enough to work behind a web proxy...

I set maven to use the company's proxy, as described in here.

Somehow, this does not work, and Maven ignores my proxy. But we can always tell the JVM to use the proxy:

MAVEN_OPTS="-Dhttp.proxyHost=my_proxy_host -Dhttp.proxyPort=my_proxy_port" mvn archetype:generate -DarchetypeCatalog=http://tapestry.formos.com/maven-snapshot-repository

Unknown said...

This post is from Jan 2009, but it's already May 2009. Tap 5.1 is no longer a snapshot, but the current prod release.

Thus, prefer Formos' Maven repo over Formos' snapshot repo:

(Good) mvn archetype:generate -DarchetypeCatalog=http://tapestry.formos.com/maven-repositoryas opposed to

(Bad) mvn archetype:generate -DarchetypeCatalog= http://tapestry.formos.com/maven-snapshot-repository

Jeanne Rash said...

Its April 2010 and I am looking for a quickstart that will generate the Eclipse .project and .classpath so it can be imported into Ecplipse 3.4 as demonstrated in the Tapestry Tutorial #1

I have tried http://tapestry.formos.com/maven-repository but it no longer creates the Eclipse project files?

BTW: I am a Struts user looking to convert an enterprise app to Tapestry and greatly looking forward to getting the going!

Unknown said...

@Jeanne: you can use mvn eclipse:eclipse to generate your .project and .classpath files

Better yet, if you install M2Eclipse, in Eclipse you can import a Maven project (as opposed to import "Existing project into workspace").

This latter approach adds a library called "Maven dependencies", which handles the dependencies declared in your POM.