Nearby lessons

33 of 35

Spring - Maven Project Setup

📌 What You Will Learn
  • Understand Spring - Maven Project Setup
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - Maven Project Setup step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

1) Project Description

In pom file, initial we will identify "Projection Description", it contains Project name, version

number, packaged type,.....

To specify the above details, we need to use the following XML tags.

1) Project Description

Where "<project>" tag is root tag in pom.xml file

Where "<modelVersion>" tag declared which version of the MAVEN we are

using.<modelVersion> tag will take 4.0.0 to support for MAVEN2.x/3.x versions.

Where "<groupid>" tag will take an unique ID for an organization, or a project.Normally we

use a group ID similar to the root Java package name of the project.

Where "<artifactid>" tag will take name of the project.The artifact ID is used as name for a

sub directory under the group ID directory in the Maven repository and as part of the

EX:name of the JAR file produced when building the project.The build result, a JAR, WAR

or EAR file, is called an artifact in Maven.

Where "<versin>" tag will take Project version number.

Where "<packaging>" tag will take different packaging formats inorder to delivery the

project like jar, war, ear,...

Example02
Loading editor…

1) Project Description

Example03
Loading editor…

2) Repository

If we use Dependencies in MAVEN Project then MAVEN will search for the dependent JARs in

Repositories.

MAVEN will use three types of Repositories in order to get dependencies.

  • Local Repository:

It is a location to manage and supply all dependencies, it will be created by MAVEN when we

execute any MAVEN command first time.

In general, MAVEN will create Local Repository at "C:/Users/User_Name/.m2/repository"

EX: C:\Users\LENOVO\.m2\repository

  • Central Repository:

It is a default Repository for MAVEN, it is located at "http://repo1.maven.org/maven2".

IN MAVEN applications, we will use some other repositories are also explicitly like.

2) Repository

In MAVEN applications, if we want to use the above explict repositories then we have to configure

them in pom file by using the following xml tags.

Example05
Loading editor…

2) Repository

In some Situations, Maven does not find the dependencies in Local Repository and in central

repository, in this context, MAVEN stops the build process and generates some Exceptions. To

overcome this problems, Maven has provided a new Features like "Remote Repository".

Remote Repository is a developer's own custom repository containing required libraries or other

project jars.

To configure Remote Repository, we have to use the following XML tags in pom.xml file.

Example06
Loading editor…

2) Repository

When we run MAVEN project then MAVEN will search for the dependencies in the following order.

Example07
Loading editor…

2) Repository

dependencies are available at Local Repository the MAVEN will use them in application. If

the dependencies are not available at Local Repository then MAVEN search for them at

Central Repository.

Example08
Loading editor…

2) Repository

into Local Repository and MAVEN will use them in the applications. If the required

dependencies are not existed in Central Repository then MAVEN will search for them in

Remote Repositories as per configuration.

Example09
Loading editor…

2) Repository

generated some Exceptions.

Example10
Loading editor…

2) Repository

in Remote Repository, if they are identified then MAVEN will load them into Local

Repository for futur reference. If the dependencies are not existed at Remote Repositories

then MAVEN will stop the execution and generate some Exceptions.

Example11
Loading editor…

3) Dependency Management

In Applications, Dependencies are the libraries[Collection of JARs] which are required to compile,

test and run our applications.

In General, in application development, we will download the required libraries from internet and

we will store them in application directory structer.

The main Advantage of MAVEN in applications development is that not to store any Dependent

JAR files in Project Directory Structer by downloading them explicitly, MAVEN has given flexibility

to the developers like to specify dependent JAR files names in pom file, where MAVEN will search

for them in the repositories and MAVEN will load them into the project directory structer

automatically.

If we need any Library in MAVEN based applications then we have to declare them in pom file like

below.

3) Dependency Management

If we provide the dependency like above then MAVEN will search for the hibernate library with the

name like

http://repo1.maven.org/maven2/org/hibernate/hibernate-core/3.5.6-Final/

MAVEN is following "Transitive Dependencies Mechanism", that is, if our dependencies are

