M2GettingStarted

HomePage | RecentChanges | Preferences

Getting Started with Maven

Installation

See http://maven.apache.org/ for information. (I'm assuming you've done this already...)

Trial Run

I ran the following in IRMApps/Projects/test:

#1 $ mvn archetype:create -DgroupId=edu.asu.vpia -DartifactId=core

#2 $ mvn archetype:create -DgroupId=edu.asu.vpia -DartifactId=site -DarchetypeArtifactId=maven-archetype-site

#3 $ mvn archetype:create -DgroupId=edu.asu.vpia -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-webapp

See: [Maven Archetype Plugin Docs]

This created three directories, think of them as 'modules':

 * core, for the source code for the webapp
 * webapp, for the webapp itself
 * site, for documentation

Then I edited webapp/pom.xml to add a dependency on Struts, and on the 'core' jar (from #1 above):

  <dependency>
      <groupId>struts</groupId>
      <artifactId>struts</artifactId>
      <version>1.2.7</version>
    </dependency>

    <dependency>
      <groupId>edu.asu.vpia</groupId>
      <artifactId>core</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

(How did I know the version should be 1.0-SNAPSHOT? Because that's the default, but you could also look in core/pom.xml.)

Now add a pom.xml file at the top level (in the 'test' directory) to tell Maven that you have multiple modules in this project:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>edu.asu.vpia</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>ASU IRM Test Project</name>
    <url>http://www.asu.edu/ia/irm</url>
    <modules>
        <module>core</module>
        <module>webapp</module>
        <module>site</module>
    </modules>
</project>

And from IRMApps/Projects/test, type

#4 $ mvn install

and see what happens. :) Look in the core/target and webapp/target for the results.

To clean up:

#5 $ mvn clean
which will delete all the 'target' directories.

The Parent POM

At this point, the parent knows about its children and can build the multi-module project. The next step is to tell each child about its parent, so that the things (like dependencies) that are common to all of the children can be declared in one place, in the parent pom.xml file.

To each child, add a section like this just below <project>

  <parent>
    <groupId>edu.asu.vpia</groupId>
    <artifactId>vppa-share</artifactId>
    <version>3.0-SNAPSHOT</version>
  </parent>

(The artifactId and version should match whatever is actually _in_ the top level pom.xml file.)

A Single Module Project Instead

If you'd rather work with a single module:

Now you can build the entire webapp and/or its documentation website from a single project.

Adding Displaytag

So, we know we want to use Matt Raible's awesome Displaytag library in this project. How do we go about figuring out what to add to pom.xml?

First, visit the m2 repository on ibiblio: http://www.ibiblio.org/maven2

Then look around for the obvious, is there a 'displaytag' directory? Sure enough, and if you navigate down the structure you'll end up at:

http://www.ibiblio.org/maven2/displaytag/displaytag/1.0/

From there, you can construct the dependency:

    <dependency>
      <groupId>displaytag</groupId>
      <artifactId>displaytag</artifactId>
      <version>1.0</version>
    </dependency>

Note that the groupId may change in the future, to (probably) 'org.displaytag', which is the package name of the library.

Installing Third Party Jars

Displaytag was easy, because it's in the central Maven repository. What about jar files that either can't be distributed (like the Servlet jars from Sun) or jars from internal projects that aren't using Maven? We do that with the 'install' plugin, for example:

$ mvn install:install-file -Dfile=webauth-2_9.jar -DgroupId=edu.asu.wmac -DartifactId=webauth -Dversion=2_9 -Dpackaging=jar

and now you can declare it as a dependency

    <dependency>
      <groupId>edu.asu.wmac</groupId>
      <artifactId>webauth</artifactId>
      <version>2_9</version>
    </dependency>


Maven Tomcat Plugin

This one deserves its own page... MavenTomcatPlugin


The Struts Blank Archetype 1.3.0-SNAPSHOT

... is available [here]. (Not yet in a repository.)

$ mvn install:install-file
      -DartifactId=struts-archetype-blank 
      -Dfile=struts-archetype-blank-1.3.0-SNAPSHOT.jar 
      -DgroupId=org.apache.struts.archetypes 
      -Dpackaging=jar 
      -Dversion=1.3.0-SNAPSHOT
See: [Maven Install Plugin docs]

$ mvn archetype:create 
      -DgroupId=edu.asu.vpia
      -DartifactId=webapp 
      -DarchetypeArtifactId=struts-archetype-blank 
      -DarchetypeVersion=1.3.0-SNAPSHOT 
      -DarchetypeGroupId=org.apache.struts.archetypes

HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited November 19, 2005 2:00 pm by WendySmoak (diff)
Search: