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
JCode Cell
1 
2<project ..... >
3<!-- Project Description -->
4<modelVersion> --- </modelVersion>
5<groupid> --- </groupid>
6<artifactid> -- </artifactid>
7<version> -- </version>
8-----
9-----
10</project>
11

1) Project Description

Example03
JCode Cell
1 
2<project ..... >
3<!-- Project Description -->
4<modelVersion>4.0.0</modelVersion>
5<groupid>com.durgasoft.banking</groupid>
6<artifactid>icici.accounts</artifactid>
7<version>1.0</version>
8<name>Account Application</name>
9<description> Application for Accounts module in icici Bank project </description>
10-----
11</project>
12

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
JCode Cell
1 
2http://repository.jboss.org/nexus/content/groups/public
3http://mvnrepository.com
4

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
JCode Cell
1 
2<repositories>
3<repository>
4<id>jboss</id>
5<name>jboss repo</name>
6<url>http://repository.jboss.org/nexus/content/groups/public/</url>
7</repository>
8</repositories>
9Remote Repository:
10

2) Repository

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

Example07
JCode Cell
1 
2<repositories>
3<repository>
4<id>durgasoft.lib</id>
5<url>http://library.durgasoft.com/maven2/lib</url>
6</repository>
7</repositories>
8

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
JCode Cell
1 
2First, MAVEN will search for the dependencies in local repository, if the required
3

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
JCode Cell
1 
2If the required Dependencies are existed in central repository then MAVEN will load them
3

2) Repository

generated some Exceptions.

Example10
JCode Cell
1 
2If Remote Repository is not configured then MAVEN will stop the application execution and
3

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
JCode Cell
1 
2If Remote Repository is configured then MAVEN will search for the required dependencies
3

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
JCode Cell
1 
2<dependencies>
3<dependency>
4<groupId>org.hibernate</groupId>
5<artifactId>hibernate-core</artifactId>
6<version>3.5.6-Final</version>
7<scope>provided</scope>
8</dependency>
9</dependencies>
10

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
JCode Cell
1 
2<groupId>org.hibernate</groupId>
3<artifactId>hibernate-core</artifactId>
4<version>3.5.6-Final</version>
5<scope>compile</scope>
6</dependency>
7

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
JCode Cell
1 
2<dependency>
3<groupId>javax.servlet</groupId>
4<artifactId>servlet-api</artifactId>
5<version>3.0.1</version>
6<scope>provided</scope>
7</dependency>
8Runtime:
9

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
JCode Cell
1 
2<dependency>
3<groupId>com.thoughtworks.xstream</groupId>
4<artifactId>xstream</artifactId>
5<version>1.4.4</version>
6<scope>runtime</scope>
7</dependency>
8Test:
9

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
JCode Cell
1 
2<dependency>
3<groupId>junit</groupId>
4<artifactId>junit</artifactId>
5<version>4.12</version>
6<scope>test</scope>
7</dependency>
8System:
9

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
JCode Cell
1 
2<dependency>
3<groupId>Explicit_Dependency</groupId>
4<artifactId>Explicit_Dependency</artifactId>
5<scope>system</scope>
6<version>1.0</version>
7<systemPath>apps\app.war\WEB-INF\lib\Explicit_Dependency.jar</systemPath>
8</dependency>
9Import:
10

Dependency Scopes

Example20
JCode Cell
1 
2<dependencyManagement>
3<dependencies>
4<dependency>
5<groupId>other.pom.group.id</groupId>
6<artifactId>other-pom-artifact-id</artifactId>
7<version>SNAPSHOT</version>
8<scope>import</scope>
9<type>pom</type>
10</dependency>
11</dependencies>
12</dependencyManagement>
13

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
JCode Cell
1 
2<project ... >
3<modelVersion>4.0.0</modelVersion>
4<groupId>com.durgasdoft</groupId>
5<artifactId>my-parent</artifactId>
6<version>0.0.1-SNAPSHOT</version>
7<packaging>pom</packaging>
8---
9</project>
10

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
JCode Cell
1 
2<project ..... >
3-----
4<parent>
5<groupId>com.durgasoft</groupId>
6<artifactId>my-parent</artifactId>
7<version>0.0.1-SNAPSHOT</version>
8</parent>
9------
10</project>
11

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
JCode Cell
1 
2<parent>
3<groupId>com.durgasoft</groupId>
4<artifactId>MavenExamples</artifactId>
5<version>0.0.1-SNAPSHOT</version>
6<relativePath>../baseapp/pom.xml</relativePath>
7</parent>
8

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
JCode Cell
1 
2<project ---- >
3<build>
4<plugins>
5<plugin>
6<groupId>org.apache.maven.plugins</groupId>
7<artifactId>maven-compiler-plugin</artifactId>
8<configuration>
9<source>1.8</source>
10<target>1.8</target>
11</configuration>
12</plugin>
13</plugins>
14</build>
15</project>
16

