7.2. Creating a Project

Now that we have confirmed that Maven is installed, let’s create a simple project! Maven supports both interactive and non-interactive modes for project creation. As the former is often intimidating for beginners, we will start with Maven’s non-interactive (batch) mode. We’ll make a note about how to use the interactive mode later in this tutorial once you are more familiar with the tool.

  1. In order to create a simple Maven project, you will need to remember the four options listed in the table below. Maven has more options, but the ones presented below are the bare minimum that are needed to create a new project. In the table, the literal option name is provided in the “Option” column, along with its general name and description.

    Option

    Name

    Description

    -DgroupId

    GroupId

    A group ID is a universally unique identifier for a project. Typically, you want this to be the fully qualified name of your project’s primary package (e.g., cs1302.mvn).

    -DartifactId

    ArtifactId

    An artifact is something that is either produced or used by a project. The artifact ID is used to name the directory that Maven creates as well as other things (e.g., JAR files).

    -D archetypeArtifactId

    Archetype

    An archetype is a template for the project. For most Java-based projects, the maven-a rchetype-quickstart archetype can be used.

    ` -DarchetypeVersion`

    Archetype Version

    The archetype / template version to use. At the time of this writing, the latest version of the maven-a rchetype-quickstart archetype is 1.4.

  2. With those terms in mind, let’s create a project directory for this tutorial called cs1302-mvn with a primary package called cs1302.mvn using the following command:

    mvn -B archetype:generate -DartifactId=cs1302-mvn -DgroupId=cs1302.mvn -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
    

    Given the length of that command, some developers prefer to break the command up over multiple lines. If you prefer that option, you can use the alternative command below.

    Be sure to press ENTER after each \.
    mvn -B archetype:generate \
    -DartifactId=cs1302-mvn \
    -DgroupId=cs1302.mvn \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeVersion=1.4
    

    Note

    If you omit the -DarchetypeVersion, then the oldest version is automatically used. If you omit the -DarchetypeArtifactId, then most Maven installations default to maven-archetype-quickstart for the archetype.

  3. Look inside the directory that Maven just created:

    find cs1302-mvn
    

    It should look similar to this:

    cs1302-mvn/
    cs1302-mvn/src
    cs1302-mvn/src/main
    cs1302-mvn/src/main/java
    cs1302-mvn/src/main/java/cs1302
    cs1302-mvn/src/main/java/cs1302/mvn
    cs1302-mvn/src/main/java/cs1302/mvn/App.java
    cs1302-mvn/src/test
    cs1302-mvn/src/test/java
    cs1302-mvn/src/test/java/cs1302
    cs1302-mvn/src/test/java/cs1302/mvn
    cs1302-mvn/src/test/java/cs1302/mvn/AppTest.java
    cs1302-mvn/pom.xml
    

    You may have noticed that it created some starter code for you! Here, the src/main/java subdirectory is the default package location for source code. This is a small change from previous exercises and tutorials where the default package for source code was src. A simple driver class with a fully qualified name of cs1302.mvn.App was created for you in src/main/java/cs1302/mvn/App.java. Remember, the name of the default package for source code is not included in the fully qualified name.

    The src/test/java directory is the default package location for unit tests, a topic that will be covered at a later point in time. The last file that you see above, pom.xml, contains the configuration settings/metadata for the Project Object Model (POM), which is what Maven uses to do its magic.

  4. In the future, you might try omitting the -B (batch mode) and subsequent options to use Maven’s interactive mode. Instead of specifying some of the project properties as command-line options, Maven will prompt you for their values.