Maven Notes

These notes lean mostly toward m2eclipse since I'm an Eclipse developer.


Maven links

Here are some useful links to Maven topics (except Eclipse integration; for that read starting the next cartouche):


Maven-Eclipse integration

There are two (major) contenders for integrating and using Maven in Eclipse. I use m2eclipse. The other one is Eclipse Integration for Apache Maven (Eclipse IAM), formerly called Q for Eclipse.

The choice was sort of arbitrary, but I do have my reasons: the first is older and they had videos explaining how to use it. The second is far more geeky in its installation: you have to squint, read and synthesize in order to use it.

Sonatype's m2eclipse...


Maven-Eclipse integration video links

The videos available for m2eclipse are crucial. They demonstrate

  1. Installing
  2. Creating a simple project
  3. Editing pom.xml
  4. Setting up a very complex set of interrelated projects
  5. Still other videos

In particular, the multimodule project stuff looks very exciting.

m2eclipse supports a number of archetypes including simple projects and even web application projects. When they create a web application in the video, however, it does not appear to be an Eclipse WTP Dynamic Web Project (based on the resulting project icon), but this might not matter anyway (as long as one can write a web application project).


m2eclipse available archetypes (list)

Among some 300 available are:

  • maven-archetype-quickstart
  • maven-archetype-j2ee-simple
  • maven-archetype-plugin
  • maven-archetype-webapp
  • softeu-archetype-jsf
  • softeu-archetype-seam
  • myfaces-archetype-helloworld-facelets
  • struts2-archetype-starter

I found that I hate these. I want to do my own thing and I can't make these work so well for me. Maybe my attitude will change?


Walk the walk...