required any other libraries then MAVEN will get them automatically without loading them explicitly

by the developers.

Example13
Loading editor…

Dependency Scopes

In Applications, some dependencies are required to all phases of the project lifecycle like compile,

test, run,... and some other required only some of phases of the project lifecycle.

In order to limit the dependencies for the lifecycle phases we will use Dependency Scopes.

There are 6 scopes available in MAVEN

  • Compile
  • Provided
  • Runtime
  • Test
  • System
  • Import
  • Compile:

It is the default scope in MAVEN . This scope will make the dependencies to avail all phases like

compile, test, run,....

EX:

<dependency>

Dependency Scopes

Note: In general, hibernate-core library is required for all phases of the application.

  • Provided:

This scope will make the dependency libraries to avail upto compilation and and upto testinbg, not

for runtime, because, at runtime, JDKs or Containers will provide the required dependencies at

runtime.

EX:

In web applications, Servlet API is required expliclty to compile and test the project, but, Servlet

API is provided by the container at runtime automatically, so that, they are not required to be

exported at runtime.

Example15
Loading editor…

Dependency Scopes

This scope indicates that the dependency is not required for compilation, but is for execution. It is

in the runtime and test class paths, but not the compile class path.

EX:

Example16
Loading editor…

Dependency Scopes

This scope indicates that the dependency is not required for normal use of the application, and is

only available for the test compilation and execution phases. This scope is not transitive.

EX:

Example17
Loading editor…

Dependency Scopes

Dependencies with system are similar to ones with scope provided. The only difference is system

dependencies are not retrieved from remote repository. They are present under project`s

subdirectory and are referred from there.

EX:

Example18
Loading editor…

Dependency Scopes

It is available in Maven 2.0.9 or later.

Import scope is only supported on a dependency of type pom in the dependencyManagement

section. It indicates the dependency to be replaced with the effective list of dependencies in the

specified POM`s dependencyManagement section.

EX:

Example19
Loading editor…

Dependency Scopes

Example20
Loading editor…

4) Project Inheritance

In MAVEN based applications, it is possible to inherit configurations from one pom file to another

pom file inorder to avoid configurations redundency.

To declare parent pom , we have to use "pom" as value to <packaging> tag in parent pom file.

EX:

4) Project Inheritance

If we want to inherit parent pom configuration details into a particular chaild pom then we have to

configure parent pom in chaild pom.

EX:

Example22
Loading editor…

4) Project Inheritance

Note: In JAVA, java.lang.Object class is common and default super class for every java class

inorder to provide 11 common and default methods to each and every java class, similarily , there

is a common and default super pom file is existed in maven inorder to provide all common

configurations and settings to the chaild pom file.

In general, parent pom contains the following configurations

  • Common data � Developers` names, SCM address, distribution management etc.
  • Constants � Such as version numbers
  • Common dependencies � Common to all child. It has same effect as writing them several

times in individual pom files.

  • Properties � For example plugins, declarations, executions and IDs.
  • Configurations
  • Resources

Note: By default, Maven looks for the parent POM first at project`s root, then the local repository,

and lastly in the remote repository. If parent POM file is not located in any other place, then you

can use code tag. This relative path shall be relative to project root.

EX:

Example23
Loading editor…

4) Project Inheritance

Note: If we want to get super pom from MAVEN then use the following command on command

prompt from the project root location which contains project specific pom file.

mvn help:effective-pom

Note: Where effective-pm is super pom configurations and project configurations.

Example24
Loading editor…

5) Build Configuration

In MAVEN , Build Configuration is mainly for plugin configurations, resources

configurations,.....which are required in MAVEN Project.

MAVEN is simply the collection of plugins, where plugins are used to perform the actions like

creating jar files, creating war files, compile Source code, executing unit test code, create project

documentation, ......

MAVEN is having "Plugin Execution Framework" at its heart inorder to execute all plugins.

In MAVEN , there are two types of Plugins.

  • Build Plugins
  • Reporting Plugins

1.Build Plugins: These plugins are executed during the build and they should be configured in

the <build/> element from the POM.

EX

