Create Java Project With Maven

Maven is a popular java project build and management tool. It is open source and totally free. The key concept of maven is POM (Project Object Model). In every maven project there is a pom.xml file which contains project information, source directory, target build directory, test scripts directory etc. And the most important information in pom.xml is that it defines all the project used libraries ( jars ) and their versions. So when some other developer want to initiate the project, he just need to run the maven command then all the project needed jars will be downloaded and used in his local project. This can make java libraries dependency management so easy.

Before detail introduction, we will show you how to setup maven and how to create java project using it.

Steps To Install Maven On Windows

  1. Install Jdk1.7 or higher. If you have installed, you can run “java -version” in command prompt to ensure it.
  2. Setup JAVA_HOME system environment variable.
  3. You can read How To Install Jdk Correctly if you do not know how to do above steps.
  4. Go to and choose the latest stable build to download.
    apache maven download page
  5. Extract the zip file to a local folder. Such as C:\WorkSpace\\Tool\apache-mvn-3.5.0
  6. Setup M2_HOME, MAVEN_HOME, M2, MAVEN_OPTS system environment variable.
    M2_HOME’s value is C:\WorkSpace\\Tool\apache-mvn-3.5.0.
    add maven m2_home system environment variable
    MAVEN_HOME’s value is same as M2_HOME.
    add maven maven_home system environment variable
    M2’s value is %M2_HOME%\bin
    add maven m2 system environment variable
    MAVEN_OPTS’s valus is -Xms256m -Xmx512m
    add maven_opts system environment variable
  7. Append maven bin directory to system environment variable PATH then you can run maven command directly in your command prompt. Because M2’s value is %M2_HOME%\bin so we append ;%M2% to the end of PATH variable. append maven bin directory to PATH systen environment variable
  8. Run “mvn -version” in a command prompt, when you see below picture, it means the installation success.maven install verification

Maven Repository Introduction

maven repository structure

Maven Repository is a place such as a directory. You store all your project related source files, libraries, and all other resources in it. There are three kind of repository.

  1. Local
  2. Central
  3. Remote

Local maven repository is a directory in your local machine. It stored all your project related jars, plugins etc. When you initially execute maven command, it is created. When you build your java project, all the dependency jars will be downloaded from the central repository and saved into local repository. The subsequent build will use the jars stored in local repository only. This can reduce build cost time when you build your project very often. You can also change it by update %M2_HOME%\conf\settings.xml file like below.

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   | Default: ${user.home}/.m2/repository

As you can see in above code, the default local repository is


But we change it to C:\WorkSpace\MvnRepository.

Central maven repository is Apache community managed. There are almost all java libraries exist in it. You can browse it by When building a project, if it can not find any dependent jars in local repository it will search in central repository and download those jars if found. You can also search an artifact in if you want to download the artifact’s pom or jars.

browse maven repository

maven central repository search feature

Remote maven repository is used when the dependent jars can not be found in both local and central repository. You can store your java project specific required jars in it. Below is an example of remote repository.


Dependence Jars Search Order In Maven Repository

  1. Search local repository.
  2. If not found in step1 then search central repository if found then download to local.
  3. If not found in step2 then search remote repository if specified, if found then download to local.
  4. If not found in all above steps, an error will be thrown.

Steps To Create Your First Maven Java Project

There is a maven-archetype-quickstart plugin that we can used to create a java project. It is just a java project template.

  1. Open command prompt and create folder C:\WorkSpace\MvnExampleProject.
  2. Run below command in the opened window.
    cd C:\WorkSpace\MvnExampleProject
    mvn archetype:generate -DartifactId=dev2qaExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    Then you can see below output in dos console. When you see a green “BUILD SUCCESS” message, it means the java project has been setup successfully.
    maven java project build success console output

  3. After that you will find there add a lot of artifact jar folders under your local repository folder.
    local repository add new artifact folders
  4. Project folder structure has also been changed.
    maven example java project structure
  5. Project Directory Layout
    From picture in step4, we can find below directory structure in the project.


    You will find all source code saved in src/main/java and all unit testing code saved in src/test/java.

  6. Content in the pom.xml
    The pom.xml content is list as below. You can find related parameter value when we run mvn command to create this project such as groupId, artifactId, name etc. You can also add dependency if you need new jars in your java project. Because the example use JUnit as test framework, so it add junit artifact in pom.xml.
    java example project pom.xml
  7. There is also and created in the example. You just need to add source code in and test code in
  8. If you remove the content in C:\WorkSpace\MvnExampleProject folder, and run the create command again, there will not any artifact download log output in the console, because all artifacts that this project needed has been downloaded in local repository. So do not need to download again.
0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x