Maven Tutorial | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

1. Introduction

  • Maven is a “Yiddish” (German Language), its meaning is “Accumelator of Knowledge”.
  • Maven is one of the application building tool.
  • Maven is more powerful than Ant.
  • Maven is a Build Tool, It will perform
    1. Directory Structers to the Applications.
    2. Download the required Dependencies (JARs).
    3. Compile the Source Code.
    4. Compile Test Code.
    5. Packaging the Applications.
    6. Run Applications.
    7. Starts the Server.
    8. Deploying the Application.
    9. Perform Unit Testing.
    10. Prepare Reports and Documents.
    11. Undeployment of our Applications.
    12. Stop Server.

2. POM File (Project Object Model)

  • Project Object Model (pom.xml)
    • It is the heart in Maven.
    • It will provide all the Project Build configuration which are required by Maven.
    • It is an XML file, in the initial version of Maven, pom file name is project.xml, later on pom.xml.
    • When we create Maven project then pom.xml file will be created automatically.
    • In Maven projects, pom.xml file is able to provide number of responsibilities.
      1. Project Description
      2. Repository
      3. Dependency Management
      4. Project Inheritance
      5. Build Configurations
      6. Build Profiles

1. Project Description

  • It will provide some details about the project like Project Name, Version Number, Packaging Type, …
  • pom.xml
<project>
	<!-- Project Description -->
	<modelVersion>4.0.0</modelVersion>  // model version of the POM
	<groupId>com.codeFactory</groupId>  // Is an unique id for our company/organization
	<artifactId>abc.xyz</artifactId>  // Application name
	<description>Application Description</description>  // Description about application
	<packaging>jar/war/ear</packaging>
	...
</project>

2. Repository

  • Repository is a place (location in system, in internet, a system in our company) which will provide all the dependent jar files to our application.
  • Repositories are able to manage all the Jar files which are required by our applications.
  • There are three types of Repositories are existed in Maven.

2.1 Local Repository

  • It is a location in our system, which will be created by any mvn command initially.
  • In our System Local Repository is existed at “C:\Users\.m2\repository”.
  • When we required Jar files in our applications then Maven will search for the dependencies first in Local Repositories.

2.2 Central Repository

<project>
	...
	<repositories>
		<repository>
			<id>jboss</id>
			<name>jboss repo</name>
			<url>https://repository.jboss.org/nexus/content/groups/public</url>
		</repository>
	</repositories>
	...
</project>

2.3 Remote Repository

  • If the dependencies are not available/existed in Local repositories, Central Repositories then Maven will search for them in Remote Repository.
  • In Application for required dependencies, If dependencies already available in Local Repository then Maven will send dependencies to the Application.
  • If dependencies are not existed in Local Repositories then Maven will search at Central Repository. If the dependencies are existed at Central Repository then Maven will download to Local Repository. If the dependencies are not existed in Central Repository then Maven will search at Remote Repository. If the dependencies are existed at Remote Repository then Maven will download to Local Repository.
  • Note: If Remote repositories is not existed or dependencies are not existed in Remote Repository them Maven will raise some Exception.

3. Dependency Management

  • In Maven Project, dependency means that a dependent Jar file.
  • Maven is not giving any option to the developers to downlaod the required Jar files, Maven will download the required Jar files and Maven will keep that Jar files in our application, but we must specify which dependencies we need in our applications.
<project>
	...
	<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.4.6-Final</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	...
</project>
  • Maven will download Transitive Dependencies automatically. Ex: A dependency depend on B and B depend on C and if we download A dependency then Maven will automatically downlaods B and C.
  • Scopes in Maven: Jar files requires to application at what time.
    1. compile – Specified Jar files available upto compilation, run and testing.
    2. provided – Available for project upto compilation and testing but not for runtime.
    3. runtime – Available for projecr upto runtime and testing
    4. test – Available for project upto testing
    5. system – Same as provided but difference is system dependencies will get Jars only from Central Repository
    6. import – It will available from Maven 2.0.9 and later.

4. Project Inheritance

  • Extending/Getting configuration from one pom from another pom.
  • If you want to make one Pom as a parent then tag value must be “pom”.
  • If you want to make a Pom as as child for a particular parent, just we have to use tag in our respective Pom file.
Parent POM
<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.codeFactory</groupId>
	<artifactId>my-parent</artifactId>
	<version>1.0</version>
	<packaging>pom</packaging>
	...
</project>


Child POM
<project>
	<parent>
		<groupId>com.codeFactory</groupId>
		<artifactId>my-parent</artifactId>
		<version>1.0.0</version>
	</parent>
	...
</project>
  • Effective POM = Super POM + Present Application POM
  • mvn help:effective-pom

5. Build Configurations

  • Plugin Configurations
    • There are 2 types of plugins.
      1. Build Plugins
        1. clean
        2. compiler
        3. deploy
        4. install
        5. resources
        6. ear
        7. jar
        8. war
      2. Reporting Plugins
        1. javadoc
        2. project-info-reports
        3. surfire-reports
<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configurations>
					<source>1.8</source>
					<target>1.8</target>
				</configurations>
			</plugin>
		</plugins>
	</build>
</project>
  • Resource Configurations
<project>
	<build>
		<resources>
			<directory>src/main/resources</directory>
		</resources>
	</build>
</project>

6. Build Profiles

  • Development Mode
  • Testing Mode
  • Production Mode
db.properties
jdbc.connection.url = ${jdbc.connection.url}


pom.xml
<project>
	...
	<profiles>
		<profile>
			<id>development</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<jdbc.connection.url>jdbc:oracle:thin:@localhost:1521:xe</jdbc.connection.url>
			</properties>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<jdbc.connection.url>jdbc:mysql://localhost:3306/db_name</jdbc.connection.url>
			</properties>
		</profile>
	</profiles>
</project>

3. Arche Types in MAVEN

  • Preparing Project directoy structure based on the type only, we are going to use Arche Type.

4. Steps to Use MAVEN For Applications

  1. Install Maven Software
    • https://maven.apche.org/download.cgi
    • Set Environment Variable
      • JAVA_HOME – C:\< java folder >\jdk1.X.X
      • M2_HOME – C:\< maven folder >
      • MAVEN_HOME – C:\< maven folder >
      • In path variable, add C:\< java folder >\jdk1.X.X\bin, C:\< maven folder >\bin
    • Check maven installed or not
      • open CMD
      • mvn –version
  2. Create Maven Project
    • Open CMD
    • Go to Project Wprkspace
    • > mvn archetype:generate
    • Choose a number or apply filter > 1219
    • Choose a number > 7
    • Define value for property ‘groupId’: com.codeFactory
    • Define value for property ‘artifactId’: demoapp
    • Define value for property ‘version’: 1.0-SNAPSHOT
    • Define value for property ‘package’: com.codeFactory
  3. Write Java Code
  4. Compile Java Code
  5. Execute Java Application

Leave a comment