Let's get started. This will chronical my way through the Apache Maven Project mini guide and some Sonatype video tutorials. It's chaotic, but I may someday be able to make something of this.

  1. I downloaded and extracted Maven from the left-hand navigation thumb at the mini guide page.
    
    gunzip -d apache-maven-3.0.2-bin.tar.gz
    tar -xf apache-maven-3.0.2-bin.tar
    
  2.  
  3. I invoke Maven experimentally
    [email protected]:~/dev/downloads/apache-maven-3.0.2> ./bin/mvn
    Warning: JAVA_HOME environment variable is not set.
    (lot's more crap here...)
    
     
  4. In consequence, I go set and export JAVA_HOME and M2_HOME for my shell. This is important since I know that a lot of the time, I may use Maven from outside Eclipse (on the command line). I add this to the bottom of .profile:
    export JAVA_HOME=/home/russ/dev/jdk1.6.0_21
    export M2_HOME=/home/russ/dev/downloads/apache-maven-3.0.2
    
    # Add Maven and Java to my path...
    PATH="$PATH:${JAVA_HOME}/bin:${M2_HOME}/bin"
    
     
  5. Lifes good (there's no POM here and nothing to build, but Maven knows where to find Java):
    [email protected]:~/dev/downloads/apache-maven-3.0.2> ./bin/mvn --version
    Apache Maven 3.0.2 (r1056850; 2011-01-08 17:58:10-0700)
    Java version: 1.6.0_21, vendor: Sun Microsystems Inc.
    Java home: /home/russ/dev/jdk1.6.0_21/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "2.6.35-25-generic", arch: "amd64", family: "unix"
    
     
  6. It says to set up the classpath variable (in Eclipse) thus, presumably for each workspace in which I intend to use Maven:
    mvn -Declipse.workspace= eclipse:add-maven-repo
    

    I think I'll work in a new Eclipse workspace just for Maven experimentation. I'm not running Eclipse right now. Also, I have not installed any Maven plug-ins, so I don't really know what this is going to do.

    mvn -Declipse.workspace=/home/russ/dev/workspaces/maven-workspace eclipse:add-maven-repo
    

    This appears to be populating /home/russ/.m2/repositor.

    [email protected]:~/.m2> tree repository/
    repository/
    `-- org
        |-- apache
        |   `-- maven
        |       `-- plugins
        |           |-- maven-antrun-plugin
        |           |   `-- 1.3
        |           |       `-- maven-antrun-plugin-1.3.pom.lastUpdated
        |           |-- maven-assembly-plugin
        |           |   `-- 2.2-beta-5
        |           |       `-- maven-assembly-plugin-2.2-beta-5.pom.lastUpdated
        |           |-- maven-clean-plugin
        |           |   `-- 2.4.1
        |           |       `-- maven-clean-plugin-2.4.1.pom.lastUpdated
        |           |-- maven-dependency-plugin
        |           |   `-- 2.1
        |           |       `-- maven-dependency-plugin-2.1.pom.lastUpdated
        |           |-- maven-deploy-plugin
        |           |   `-- 2.5
        |           |       `-- maven-deploy-plugin-2.5.pom.lastUpdated
        |           |-- maven-install-plugin
        |           |   `-- 2.3.1
        |           |       `-- maven-install-plugin-2.3.1.pom.lastUpdated
        |           |-- maven-release-plugin
        |           |   `-- 2.0
        |           |       `-- maven-release-plugin-2.0.pom.lastUpdated
        |           |-- maven-site-plugin
        |           |   `-- 2.0.1
        |           |       `-- maven-site-plugin-2.0.1.pom.lastUpdated
        |           `-- resolver-status.properties
        `-- codehaus
            `-- mojo
                `-- resolver-status.properties
    
    22 directories, 10 files
    
  7.  
  8. Tailoring the conf/settings.xml file:
      <localRepository>/home/russ/.m2/repository</localRepository>
    	.
    	.
    	.
      <proxies>
        <proxy>
          <id>optional</id>
          <active>true</active>
          <protocol>http</protocol>
          <host>web-proxy.austin.hp.com</host>
          <port>8080</port>
        </proxy>
      </proxies>
    
    
  9.  
  10. Planning on doing a tutorial, I created /home/russ/dev/maven-tutorial, went there, and created the project thus:
    [email protected]:~/dev/maven-tutorial> mvn archetype:create -DgroupId=com.mytutorial \
        -DartifactId=simple
    [INFO] Scanning for projects...
    Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-clean-\
        plugin/2.4.1/maven-clean-plugin-2.4.1.pom
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-clean-\
        plugin/2.4.1/maven-clean-plugin-2.4.1.pom (5 KB at 3.9 KB/sec)
    Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-\
        plugins/18/maven-plugins-18.pom
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-\
        plugins/18/maven-plugins-18.pom (13 KB at 18.4 KB/sec)
    Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-parent/16/\
        maven-parent-16.pom
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/maven-parent/16/\
        maven-parent-16.pom (23 KB at 34.1 KB/sec)
    .
    .
    .
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/doxia/doxia-sink-\
        api/1.0-alpha-9/doxia-sink-api-1.0-alpha-9.jar (10 KB at 2.5 KB/sec)
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker\
        /2.0.10/maven-invoker-2.0.10.jar (28 KB at 7.7 KB/sec)
    Downloaded: http://repo1.maven.org/maven2/commons-collections/commons-collections\
        /3.2.1/commons-collections-3.2.1.jar (562 KB at 114.1 KB/sec)
    [WARNING] This goal is deprecated. Please use mvn archetype:generate instead
    [INFO] Defaulting package to group ID: com.mytutorial
    Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-\
        archetype-quickstart/maven-metadata.xml
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-\
        archetype-quickstart/maven-metadata.xml (531 B at 0.6 KB/sec)
    Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-\
        archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-\
        archetype-quickstart/1.1/maven-archetype-quickstart-1.1.jar (7 KB at 10.8 KB/sec)
    Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-\
        archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom
    Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-\
        archetype-quickstart/1.1/maven-archetype-quickstart-1.1.pom (2 KB at 2.3 KB/sec)
    [INFO] ----------------------------------------------------------------------------
    [INFO] Using following parameters for creating project from Old (1.x) Archetype: \
        maven-archetype-quickstart:RELEASE
    [INFO] ----------------------------------------------------------------------------
    [INFO] Parameter: groupId, Value: com.mytutorial
    [INFO] Parameter: packageName, Value: com.mytutorial
    [INFO] Parameter: package, Value: com.mytutorial
    [INFO] Parameter: artifactId, Value: simple
    [INFO] Parameter: basedir, Value: /home/russ/dev/maven-tutorial
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] ********************* End of debug info from resources from generated POM
    [INFO] project created from Old (1.x) Archetype in dir: /home/russ/dev/maven-\
        tutorial/simple
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1:31.955s
    [INFO] Finished at: Mon Jan 31 11:42:52 MST 2011
    [INFO] Final Memory: 9M/118M
    [INFO] ------------------------------------------------------------------------
    
    [email protected]:~/dev/maven-tutorial> tree simple
    simple
    |-- pom.xml
    `-- src
        |-- main
        |   `-- java
        |       `-- com
        |           `-- mytutorial
        |               `-- App.java
        `-- test
            `-- java
                `-- com
                    `-- mytutorial
                        `-- AppTest.java
    
    9 directories, 3 files
    
    [email protected]:~/dev/maven-tutorial> cd simple
    [email protected]:~/dev/maven-tutorial/simple> cat pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org\
         /2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    	http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.mytutorial</groupId>
      <artifactId>simple</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>simple</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    
  11.  
  12. Installed m2eclipse following the first two videos. Couldn't get right-click Run As -> Maven install to work. Network appeared very slow and ultimately failed. Bailed to command line where, in ~/, I ran
    [email protected]:~/dev/workspaces/maven-workspace/example-project> cat pom.xml
    

    which worked fairly quickly and succeeded.