1.Clean : It is used when you want to remove files generated at build-time in acreating

project's directory.

2.Compiler: Compiles Java source code.

3.Deploy: It can be used to store artifacts in remote repository while deploying the

applications in order to share to other projects .

4.Install: It can be used to install artifacts into local repository.

5.Resources: It will include all the project resources in output directory while

JAR files.

6.Ear: create ear file from the current project.

7.jar: creates jar file from the current project.

8.war: creates war file from the current project.

9.rar: creates rar file from the current project.

  • Reporting plugins: These plugins are executed during the site generation and they should be

configured in the <reporting/> element from the POM.

EX:

1.changelog: Generate a list of recent changes from your SCM[Software Configuration

Management].

2.changes: Generate a report from an issue tracker or a change document.

3.javadoc: Generate Javadoc for the project.

4.project-info-resports: Generate standard project reports.

5.surfire-report: Generate a report based on the results of unit tests.

IN general, we will use MAVEN compiler plugin inorder to perform Compilation, for this we have to

use the following xml tags in pom.xml file.

5) Build Configuration

By default, all files placed in "src\main\config" are packaged into the generated project artifact and

any file which we placed in "src\test\resources" are available in project classpath during unit tests.

If we want to provide our own customized resources location in project then we have to configure

them in pom.xml file under <build> tag like below.

Example26
Loading editor…

5) Build Configuration

Example27
Loading editor…

6) Build Profiles

IN general, profiles are used to customize the build lifecycle for different environments like

development, testing, production,.....

Example:

6) Build Profiles

Where each and every profile has its own id, it can be used to access the respective environment

or profile.

In src/main/resources/db.properties

jdbc.connection.url = ${jdbc.connection.url}

If we provide the above setups like above then at compilation time, the respective jdbc URL will be

injected to the "jdbc.connection.url" property depending on the target environment.

Use the following command on command prompt inorder to compile the project.

C:/apps>mvn compile

Here "jdbc.connection.profile" property will take "jdbc:oracle:thin:@localhost:1521:xe" value.

C:/apps>mvn compile -Ptest

Here "jdbc.connection.profile" property will take "jdbc:mysql://localhost:3306/durgadb" value.

jdbc.connection.url = ${jdbc.connection.url}

If we provide the above setups like above then at compilation time, the respective jdbc URL will be

injected to the "jdbc.connection.url" property depending on the target environment.

Use the following command on command prompt inorder to compile the project.

C:/apps>mvn compile

Here "jdbc.connection.profile" property will take "jdbc:oracle:thin:@localhost:1521:xe" value.

C:/apps>mvn compile -Ptest

Here "jdbc.connection.profile" property will take "jdbc:mysql://localhost:3306/durgadb" value.

Archetypes in MAVEN

MAVEN is providing Standard directory structers to prepare projects depending on the

ARCHETYPE selection.

To create sample MAVEN standalone application directory structer we will use the following

command on command prompt .

C:\mvnapps>mvn archetype:create -DgroupId=com.durgasoft -DartifactId=sampleapp

-Dpackagename=com.durgasoft.bankapp

Steps to Create First Project in MAVEN In Standalone:

Install MAVEN Software.

Create MAVEN Project

Write Java Code

Compile Java Code

Execute Java Application

  • Install MAVEN Software:
Example29
Loading editor…

Installation Process

  • download apache-maven-3.5.4.zip file from internet[https://maven.apache.org/download.cgi]
  • Unzip apache-maven-3.5.4.zip file under C drive and we will get apache-maven-3.5.4 folder.
  • Set the following Environment Variables in System.

a) JAVA_HOME: C:/Java/jdk1.8.0;

b) path: C:/Java/jdk1.8.0/bin;C:\apache-maven-3.5.4\bin;

c) M2_HOME: C:/apache-maven-3.5.4

d) MAVEN_HOME: C:/apache-maven-3.5.4 [Optional]

  • Test MAVEN Installation:

Open Command prompt and use the following command.

C:\apache-maven-3.5.4\bin>mvn --version

Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-

18T00:03:14+05:30)