5) Build Configuration

Example27
JCode Cell
1 
2<build>
3----
4<resources>
5<resource>
6<directory>src/main/config</directory</directory>
7</resource>
8<resource>
9<directory>src/main/resources</directory</directory>
10</resource>
11----
12</resources>
13----
14</build>
15

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
JCode Cell
1 
2<profiles>
3<profile>
4<id>development</id>
5<activation>
6<activeByDefault>true</activeByDefault>
7</activation>
8<properties>
9<jdbc.connection.url>jdbc:oracle:thin:@localhost:1521:xe</jdbc.connection.url>
10</properties>
11</profile>
12<profile>
13<id>test</id>
14<properties>
15<jdbc.connection.url>jdbc:mysql://localhost:3306/durgadb</jdbc.connection.url>
16</properties>
17</profile>
18</profiles>
19

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
JCode Cell
1 
2package com.durgasoft;
3

Installation Process

AppTest.java

Example32
JCode Cell
1 
2/**
3* Hello world!
4*
5*/
6public class App
7{
8public static void main( String[] args )
9{
10System.out.println( "Welcome to Durgasoft" );
11}
12}
13

Installation Process

Example33
JCode Cell
1 
2package com.durgasoft;
3

Installation Process

Example34
JCode Cell
1 
2import static org.junit.Assert.assertTrue;
3

Installation Process

Example35
JCode Cell
1 
2import org.junit.Test;
3

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
JCode Cell
1 
2/**
3* Unit test for simple App.
4*/
5public class AppTest
6{
7/**
8* Rigorous Test :-)
9*/
10@Test
11public void shouldAnswerWithTrue()
12{
13System.out.println("Welcome To Durgasoft");
14assertTrue( true );
15}
16}
17Compile Java Code:
18
Output

Welcome To Durgasoft
      

1) Execute Test case directly

E:\mvn_projects\sampleapp>mvn test

1) Execute Test case directly

Example38
JCode Cell
1 
2Execute Mian App by creating project.
3

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
JCode Cell
1 
2Open Eclipse IDE
3Right Click on "Project Explorer"
4Select "New"
5Select "Others"
6Search for "Maven".
7Select for "Maven Project".
8Click on "Next" button.
9Click on "Next" button.
10Provide the following details.
11

Execute Main Class

Example43
JCode Cell
1 
2Click on "Finish" Button.
3

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
JCode Cell
1 
2Provide the required configurations in pom.xml
3Open pom.xml file File and provide java8 plugin configuration.
4<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/
5

Execute Main Class

Example45
JCode Cell
1 
2<modelVersion>4.0.0</modelVersion>
3

Execute Main Class

Example46
JCode Cell
1 
2<groupId>com.durgasoft</groupId>
3<artifactId>helloapp</artifactId>
4<version>0.0.1-SNAPSHOT</version>
5<packaging>jar</packaging>
6

Execute Main Class

Example47
JCode Cell
1 
2<name>helloapp</name>
3<url>http://maven.apache.org</url>
4

Execute Main Class

Example48
JCode Cell
1 
2<properties>
3<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4</properties>
5

Execute Main Class

3.Write Application Logic in AppTest.java file

src/test/java/AppTest.java

Example49
JCode Cell
1 
2<dependencies>
3<dependency>
4<groupId>junit</groupId>
5<artifactId>junit</artifactId>
6<version>3.8.1</version>
7<scope>test</scope>
8</dependency>
9</dependencies>
10<build>
11<plugins>
12<plugin>
13<groupId>org.apache.maven.plugins</groupId>
14<artifactId>maven-compiler-plugin</artifactId>
15<version>3.7.0</version>
16<configuration>
17<source>1.8</source>
18<target>1.8</target>
19</configuration>
20</plugin>
21</plugins>
22</build>
23</project>
24Save pom.xml file and see "Markers" for errors.
25Right Click on :Project"
26Select "Maven" .
27Select "Update Project".
28Click on "OK" button.
29

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
JCode Cell
1 
2package com.durgasoft;
3import junit.framework.Test;
4import junit.framework.TestCase;
5import junit.framework.TestSuite;
6public class AppTest extends TestCase
7{
8public AppTest( String testName )
9{
10super( testName );
11}
12public static Test suite()
13{
14return new TestSuite( AppTest.class );
15}
16public void testApp()
17{
18System.out.println("Hello Maven!");
19assertTrue( true );
20}
21}
22
Output

