Professional Documents
Culture Documents
Maven
Maven
—-----
In general, while preparing projects , Java
developers have to perform the following activities .
1.Preparing Project Structure
2.Downloading Dependencies[Jars] and attach all the
dependencies to the project.
3.Compile the Code
4.Unit Testing
5.Generating Test reports
6.Start the server
7.Deploying web application
8.Generating Documentation
9.Undeploying web application
10. Stop the server
—--------
—---------
MAVEN Architecture:
Where a plugin is an action or development activity , when
we select a plugin in Maven , automatically MAVEN software
will perform the following actions.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>appName</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Logical Name of the project</name>
<description> description about the
project</description>
<packaging>jar/war/pom</packaging>
</project>
Local Repository:
It is a location in our computer,it will be created when we
execute the first maven application after installation of
the maven.
“C:\Users\{User}\.m2\repository”
Central Repository:
It is a location in the web[Website] maintained by Apache
Software Foundations and some other third party
organizations collaboration, it is a frequently used
repository in Maven projects, it is able to provide almost
all the dependencies which are free sources.
<project>
—----
<repositories>
<repository>
<central>
<id>central</id>
<url>https://repo.nexus.repository/maven</url>
</central>
</repository>
</repositories>
—-----
</project>
Remote Repository:
It is a location in our company , it is able to provide our
project specific dependencies.
<project>
—----
<repositories>
<repository>
<remoteRepository>
<id>remote</id>
<url>https://com.durgasoft.libraries</url>
</remoteRepository>
</repository>
</repositories>
—-----
</project>
Dependency Management:
—------------------------
Dependency is a jar file which is required in our
application.
EX:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>run</scope>
</dependency>
Dependencies Scope:
—--------------------
In general, In the projects we need dependencies for the
following activities of the project.
1. Compilation of the project
2. Testing the Project
3. Running the project
1. Compile
2. Provided
3. Runtime
4. Test
5. System
6. Import
Compile:
It is able to provide the dependency to all the phases of
the project like Compilation, Testing and Runtime.
EX:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
<scope>compile</scope>
</dependency>
Provided:
It is able to provide the dependency for compilation and
testing but not for runtime.
EX:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
Runtime:
It is able to provide the dependency at runtime and at
testing.
EX:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>runtime</scope>
</dependency>
Test:
It is able to provide the dependency for the project
testing.
EX:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
System:
It will not provide dependency by searching in the
repositories, it will provide the dependency from the sun
directory structures in our project.
import:
It is a deprecated scope , not utilized in the present
applications, it will be used to override the pom
configurations by importing super pom configurations.
Transitive Dependencies:
—-------------------------
One dependency is dependent on another dependency, this
process is called “Transitive Dependency”, MAVEN is able to
get all the dependencies in transitive dependency mode only.
Build Configurations:
—--------------------
The main purpose of Build configuration is to configure
plugins.
1. <project>
2. ...
3. <build>
4. <pluginManagement>
5. <plugins>
6. <plugin>
7. <groupId>org.apache.maven.plugins</groupId>
8. <artifactId>maven-compiler-plugin</artifactId>
9. <version>3.11.0</version>
10. <configuration>
11. <source>1.17</source>
<target>1.17</target>
12. </configuration>
13. </plugin>
14. </plugins>
15. </pluginManagement>
16. </build>
17. …
18. </project>
Profiles configurations:
—-------------------------
In general, in any Project , there are three main lifecycle
stages.
1. Development
2. Testing Mode
3. Production Mode
1. <profiles>
2. <profile>
3. <id>profileName</id>
4. <activation>
5. <activeByDefault>true</activeByDefault>
6. </activation>
<properties>
—---------
—---------
</properties>
7. ...
8. </profile>
9. </profiles>
EX:
—--
<project>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
—----Development configurations—----
<properties>
</profile>
<profile>
<id>test</id>
<properties>
—----Test configurations—----
<properties>
</profile>
<profile>
<id>production</id>
<properties>
—----Production configurations—----
<properties>
</profile>
</profiles>
</project>
Project Inheritance:
—---------------------
In Java applications, we are able to define inheritance
relations between classes in order to reuse one class
properties and behaviors in another class in order to
improve Code Reusability.
EX:
Parent pom
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
—----
—----
</project>
Child pom:
<project>
—-
<parent>
<groupId>com.durgasoft</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0</version>
</parent>
—--
</project>
IN MAVEN, for each and every pom there is a common and
default super pom, which contains all the minimum
configurations of the projects, which includes default
repositories, ……
“org\apache\maven\model\pom.xml”.
mvn help:effective-pom
ArcheTypes:
—-----------
Archetype is Project type in order to provide the required
directory structure and the required setups.
mvn archetype:generate
App.java
package com.durgasoft;
AppTest.java
package com.durgasoft;
import org.junit.Test;
public class AppTest
{
@Test
public void shouldAnswerWithTrue()
{
System.out.println("Hello User!, This is from
JUNIT AppTest.java");
assertTrue( true );
}
}
To run AppTest.java file use the following command on
command prompt
mvn test
java com.durgasoft.App
<!--
https://mvnrepository.com/artifact/mysql/mysql-connector-jav
a -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
EX:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>app20</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.source
Encoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
</project>
Main.java
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) {
try(
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/
durgadb", "root", "root");
Statement statement =
connection.createStatement();
ResultSet resultSet =
statement.executeQuery("select * from emp1");
) {
System.out.println("ENO\tENAME\tESAL\tEADDR");
System.out.println("-----------------------------");
while(resultSet.next()){
System.out.print(resultSet.getInt("ENO")+"\t");
System.out.print(resultSet.getString("ENAME")+"\t\t");
System.out.print(resultSet.getFloat("ESAL")+"\t");
System.out.print(resultSet.getString("EADDR")+"\n");
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
oracle-ojdbc-21-xe.pom
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>oracle</groupId>
<artifactId>oracle-ojdbc</artifactId>
<version>21-xe</version>
<description>POM was created from
install:install-file</description>
</project>
EX:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>app20</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.source
Encoding>
</properties>
<dependencies>
<dependency>
<groupId>oracle</groupId>
<artifactId>oracle-ojdbc</artifactId>
<version>21-xe</version>
</dependency>
</dependencies>
</project>
Main.java
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
System.out.println("ENO\tENAME\tESAL\tEADDR");
System.out.println("-----------------------------");
while(resultSet.next()){
System.out.print(resultSet.getInt("ENO")+"\t");
System.out.print(resultSet.getString("ENAME")+"\t\t");
System.out.print(resultSet.getFloat("ESAL")+"\t");
System.out.print(resultSet.getString("EADDR")+"\n");
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<finalName>app03</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</build>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
package com.durgasoft.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response.setContentType("text/html");
out.println("<html>");
out.println("<body>");
out.println("</body></html>");
}
6. Run web application and access the web resources
EX:
loginform.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<h2 style="color: red;" align="center">Durga Software
Solutions</h2>
<table align="center">
<tr>
<td>User Name</td>
</tr>
<tr>
<td>Password</td>
<td><input type="password"
name="upwd"></td>
</tr>
<tr>
<td><input type="submit"
value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
Success.html
<html>
<head>
</head>
<body>
<br><br><br>
Login Success
</h1>
<h3 align="center">
</h3>
</body>
</html>
Failure.html
<html>
<head>
</head>
<body>
<br><br><br>
Login Failure
</h1>
<h3 align="center">
</h3>
</body>
</html>
UserAction.java
package com.durgasoft.app04.action;
import java.sql.ResultSet;
import com.durgasoft.app04.factories.ConnectionFactory;
try {
ResultSet resultSet =
ConnectionFactory.getConnection().
createStatement().
if(resultSet.next()) {
status = "success";
}else {
status = "failure";
} catch (Exception e) {
e.printStackTrace();
return status;
LoginServlet.java
package com.durgasoft.app04.servlet;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.durgasoft.app04.action.UserAction;
if(status.equalsIgnoreCase("success")) {
requestDispatcher =
request.getRequestDispatcher("/success.html");
requestDispatcher.forward(request, response);
}else {
requestDispatcher =
request.getRequestDispatcher("/failure.html");
requestDispatcher.forward(request, response);
ConnectionFactory.java
package com.durgasoft.app04.factories;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dur
gadb", properties);
} catch (Exception e) {
e.printStackTrace();
return connection;
db.properties
user = root
password = root
Web.xml
<web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee">
<welcome-file-list>
<welcome-file>loginform.html</welcome-file>
</welcome-file-list>
</web-app>
Pom.xml
<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.durgasoft</groupId>
<artifactId>app04</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--
https://mvnrepository.com/artifact/jakarta.servlet/jakarta.s
ervlet-api -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0-M2</version>
<scope>provided</scope>
</dependency>
<!--
https://mvnrepository.com/artifact/mysql/mysql-connector-jav
a -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
<build>
<finalName>app04</finalName>
</build>
</project>
EX:
Hello.java
package com.durgasoft.app05.beans;
this.name = name;
}
public String getName() {
return name;
App.java
package com.durgasoft.app05;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationC
ontext;
import com.durgasoft.app05.beans.Hello;
System.out.println(hello.sayHello());
}
SpringConfig.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/be
ans
https://www.springframework.org/schema/beans/spring-beans.xs
d">
</bean>
</beans>
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>app05</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>app05</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEnc
oding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.25</version>
</dependency>
<!--
https://mvnrepository.com/artifact/commons-logging/commons-l
ogging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>