Link Search Menu Expand Document

Quick version

First, read the official Apache Maven in 5 minutes guide, and practice using Maven with a “Hello World” type scratch project so that you understand how Maven works BEFORE trying to convert your first pre-existing Ant based project to Maven.

Now, assuming that your CS56 project is set up in the normal way with Ant:

  1. You probably have a src directory has has under it, the packages, e.g. src/edu/ucsb/cs56/package/

    To migrate this to the Maven way:

    • Create a directory src/main/java with mkdir -p src/main/java
    • Move everything in src to src/main/java:
       mv src/* src/main/java

      You’ll get a message that src/main/java can’t be moved into itself, which is normal, e.g.

       mv: rename src/main to src/main/java/main: Invalid argument

      But the rest of the files should be moved ok, and then the edu/ucsb/cs56... tree should be under src/main/java.

  2. If you have a directory with resources, put those files in src/main/resources

  3. If you have tests, those go under src/test/java/ then the full package name, e.g. src/test/java/edu/ucsb/cs56/ The Maven convention is to keep test code separate from regular code. Instead, you need to create a directory java sources to src/main/java,

  4. Create a pom.xml following this convention.

Be sure to change YOUR_PROJECT_NAME_HERE to the name of your project, and also fix the line with the name of the Main class that you want to run (look for ClassWithTheMainInIt in the example pom.xml below.)

<project xmlns="" xmlns:xsi=""





  <!-- Make jar file executable -->
              <!-- full package name of class with the main you want to run -->


Then, you should be able to at least do:

mvn compile
  1. If you get errors that some features are not supported in a given version of Java, you may need to modify the pom.xml to indicate the level of Java needed. For example:
   [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project cs56Parser: Compilation failure
   [ERROR] /Users/pconrad/github/lab06_starter_code/src/main/java/edu/ucsb/cs56/pconrad/parsing/tokenizer/      [33,27] lambda expressions are not supported in -source 1.5 
   [ERROR] (use -source 8 or higher to enable lambda expressions)
   [ERROR] -> [Help 1]

Here is an article that explains how to set the version of the compiler. Or, here’s the quick version: add this to your pom.xml:

  1. The next problem you may encounter is this one:
[INFO] -------------------------------------------------------------
[ERROR] /Users/pconrad/github/lab06_starter_code/src/main/java/edu/ucsb/cs56/pconrad/parsing/parser/[8,24] package org.junit does not exist

This may be because you have code that references JUnit under the src/main/java directory, when it should be under the src/test/java directory. Disentangling these for a complex project may be complicated, but for many CS56 projects with comparatively little test coverage, it may not be that hard. The idea is to move all the classes that reference JUnit into a parallel directory hierarchy under src/test/java/... where the ... is the package name.

Start with the first file that is reporting an error.

Copy paste its full path name from the error message into the terminal, with an ls command to make sure you have the name right.

   ls /Users/pconrad/github/lab06_starter_code/src/main/java/edu/ucsb/cs56/pconrad/parsing/parser/

Then, use your up arrow to edit the command. Remove the filename from the end, replace ls with mkdir -p and replace src/main/java with src/test/java, like this:

   mkdir -p /Users/pconrad/github/lab06_starter_code/src/test/java/edu/ucsb/cs56/pconrad/parsing/parser/

Then, again using copy/paste, mv the filename of the offending file into the directory you just created:

   mv /Users/pconrad/github/lab06_starter_code/src/main/java/edu/ucsb/cs56/pconrad/parsing/parser/ /Users/pconrad/github/lab06_starter_code/src/test/java/edu/ucsb/cs56/pconrad/parsing/parser/

Run the compile again. With luck and skill, the first file will no longer be on the list of complaints. Repeat until you get them all.

  1. To generate javadoc, you’ll need to include the Maven javadoc plugin.

Put this plugin in the <plugins> element of your pom.xml:



The most commonly used maven goals for this plugin are these (more can be found here):

Goal Description
mvn javadoc:javadoc generates the Javadoc files for the project
mvn javadoc:test-javadoc generates the test Javadoc files for the project

The configuration part in the pom.xml snippet above overrides the default for where the generated javadoc goes, setting it to docs. This is compatible with publishing the docs via github pages.

To make a link to that directory, you’ll need a link to where OWNER is the name of the organization or user that owns the repo.

In addition, you may want to customize the mvn clean goal so that it wipes out the docs/apidocs directory when mvn clean is run. To do that, add this plugin as well:



  • This Stack Overflow Answer has at least some good information, though still leaves a lot out.
  • The page How to convert from “Ant to Maven in 5 minutes” from “Sonotype” comes up often in web searches, and is mentioned in Stack Overflow articles on the subject, but doesn’t really have a lot of specifics, and in my view isn’t particularly helpful to beginners. YMMV.