Hello Maven!
      

Execute Main Class

EX:

src

main

java

com

durgasoft

|| |----App.java

|| |----JdbcApp.java

test

java

com

durgasoft

| |---AppTest.java

|---pom.xml

pom.xml

Example51
JCode Cell
1 
2Provide ojdbc6.jar file dependency in pom.xml
3<dependencies>
4<dependency>
5<groupId>oracle</groupId>
6<artifactId>oracle-jdbc</artifactId>
7<version>11.2.0-XE</version>
8<scope>runtime</scope>
9</dependency>
10</dependencies>
11

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
JCode Cell
1 
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/
3

Execute Main Class

Example53
JCode Cell
1 
2<modelVersion>4.0.0</modelVersion>
3

Execute Main Class

Example54
JCode Cell
1 
2<groupId>com.durgasoft</groupId>
3<artifactId>helloapp</artifactId>
4<version>0.0.1-SNAPSHOT</version>
5<packaging>jar</packaging>
6

Execute Main Class

Example55
JCode Cell
1 
2<name>helloapp</name>
3<url>http://maven.apache.org</url>
4

Execute Main Class

Example56
JCode Cell
1 
2<properties>
3<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4</properties>
5

Execute Main Class

JdbcApp.java

Example57
JCode Cell
1 
2<dependencies>
3<dependency>
4<groupId>junit</groupId>
5<artifactId>junit</artifactId>
6<version>3.8.1</version>
7<scope>test</scope>
8</dependency>
9<dependency>
10<groupId>oracle</groupId>
11<artifactId>oracle-jdbc</artifactId>
12<version>11.2.0-XE</version>
13<scope>runtime</scope>
14</dependency>
15</dependencies>
16<build>
17<plugins>
18<plugin>
19<groupId>org.apache.maven.plugins</groupId>
20<artifactId>maven-compiler-plugin</artifactId>
21<version>3.7.0</version>
22<configuration>
23<source>1.8</source>
24<target>1.8</target>
25</configuration>
26</plugin>
27</plugins>
28</build>
29</project>
30

Execute Main Class

Example58
JCode Cell
1 
2package com.durgasoft;
3

Execute Main Class

Example59
JCode Cell
1 
2import java.sql.Connection;
3import java.sql.DriverManager;
4import java.sql.ResultSet;
5import java.sql.Statement;
6

Execute Main Class

", "durga");

Example60
JCode Cell
1 
2public class JdbcApp {
3Connection con;
4Statement st;
5ResultSet rs;
6public JdbcApp() {
7try {
8Class.forName("oracle.jdbc.OracleDriver");
9con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system
10

Execute Main Class

AppTest.java

Example61
JCode Cell
1 
2st = con.createStatement();
3} catch (Exception e) {
4e.printStackTrace();
5}
6}
7public void displayEmpDetails() {
8try {
9rs = st.executeQuery("select * from emp1");
10System.out.println("ENO\tENAME\tESAL\tEADDR");
11System.out.println("------------------------------");
12while(rs.next()) {
13System.out.print(rs.getInt(1)+"\t");
14System.out.print(rs.getString(2)+"\t");
15System.out.print(rs.getFloat(3)+"\t");
16System.out.println(rs.getString(4));
17}
18} catch (Exception e) {
19e.printStackTrace();
20}
21}
22}
23

Execute Main Class

Example62
JCode Cell
1 
2package com.durgasoft;
3

Execute Main Class

Example63
JCode Cell
1 
2import java.sql.Connection;
3import java.sql.DriverManager;
4import java.sql.ResultSet;
5import java.sql.Statement;
6

Execute Main Class

Example64
JCode Cell
1 
2import junit.framework.Test;
3import junit.framework.TestCase;
4import junit.framework.TestSuite;
5

Execute Main Class

