03. ♥ Setting up Maven step by step tutorial

This is mainly for the beginners of Java to get started with Maven. This is required not only for learning Java, but also conducive to creating your self-taught Java projects. Extends Setting up Java step by step tutorial. This is an industrial strength tool to build Java applications.

Install Maven

Maven is a build, dependency management (i.e. jar files dependency) and deployment tool.

Step 1: Download and install Maven from:

http://maven.apache.org/download.html

Download the binary “zip” file “apache-maven-3.5.0-bin.zip” if in Windows or the “apache-maven-3.5.0-bin.tar.gz” if in Unix, and unpack them into the “tools” folder.

Install Maven

Step 2: Configure the environment variables “M3_HOME” and “path”. For example

On DOS

Previously in Java installation tutorial “environment-setup.cmd” was created under “c:\scripts”. Add Maven environment settings to that file.

Run the script:

On Unix

type the following in “.profile”

Verify installation

Once maven has been set up correctly, you should be able to verify it by

also, “where” in DOS and “which” in Unix.

Configure Maven

You can set up the repository location by opening the settings.xml file under conf folder inside your maven installation directory “c:\tools\apache-maven-3.5.0-bin\apache-maven-3.5.0\conf”. Repository is where the dependency Java libraries will be stored.

Rename the settings.xml to settings.xml_old, and create a new “settings.xml” as shown below with a basic config.

Local Repository

Are you behind a firewall?

If your internet access requires proxy settings, you need have that added to your settings.xml file.

Create a Maven project

A maven project is nothing but a Java project as we saw earlier with a predefined structure. Maven follows a certain convention in terms of the directory structure.

Step 1: Create a Java project using Maven. “com.mytutorial” is a package inside “src/main/java”.

Step 2: “pom.xml” file looks as shown below.

The pom.xml file is where you define the dependent libraries like “hibernate”, “spring”, “junit”, etc. Change the version to the latest possible. For example: “4.9” as per

http://repo1.maven.apache.org/maven2/junit/junit/4.9/“.

You can also search at

http://search.maven.org/

prefix it with “g:” for group as in “g:junit”, and “a:” for artifact id as in “a:junit”.

Step 3: Inspect the local repository location that we set up via the “settings.xml”.

Maven Local Repository

Step 4: Create a Java “HelloWorld.java” as shown below inside “C:\projects\simple-maven-project\src\main\java\com\mytutorial” where “src\main\java” is the mvn stipulated project structure and “com\mytutorial” is the package.

Step 5: Execute the following command from the project base folder “C:\projects\simple-maven-project”. This basically looks at the “pom.xml” file in that folder.

This would have created a jar file named “simple-maven-project-1.0-SNAPSHOT.jar” inside the “C:\projects\simple-maven-project\target”. This jar file contains the packages & class files. The maven default plugins have compiled the “.java” files and packaged it up as a jar file. This is the basic power of a build tool. It can do a lot more.

Step 6: Run “com.mytutorial.HelloWorld”

Much simpler to create Java projects as jar files.

Tips:

#1. Setting up the Maven environment

Instead of running the “c:\scripts>environment-setup.cmd” in Windows, you could set up the Windows environment variables as demonstrated in setting up Java tutorial.

set M3_HOME

set MAVEN_OPTS

set the path variable

#2. Installing the jar file to local maven repository

Instead of “mvn clean package”, you can install it to the repository in your “c:\tools\home\.m2\repository” folder.

Publish your project artifact(i.e jar file) to the local repository

Next Java Beginner Tutorial: Setting up Eclipse IDE step by step tutorial.

Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.

Posted in Setting up & getting started tutorials
4 comments on “03. ♥ Setting up Maven step by step tutorial
  1. anjaneyareddym says:

    Hi Arul,

    After removing the profile in setting.xml.archetype command is executed. Thanks.

    Regards,
    Anjaneya

  2. Arulkumaran says:

    1. Check your settings.xml file to ensure that your proxies and repositories are set up correctly. If you are behind a fire wall, you need to set up the proxy. The following is a dummy example. You need to fill in the correct proxy details.

    2. See if you can reach http://search.maven.org/ which is the maven repository. In the search box enter “org/apache/maven/plugins”. You should see “maven-archetype-plugin”.

    3. run your maven command with -X switch as it will give you more debugging statements as to where it is trying to download the plugin from.

  3. anjaneyareddym says:

    I did the same setting as above,But am getting below error.
    ownloading: http://search.maven.org/org/codehaus/mojo/maven-metadata.xml
    INFO] ————————————————————————
    INFO] BUILD FAILURE
    INFO] ————————————————————————
    INFO] Total time: 1.012s
    INFO] Finished at: Tue Mar 17 23:14:40 IST 2015
    INFO] Final Memory: 5M/15M
    INFO] ————————————————————————
    ERROR] No plugin found for prefix ‘archetype’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (c:\tools\home\.m2\repository), cen
    ERROR]
    ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    ERROR] Re-run Maven using the -X switch to enable full debug logging.
    ERROR]
    ERROR] For more information about the errors and possible solutions, please read the following articles:
    ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

    Please help on this

800+ Interview Q&As with lots of diagrams & code ♥Free | ♦FAQs | Hover/Slide for full text

open all | close all

200+ Java Interview FAQs – Quick Prep

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your

open all | close all