Maven home: C:\apache-maven-3.5.4\bin\..

Java version: 1.8.0, vendor: Oracle Corporation, runtime: C:\Java\jdk1.8.0\jre

Default locale: en_US, platform encoding: Cp1252

OS name: "windows 8.1", version: "6.3", arch: "x86", family: "windows"

  • Create MAVEN Project:

a) Create a seperate folder for MAVEN applications

E:/mvn_projects

b) Use the following command on Command prompt.

E:/mvn_projects>mvn archetype:generate

c) Provide archetype Number: 1202[ for maven-archetype-quickstart application]

d) Provide archetype version number : 7

e) Provide "groupId" value: com.durgasoft

f) Provide "archetype" value : sampleapp

g) Provide "version" number : 1.0 or 1.0-SNAPSHOT

h) Provide "package" name : com.durgasoft

i) Provide "Y" to confirm all the above details

If we do the above steps then MAVEN will create quick start project with the following directories

and files.

E:/mvn_projects

Sampleapp

|--src

| |---main

| | |----java

|| |---com

|| |---durgasoft

|| |----App.java

| |---test

| |----java

| |---com

| |----durgasoft

| |-----AppTest.java

|---pom.xml

  • Write Java Code:

App.java

Installation Process

Example31
Loading editor…

Installation Process

AppTest.java

Example32
Loading editor…

Installation Process

Example33
Loading editor…

Installation Process

Example34
Loading editor…

Installation Process

Example35
Loading editor…

Installation Process

To Compile JAVA code, first, goto application folder then use the following command on

command prompt.

E:\mvn_projects\sampleapp>mvn compile

If we use the above command then MAVEN will compile JAVA files and generates the .class files

by creating "target" folder under "application folder".

  • Execute MAVEN Project:

IN Two ways we are able to execute MAVEN Project.

  • Execute Test case directly
  • Execute Main App by creating project.
Example36
Loading editor…

1) Execute Test case directly

E:\mvn_projects\sampleapp>mvn test

1) Execute Test case directly

Example38
Loading editor…

a) Create jar file for the project

E:\mvn_projects\sampleapp>mvn package

b) Set classpath environment variable to the generated jar file

E:\mvn_projects\sampleapp>set classpath=E:\mvn_projects\sampleapp\target\sampleapp-

1.0.jar;

Execute Main Class

E:\mvn_projects\sampleapp>java com.durgasoft.App

c) Welcome to Durgasoft

Steps to prepare MAVEN Project in Eclipse

  • Create Maven Project in Eclipse:
  • Provide the required configurations in pom.xml
  • Write Application Logic in AppTest.java file
  • Run Maven Project:
  • Create Maven Project in Eclipse:

Execute Main Class

a) groupId: com.durgasoft

b) artifactId: sampleapp

c) version: 0.0.1-SNAPSHOT

d) package: com.durgasoft

Example42
Loading editor…

Execute Main Class

Example43
Loading editor…

Execute Main Class

XMLSchema-instance"

  • xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m

aven-4.0.0.xsd">

Example44
Loading editor…

Execute Main Class

Example45
Loading editor…

Execute Main Class

Example46
Loading editor…

Execute Main Class

Example47
Loading editor…

Execute Main Class

Example48
Loading editor…

Execute Main Class

3.Write Application Logic in AppTest.java file

src/test/java/AppTest.java

Example49
Loading editor…

Execute Main Class

Save the above program

  • Run Maven Project:

Right Click on Project[Sampleapp].

Select "Run As".

Select "Maven Test" and see the Result in Console.

To prepare JDBC Application with MAVEN then we have to use the following steps with all the

above steps.

  • Install ojdbc6.jar file in Local Repository.
  • Provide ojdbc6.jar file dependency in pom.xml
  • Install ojdbc6.jar file in Local Repository.

a. Keep ojdbc6.jar file in our working directory location.

E:/Mvn_Projects/ ojdbc6.jar

b. use "mvn install:install-file" command on command prompt to install jar file.

E:/Mvn_Projects> mvn install:install-file -DgroupId=oracle -DartifactId=oracle-jdbc