Example65
JCode Cell
1 
2/**
3* Unit test for simple App.
4*/
5public class AppTest
6extends TestCase
7{
8/**
9* Create the test case
10*
11* @param testName name of the test case
12*/
13public AppTest( String testName )
14{
15super( testName );
16}
17

Execute Main Class

Example66
JCode Cell
1 
2/**
3* @return the suite of tests being tested
4*/
5public static Test suite()
6{
7return new TestSuite( AppTest.class );
8}
9

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
JCode Cell
1 
2/**
3* Rigourous Test :-)
4*/
5public void testApp()
6{
7JdbcApp jdbcApp = new JdbcApp();
8jdbcApp.displayEmpDetails();
9assertTrue( true );
10}
11}
12

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
JCode Cell
1 
2<dependency>
3<groupId>mysql</groupId>
4<artifactId>mysql-connector-java</artifactId>
5<version>8.0.11</version>
6</dependency>
7

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
JCode Cell
1 
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/
3

Execute Main Class

Example70
JCode Cell
1 
2<modelVersion>4.0.0</modelVersion>
3

Execute Main Class

Example71
JCode Cell
1 
2<groupId>com.durgasoft</groupId>
3<artifactId>helloapp</artifactId>
4<version>0.0.1-SNAPSHOT</version>
5<packaging>jar</packaging>
6

Execute Main Class

Example72
JCode Cell
1 
2<name>helloapp</name>
3<url>http://maven.apache.org</url>
4

Execute Main Class

Example73
JCode Cell
1 
2<properties>
3<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4</properties>
5

Execute Main Class

Example74
JCode Cell
1 
2<dependencies>
3<dependency>
4<groupId>junit</groupId>
5<artifactId>junit</artifactId>
6<version>3.8.1</version>
7<scope>test</scope>
8</dependency>
9<!--
10<dependency>
11<groupId>oracle</groupId>
12<artifactId>oracle-jdbc</artifactId>
13<version>11.2.0-XE</version>
14<scope>runtime</scope>
15</dependency>
16-->
17

Execute Main Class

Example75
JCode Cell
1 
2<dependency>
3<groupId>mysql</groupId>
4<artifactId>mysql-connector-java</artifactId>
5<version>8.0.11</version>
6</dependency>
7

Execute Main Class

JdbcApp.java

Example76
JCode Cell
1 
2</dependencies>
3<build>
4<plugins>
5<plugin>
6<groupId>org.apache.maven.plugins</groupId>
7<artifactId>maven-compiler-plugin</artifactId>
8<version>3.7.0</version>
9<configuration>
10<source>1.8</source>
11<target>1.8</target>
12</configuration>
13</plugin>
14</plugins>
15</build>
16</project>
17

Execute Main Class

Example77
JCode Cell
1 
2package com.durgasoft;
3

Execute Main Class

Example78
JCode Cell
1 
2import java.sql.Connection;
3import java.sql.DriverManager;
4import java.sql.ResultSet;
5import java.sql.Statement;
6

Execute Main Class

root");

