Maven Guide

Table of Contents

1 Installation

1.1 Windows

Download the latest version of Maven from the project website, unzip and place into C:\Program Files\apache-maven. Create a new environment variable called M2_HOME and define it to be the location to where you just copied the unarchived file. Then add %M2_HOME%\bin to your PATH. Test the result by opening a console window and entering "mvn --version" and the version info should be displayed.

1.2 Linux

Download the binary from the website, unzip it and place the directory into e.g. $HOME/Local; then symlink all the programs in the bin/ sub-directory from somewhere in your path, e.g. $HOME/bin. For the lazy, you can do it like so:

$ cd ~/bin
$ find ~/Local/apache-maven-3.0.3/bin -executable -print -exec ln -s "{}" \;

The programs in Maven are smart enough to check and see if they're symlinks (and if they are, follow the link to their real home) before attempting to locate the .jar files they are dependent on.

2 Project Object Model

Maven is controlled by a file called "pom.xml" - the POM stands for Project Object Model.

3 Java Servlets

3.1 Compiling Servlets

The servlet API is one of those javax things; normally the implementation that you use is specific to the servlet container, but it implements a common interface. When we deploy, the servlet container's implementation will automatically get used, but at build time we need something obviously or else the thing won't compile right. Rather than specifying which servlet container to use (which in reality can be variable), specify the generic interface as the dependency like so:

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>

3.2 Running a Servlet with Jetty

It's possible to execute your servlet using Maven, without needing an external servlet container, using Jetty. To do this, drop the following into the build part of your pom.xml file:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.19</version>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <connectors>
      <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
        <port>8666</port>
        <maxIdleTime>60000</maxIdleTime>
      </connector>
    </connectors>
  </configuration>
</plugin>

You can then start the Jetty server using "mvn jetty:run" - using this configuration, the server will be listening on port 8666. If you go directly to the root directory then you'll get a mapping of the registered servlets. Note that when using this technique, any code or configuration that you modify and save will automatically recompile and be loaded into the running Jetty container, making this method super awesome for quick turnaround when doing rapid development with HTML, CSS, JavaScript, etc.

3.2.1 Fixing Jetty File Locking on Windows

In the default configuration, Jetty will cause static file content (such as style sheets and JavaScript code) to be locked in Windows. To fix this, you need to copy the Jetty webdefault.xml file and change the useFileMappedBuffer to false. Then update the Jetty plugin configuration in the pom.xml file to reference your tweaked webdefault.xml file.

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <configuration>
    <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
    <!-- other config -->
  </configuration>
</plugin>

4 Scala

To use Scala from Maven, we must first add a repository to our Maven configuration which provides all the Scala stuff we might need.

The first repository here provides everything that we'll need as far as libraries go. For example, the repository includes the Scala language itself, the Lift web framework, and many more. You can browse the Maven repository at http://scala-tools.org/repo-releases/.

<repositories>
  <repository>
    <id>scala-tools.org</id>
    <name>Scala-tools Maven2 Repository</name>
    <url>http://scala-tools.org/repo-releases</url>
  </repository>
</repositories>

The plugin repository of course includes all the Maven plugins that are required to deal with Scala, in particular for invoking the compiler during the appropriate lifecycle phase.

<pluginRepositories>
  <pluginRepository>
    <id>scala-tools.org</id>
    <name>Scala-tools Maven2 Repository</name>
    <url>http://scala-tools.org/repo-releases</url>
  </pluginRepository>
</pluginRepositories>

4.1 Compiling Scala Code

You can compile Scala code from Maven without having Scala actually installed on your machine. This is possible because the Scala language is provided as .jar files that can be loaded by the JVM with no extra setup. This is how the Scala plugin for Maven operates, and this is also handy because it allows you to specify what version of Scala to use.

<dependencies>
  <dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.8.1</version>
  </dependency>
</dependencies>

Now in our description of the build lifecycle phase, we have to configure the plugin and indicate for which goals we want it to be invoked. You can find more information at http://scala-tools.org/mvnsites/maven-scala-plugin/.

<build>
  <sourceDirectory>src/main/scala</sourceDirectory>
  <testSourceDirectory>src/test/scala</testSourceDirectory>
  <plugins>
    <plugin>
      <groupId>org.scala-tools</groupId>
      <artifactId>maven-scala-plugin</artifactId>
      <version>2.15.2</version>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>testCompile</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <useFsc>true</useFsc>
        <sourceDir>src/main/scala</sourceDir>
        <jvmArgs>
          <jvmArg>-Xms64m</jvmArg>
          <jvmArg>-Xmx1024m</jvmArg>
        </jvmArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

4.2 Testing with ScalaTest

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.2</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.scalatest</groupId>
    <artifactId>scalatest</artifactId>
    <version>1.3</version>
    <scope>test</scope>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.7.1</version>
      <configuration>
        <includes>
          <include>**/*Spec.class</include>
        </includes>
      </configuration>
    </plugin>
  </plugins>
</build>

5 Help Online / Links

Date: 2013-07-03T20:42-0400

Author: Taylor Venable

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0