-Dpackaging=jar -Dversion=11.2.0-XE -DgeneratePom=true -Dfile=ojdbc6.jar

Example50
Loading editor…

Execute Main Class

EX:

src

main

java

com

durgasoft

|| |----App.java

|| |----JdbcApp.java

test

java

com

durgasoft

| |---AppTest.java

|---pom.xml

pom.xml

Example51
Loading editor…

Execute Main Class

XMLSchema-instance"

  • xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m

aven-4.0.0.xsd">

Example52
Loading editor…

Execute Main Class

Example53
Loading editor…

Execute Main Class

Example54
Loading editor…

Execute Main Class

Example55
Loading editor…

Execute Main Class

Example56
Loading editor…

Execute Main Class

JdbcApp.java

Example57
Loading editor…

Execute Main Class

Example58
Loading editor…

Execute Main Class

Example59
Loading editor…

Execute Main Class

", "durga");

Example60
Loading editor…

Execute Main Class

AppTest.java

Example61
Loading editor…

Execute Main Class

Example62
Loading editor…

Execute Main Class

Example63
Loading editor…

Execute Main Class

Example64
Loading editor…

Execute Main Class

Example65
Loading editor…

Execute Main Class

Example66
Loading editor…

Execute Main Class

If we want to use MySQL Database in Our project then we have to use the following dependency

in pom file.

Example67
Loading editor…

Execute Main Class

In Java Application

Driver Class: com.mysql.cj.jdbc.Driver

Driver UIRL: jdbc:mysql://localhost:3306/durgadb

DB User Name: root

DB Password : root

Example:

pom.xml

Example68
Loading editor…

Execute Main Class

XMLSchema-instance"

  • xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m

aven-4.0.0.xsd">

Example69
Loading editor…

Execute Main Class

Example70
Loading editor…

Execute Main Class

Example71
Loading editor…

Execute Main Class

Example72
Loading editor…

Execute Main Class

Example73
Loading editor…

Execute Main Class

Example74
Loading editor…

Execute Main Class

Example75
Loading editor…

Execute Main Class

JdbcApp.java

Example76
Loading editor…

Execute Main Class

Example77
Loading editor…

Execute Main Class

Example78
Loading editor…

Execute Main Class

root");

Example79
Loading editor…

Execute Main Class

AppTest.java

Example80
Loading editor…

Execute Main Class

Example81
Loading editor…

Execute Main Class

Example82
Loading editor…

Execute Main Class

Example83
Loading editor…

Execute Main Class

Example84
Loading editor…

Execute Main Class

Example85
Loading editor…

Execute Main Class

", "durga");

Example86
Loading editor…

Execute Main Class

Web Application in Maven

Steps to Prepare Web Application in Maven:

Example87
Loading editor…

Execute Main Class

i. Click on File in "Eclipse".

ii. Click on "New".

iii. Click on "Others".

iv. Click on "Maven".

v. Select "Maven Project".

vi. Click on "Next".

vii. Click on "Next".

viii. Select "org.apache.maven.archetypes maven-archetype-webapp 1.0".

ix. Click on "Next" button.

x. Provide the following details

a. Group Id: com.durgasoft

b. Artifact Id: loginapp

c. Version: 0.0.1-SNAPSHOT

d. packag: com.durgasoft

xi. Click on "Finish" button.

Example88
Loading editor…

Execute Main Class

3 .Provide Servlet API Dependency:

Example89
Loading editor…

Execute Main Class

a. Right Click on Project.

b. Select "Maven".

c. Select "Update Project".

or

a. Select "Markers" in Tools bar.

b. Select "Maven Problems".

c. Right Click on "Project configuration is not up-to-date with pom.xml".

d. Select "Quick Fix" .

e. Click on "Finish" button.

EX: pom.xml

Example90
Loading editor…

Execute Main Class

XMLSchema-instance"

  • xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven

-v4_0_0.xsd">

Example91
Loading editor…

Execute Main Class

Example92
Loading editor…
📝 Key Takeaways
  • Key ideas of Spring - Maven Project Setup explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.