Maven

This is mainly for the beginners of Java to get started with Maven.

This is an industrial strength tool to build Java applications.

Search for Maven repositories : https://mvnrepository.com

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, say example, the “tools” folder.

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

SET M3_HOME=D:\JavaTraining\tools\apache-maven-3.5.0

SET PATH=%PATH%;%JAVA_HOME%\bin;%M3_HOME%\bin

Verify installation

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

C:\Users\rdayala.ORADEV>mvn --version

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T01:09:06+05:30)

Maven home: D:\JavaTraining\tools\apache-maven-3.5.0\bin\..
 Java version: 1.8.0_121, vendor: Oracle Corporation
 Java home: C:\Program Files\Java\jdk1.8.0_121\jre
 Default locale: en_US, platform encoding: Cp1252
 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

where is maven pickup up from :

C:\Users\rdayala.ORADEV>where mvn

D:\JavaTraining\tools\apache-maven-3.5.0\bin\mvn
 D:\JavaTraining\tools\apache-maven-3.5.0\bin\mvn.cmd

Configure Maven :

You can set up the repository location by opening the settings.xml file under conf folder inside your maven installation directory “D:\JavaTraining\tools\apache-maven-3.5.0\conf”.

Repository is where the dependency Java libraries will be stored.

This is the default location :

Default: ${user.home}/.m2/repository

You can change it to like :

<localRepository>c:\tools\home\.m2\repository</localRepository>

Are you behind a firewall?

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

<proxy>
    <id>optional</id>
    <active>true</active>
    <protocol>http</protocol>
    <username>proxyuser</username>
    <password>proxypass</password>
    <host>webproxy</host>
    <port>8080</port>
    <nonProxyHosts>local.net,some.host.com</nonProxyHosts>
</proxy>

Create a Maven project(command line) :

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”.

c:\projects>mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simple-maven-project -DinteractiveMode=false

groupId --> is like package name

artifactId --> project name

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

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/maven-v4_0_0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mytutorial</groupId>
    <artifactId>simple-maven-project</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>simple-maven-project</name>
    <url>http://maven.apache.org</url>

    <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.9</version>
           <scope>test</scope>
       </dependency>
    </dependencies>
</project>

The pom.xml file is where you define the dependent libraries like “hibernate”, “spring”, “junit”, etc.  Change the version of the dependency 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 will pull all the dependencies and stores them in 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.

src/main/java – where all Java sources will be written.
src/main/resources – where all the application properties will be residing.
src/test/java – unit tests will be written here

package com.mytutorial;

public class HelloWorld {
    public static void main(String[] args) {
       System.out.println("My first java code from a Maven Java project");
   }
}

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.

c:\projects\simple-maven-project>mvn clean package

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”

‘Target’ is the temporary folder used by Maven.

c:\projects>java -cp simple-maven-project\target\simple-maven-project-1.0-SNAPSHOT.jar com.mytutorial.HelloWorld

My first java code from a Maven Java project

Configure Eclipse to work with Maven :

Suppose that the user has changed the Maven settings in the file, “D:\JavaTraining\tools\apache-maven-3.5.0\conf\settings.xml” to point to a different local repository.

Step 1: Let’s configure Eclipse to point to this file via Window –> Preferences, and in the pop up console select “Maven –> User Settings“.

Import an existing Maven project into Eclipse :

Step 1: File –> Import, and in the pop up console select “Maven –> Existing Maven Projects“.

Create a new Maven Project In Eclipse :

Step 1: File –> New Project…, and then in the pop up console select “Maven –> Maven Project“.

Select the Maven archetype, and then assign a Maven “group id” & “artifact id”.

Maven Build Life Cycle : 

  • Validate
  • Compile – compiling source code and compiling test code : mvn clean compile
  • Test – runs all unit test cases : mvn clean test-compile ; mvn test – run unit tests
  • Package – builds JAR file : mvn clean package
  • Integration Test
  • Verify
  • Install – putting the JAR file to local repository : mvn clean install
  • Deploy – putting the JAR file to remote repository

You can build the “jar” file within Eclipse by selecting the project, say example, “simple-maven-project“, and right mouse click to select “Run As–>Maven Install“.

Installing the jar file to local maven repository :

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

c:\projects\simple-maven-project>mvn clean install

** Q. Maven uses a settings.xml file for its configuration.  How do you find out which settings.xml file is being used by Maven?

A. With the debug option mvn -X command.

[DEBUG] Populating class realm maven.api
 [INFO] Error stacktraces are turned on.
 [DEBUG] Message scheme: color
 [DEBUG] Message styles: debug info warning error success failure strong mojo project

[DEBUG] Reading global settings from D:\JavaTraining\tools\apache-maven-3.5.0\bin\..\conf\settings.xml
 [DEBUG] Reading user settings from C:\Users\rdayala.ORADEV\.m2\settings.xml
 [DEBUG] Reading global toolchains from D:\JavaTraining\tools\apache-maven-3.5.0\bin\..\conf\toolchains.xml
 [DEBUG] Reading user toolchains from C:\Users\rdayala.ORADEV\.m2\toolchains.xml
 [DEBUG] Using local repository at C:\Users\rdayala.ORADEV\.m2\repository
 [DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for C:\Users\rdayala.ORADEV\.m2\repository

[INFO] Scanning for projects...

Q. How do you know that your Java and Maven are set up correctly?
A. Typing “java” or “mvn” on a command prompt should recognize the command.

Q. How does Maven know where to download the library jar files from? In other words, where do you specify the “Maven central repository URL”?
A. Via the global “settings.xml file or project specific “pom.xml” file.

<repositories>
    <repository>
       <id>cloudera</id>
       <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
</repositories>

Q. If I have the jar file, can I manually install JAR into my local Maven repository ?
A. Yes, use the following command :

mvn install:install-file \
     -DgroupId=org.springframework \
     -DartifactId=spring-core \
     -Dpackaging=jar \
     -Dversion=3.1.0.RELEASE \
     -Dfile=jta-1.0.1B.jar \
     -DgeneratePom=true

Q. How do you search for artifacts on the central repository?
A. Go to central repository website http://search.maven.org/ and search. Prefix with g: for groupid, a: for artifactid, and v: for version number.

g:org.springframework a:spring-core v:3.1.0.RELEASE

Advanced Maven Commands :

mvn help:effective-settings --> all Maven settings will be shown
mvn help:effective-pom --> complete POM that is build by Maven
mvn dependency:tree --> show you all the dependencies coming into your project, their hierarchy

For any of the commands, if we put –debug, maven executes the commands in DEBUG mode.