Example79
JCode Cell
1 
2public class JdbcApp {
3Connection con;
4Statement st;
5ResultSet rs;
6public JdbcApp() {
7try {
8Class.forName("com.mysql.jdbc.Driver");
9con = DriverManager.getConnection("jdbc:mysql://localhost:3300/durgadb", "root", "
10

Execute Main Class

AppTest.java

Example80
JCode Cell
1 
2st = con.createStatement();
3} catch (Exception e) {
4e.printStackTrace();
5}
6}
7public void displayEmpDetails() {
8try {
9rs = st.executeQuery("select * from emp1");
10System.out.println("ENO\tENAME\tESAL\tEADDR");
11System.out.println("------------------------------");
12while(rs.next()) {
13System.out.print(rs.getInt(1)+"\t");
14System.out.print(rs.getString(2)+"\t");
15System.out.print(rs.getFloat(3)+"\t");
16System.out.println(rs.getString(4));
17}
18} catch (Exception e) {
19e.printStackTrace();
20}
21}
22}
23

Execute Main Class

Example81
JCode Cell
1 
2package com.durgasoft;
3

Execute Main Class

Example82
JCode Cell
1 
2import java.sql.Connection;
3import java.sql.DriverManager;
4import java.sql.ResultSet;
5import java.sql.Statement;
6

Execute Main Class

Example83
JCode Cell
1 
2import junit.framework.Test;
3import junit.framework.TestCase;
4import junit.framework.TestSuite;
5

Execute Main Class

Example84
JCode Cell
1 
2/**
3* Unit test for simple App.
4*/
5public class AppTest
6extends TestCase
7{
8/**
9* Create the test case
10*
11* @param testName name of the test case
12*/
13public AppTest( String testName )
14{
15super( testName );
16}
17

Execute Main Class

Example85
JCode Cell
1 
2/**
3* @return the suite of tests being tested
4*/
5public static Test suite()
6{
7return new TestSuite( AppTest.class );
8}
9

Execute Main Class

", "durga");

Example86
JCode Cell
1 
2/**
3* Rigourous Test :-)
4*/
5public void testApp()
6{
7System.out.println("Welcome To Maven");
8JdbcApp jdbcApp = new JdbcApp();
9jdbcApp.displayEmpDetails();
10/*
11Connection con = null;
12Statement st = null;
13ResultSet rs = null;
14try {
15Class.forName("oracle.jdbc.OracleDriver");
16con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system
17
Output

Welcome To Maven
      

Execute Main Class

Web Application in Maven

Steps to Prepare Web Application in Maven:

Example87
JCode Cell
1 
2st = con.createStatement();
3rs = st.executeQuery("select * from emp1");
4System.out.println("ENO\tENAME\tESAL\tEADDR");
5System.out.println("------------------------------");
6while(rs.next()) {
7System.out.print(rs.getInt(1)+"\t");
8System.out.print(rs.getString(2)+"\t");
9System.out.print(rs.getFloat(3)+"\t");
10System.out.println(rs.getString(4));
11}
12} catch (Exception e) {
13e.printStackTrace();
14}*/
15assertTrue( true );
16}
17}
18

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
JCode Cell
1 
2Create Web Project in Maven
3Provide Configurations in pom.xml file
4Prepare Web Resources.
5Update Project
6Run Web Application
7Create Web Project in Maven:
8

Execute Main Class

3 .Provide Servlet API Dependency:

Example89
JCode Cell
1 
2Provide Configurations in pom.xml file:
3Provide Compiler Plugin for JAVA8 version:
4<plugin>
5<groupId>org.apache.maven.plugins</groupId>
6<artifactId>maven-compiler-plugin</artifactId>
7<version>3.7.0</version>
8<configuration>
9<source>1.8</source>
10<target>1.8</target>
11</configuration>
12</plugin>
13Provide Tomcat Plugin:
14<plugin>
15<groupId>org.apache.tomcat.maven</groupId>
16<artifactId>tomcat7-maven-plugin</artifactId>
17<version>2.2</version>
18</plugin>
19

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
JCode Cell
1 
2<dependency>
3<groupId>javax.servlet</groupId>
4<artifactId>javax.servlet-api</artifactId>
5<version>3.1.0</version>
6<scope>provided</scope>
7</dependency>
8Save pom.xml file.
9Update Maven Project.
10

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
JCode Cell
1 
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/
3

Execute Main Class

Example92
JCode Cell
1 
2<modelVersion>4.0.0</modelVersion>
3<groupId>com.durgasoft</groupId>
4<artifactId>loginapp</artifactId>
5<packaging>war</packaging>
6<version>0.0.1-SNAPSHOT</version>
7<name>loginapp Maven Webapp</name>
8<url>http://maven.apache.org</url>
9<dependencies>
10<dependency>
11<groupId>junit</groupId>
12<artifactId>junit</artifactId>
13<version>3.8.1</version>
14<scope>test</scope>
15</dependency>
16<dependency>
17<groupId>javax.servlet</groupId>
18<artifactId>javax.servlet-api</artifactId>
19<version>3.1.0</version>
20<scope>provided</scope>
21</dependency>
22</dependencies>
23<build>
24<finalName>loginapp</finalName>
25<plugins>
26<plugin>
27<groupId>org.apache.maven.plugins</groupId>
28<artifactId>maven-compiler-plugin</artifactId>
29<version>3.7.0</version>
30<configuration>
31<source>1.8</source>
32<target>1.8</target>
33</configuration>
34</plugin>
35<plugin>
36<groupId>org.apache.tomcat.maven</groupId>
37<artifactId>tomcat7-maven-plugin</artifactId>
38<version>2.2</version>
39</plugin>
40</plugins>
41</build>
42</project>
43Prepare Web Resources:
44
📝 Key Takeaways
  • Key ideas of Spring - Maven Project Setup explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end