Maven setup

This tutorial will show you how to setup Apache Maven in development environment.

Prerequisites

Maven is a Java tool, so you must have Java installed in order to proceed. Maven 3.3 requires JDK 1.7 or above to execute. So first install Java 1.7 and setup environment path for Java.
Installations

1. Download the binary distribution archive from the link https://maven.apache.org/download.cgi
2. Extract the downloaded archiev file in C: drive or anywhere else as per your convenience.

3. Now do right-click on Computer. Then click on Properties. Then click on Advance system settings in new window. Then go to Advance tab in new window and click on Environment variables… Then in user variables section create one variable called M2_REPO by clicking on New button and value should be like C:apache-maven-3.3.9bin as shown below in the picture

maven setup

We have created variables in user variables section because we want to use those variables only for this particular user in OS profile.

4. Then edit the Path variable in user variables section or if Path variable does not exist then create the Path variable and add the M2_REPO to it like below

maven setup

Now to verify if maven is correctly setup open command prompt and type mvn –version, you will see the below output

maven setup

If you see the command executed successfully then you can create a maven based project from command prompt. You will need somewhere for your project to reside, create a directory somewhere and start a shell or command prompt in that directory. On your command line, execute the following Maven goal:

mvn archetype:generate -DgroupId=com.roytuts -DartifactId=java-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

You will notice that the generate goal created a directory with the same name given as the artifactId.

If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete.

In the above command

-DgroupId : it is the groupId of the project generally given company name
-DartifactId : It is project name
-DarchetypeArtifactId : It specifies what type of project will be created, for example, if you want to create web based application then you have to mention maven-archetype-webapp

Under this directory you will notice the standard project structure as defined in https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

The src/main/java directory contains the project source code, the src/test/java directory contains the test source, and the pom.xml file is the project’s Project Object Model, or POM.

Configuring Maven

Now we have finished maven installation part but we need more configurations to maven because we need to point to the repository where all jar required libraries for a project will be downloaded and to dowload jar files we need to setup the remote repository URL from where jar will be downloaded to our local repository.

So we can specify our user configuration in ${user.home}/.m2/settings.xml or C:apache-maven-3.3.9confsettings.xml.

I will here use C:apache-maven-3.3.9confsettings.xml to configure the maven settings. Now open settings.xml file and setup local repository in C:repo where all required jar files will be downloaded from the remote repository

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>C:repo</localRepository>
  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->
  ...more

Setup remote repository from where jar files will be downloaded. I am using UK Central for downloading files.

...
<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
        <id>UK</id>
        <name>UK Central</name>
        <url>http://uk.maven.org/maven2</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>
...

Configuring maven with Eclipse

Most of us using IDE to create our projects because IDE helps us to create project easily with less effort. So here I will show how to configure maven with Eclipse so that we can create or build maven based project from Eclipse and we no longer will require command prompt for creating or building the project.

So to configure maven in Eclipse do the following steps

1. Open Eclipse
2. Go to Windows -> Preferences. In Preferences window expand Maven and click on Installations. Then click on Add button and select the apache maven installation directory in C: drive. Click Finish.
3. Now click on Maven -> User Settings from the expanded maven menu in the same way as previously clicked on Installations. Now browse the settings.xml file from maven installation directory and click on OK.

Now you are done and you can create maven based project directly from the Eclipse. To create a new maven project go to File -> New -> Other. In the New popup window under Maven select Maven Project and click Next. Then select workspace and click Next. In nexy window if you want to create standalone project then select option having Artifact Id maven-archetype-quickstart or if you want to create web based project then select option having Artifact Id maven-archetype-webapp and click Next. In next window give groupId(for example, com.roytuts) and artifactId(for example, java-app) and click Finish.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *