Creating project using Maven

Maven is a tool used for building and managing any Java-based project. Maven helps in:

  • Making the build process easy
  • Providing a uniform build system. Maven allows a project to build using its project object model (POM) and a set of plugins that are shared by all projects using Maven, providing a uniform build system.
  • Providing quality project information. This information is taken from the POM and in part generated from the project’s sources. For example, Maven can provide change log document created directly from source control, cross referenced sources, unit test reports including coverage
  • Providing guidelines for best practices development such as keeping the test source code in a separate, but parallel source tree, using test case naming conventions to locate and execute tests, etc.
  • Allowing transparent migration to new features

Steps in creating project using Maven:

  1. Install Maven and Java. Test the Maven installation by running mvn from the command-line:

    mvn -v

    If installation goes well, you should see similar information like below about the maven installation:

    Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T12:57:37+01:00)
    Maven home: /usr/local/Cellar/maven/3.3.3/libexec
    Java version: 1.8.0_60, vendor: Oracle Corporation
    Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: “mac os x”, version: “10.10.5”, arch: “x86_64”, family: “mac”

  2. Now create a project from a Maven template. In the command-line navigate to the folder you want to create the java project and type the below command:

    mvn archetype:generate -DgroupId=com.roytech -DartifactId=goodnews -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    It tells Maven to create a Java project from Maven maven-archetype-quickstart template. In -DgroupId we have given the group or organization’s name that the project belongs to. Often expressed as an inverted domain name. In -DartifactId we have given the project’s library artifact (for example, name of its JAR or WAR file).

  3. With mvn archetype:generate + maven-archetype-quickstart template, the following project directory structure is created:

    In simple, all source codes are in folder /src/main/java/, all test codes are in /src/test/java/. In addition, a standard pom.xml gets generated. This POM file contains the entire project information, everything from directory structure, project plugins, project dependencies, information related to project building, etc. It includes the following details of the project configuration:

    • modelVersion – POM model version (always 4.0.0)
    • groupId – Group or organization that the project belongs to. Often expressed as an inverted domain name
    • artifactId – Name to be given to the project’s library artifact (for example, the name of its JAR or WAR file)
    • version – Version of the project that is being built
    • packaging – How the project should be packaged. Defaults to “jar” for JAR file packaging. Use “war” for WAR file packaging

  4. Maven is now ready to build the project. You can execute several build lifecycle goals with Maven now, including goals to compile the project’s code, create a library package (such as a JAR file), and install the library in the local Maven dependency repository.

    To try out the build, issue the following at the command line:

    mvn compile

    This will run Maven, telling it to execute the compile goal. When it’s finished, you should find the compiled .class files in the target/classes directory.

    Since it’s unlikely that you’ll want to distribute or work with .class files directly, you’ll probably want to run the package goal instead:

    mvn package

    The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within the target directory. The name of the JAR file will be based on the project’s and . For example, given the minimal pom.xml file from before, the JAR file will be named goodnews-1.0-SNAPSHOT.jar.

    If you’ve changed the value of from “jar” to “war”, the result will be a WAR file within the target directory instead of a JAR file.

    Maven also maintains a repository of dependencies on your local machine (usually in a .m2/repository directory in your home directory) for quick access to project dependencies. If you’d like to install your project’s JAR file to that local repository, then you should invoke the install goal:

    mvn install

    The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency.

  5. Understanding dependency in pom.xml.

    If you look into the pom.xml, there is junit dependency is present. Junit is used for unit testing.


    This block of XML declares a list of dependencies for the project. Specifically, it declares a single dependency for the Junit library. Within the “dependency” element, the dependency coordinates are defined by three sub-elements:

    • <groupId> – The group or organization that the dependency belongs to
    • <artifactId> – The library that is required
    • <version> – The specific version of the library that is required

    By default, all dependencies are scoped as compile dependencies. That is, they should be available at compile-time (and if you were building a WAR file, including in the /WEB-INF/libs folder of the WAR). Additionally, you may specify a <scope> element to specify one of the following scopes:

    • provided – Dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code (e.g., the Java Servlet API)
    • test – Dependencies that are used for compiling and running tests, but not required for building or running the project’s runtime code

    Now if you run mvn compile or mvn package, Maven should resolve the Junit dependency from the Maven Central repository and the build will be successful.

Creating project using Maven

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s