0% found this document useful (0 votes)
2 views15 pages

JavaProgrammingForBeginners-Presentation-61-75

Maven is a build automation tool used primarily for Java projects that simplifies project management by handling dependencies, building JAR files, and running applications. It follows a predefined structure and utilizes a Project Object Model (pom.xml) to manage project configurations and dependencies. The document also discusses Maven commands, Spring Boot integration, versioning schemes, and the transition from JDBC to Spring Data JPA for easier database management.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views15 pages

JavaProgrammingForBeginners-Presentation-61-75

Maven is a build automation tool used primarily for Java projects that simplifies project management by handling dependencies, building JAR files, and running applications. It follows a predefined structure and utilizes a Project Object Model (pom.xml) to manage project configurations and dependencies. The document also discusses Maven commands, Spring Boot integration, versioning schemes, and the transition from JDBC to Spring Data JPA for easier database management.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

What is Maven?

Things you do when writing code each day:


Create new projects
Manages dependencies and their versions
Spring, Spring MVC, Hibernate,...
Add/modify dependencies
Build a JAR file
Run your application locally in Tomcat or Jetty or ..
Run unit tests
Deploy to a test environment
and a lot more..
Maven helps you do all these and more...

61
Exploring Project Object Model - pom.xml
Let's explore Project Object Model - pom.xml
1: Maven dependencies: Frameworks & libraries used in a project
Ex: spring-boot-starter-web and spring-boot-starter-test
Why are there so many dependencies in the classpath?
Answer: Transitive Dependencies
(REMEMBER) Spring dependencies are DIFFERENT

2: Parent Pom: spring-boot-starter-parent


Dependency Management: spring-boot-dependencies
Properties: java.version, plugins and configurations
3: Name of our project: groupId + artifactId
1: groupId: Similar to package name
2: artifactId: Similar to class name
Why is it important?
Think about this: How can other projects use our new project?

Activity: help:effective-pom, dependency:tree & Eclipse UI


Let's add a new dependency: spring-boot-starter-web

62
Exploring Maven Build Life Cycle
When we run a maven command, maven build life
cycle is used
Build LifeCycle is a sequence of steps
Validate
Compile
Test
Package
Integration Test
Verify
Install
Deploy

63
How does Maven Work?
Maven follows Convention over Configuration
Pre defined folder structure
Almost all Java projects follow Maven structure (Consistency)
Maven central repository contains jars (and others) indexed
by artifact id and group id
Stores all the versions of dependencies
repositories > repository
pluginRepositories > pluginRepository
When a dependency is added to pom.xml, Maven tries to
download the dependency
Downloaded dependencies are stored inside your maven local repository
Local Repository : a temp folder on your machine where maven stores the
jar and dependency files that are downloaded from Maven Repository.

64
Important Maven Commands
mvn --version
mvn compile: Compile source files
mvn test-compile: Compile test files
OBSERVCE CAREFULLY: This will also compile source files
mvn clean: Delete target directory
mvn test: Run unit tests
mvn package: Create a jar
mvn help:effective-pom
mvn dependency:tree

65
Spring Boot Maven Plugin
Spring Boot Maven Plugin: Provides Spring Boot
support in Apache Maven
Example: Create executable jar package
Example: Run Spring Boot application
Example: Create a Container Image
Commands:
mvn spring-boot:repackage (create jar or war)
Run package using java -jar
mvn spring-boot:run (Run application)
mvn spring-boot:start (Non-blocking. Use it to run integration tests.)
mvn spring-boot:stop (Stop application started with start command)
mvn spring-boot:build-image (Build a container image)

66
How are Spring Releases Versioned?
Version scheme - MAJOR.MINOR.PATCH[-MODIFIER]
MAJOR: Significant amount of work to upgrade (10.0.0 to 11.0.0)
MINOR: Little to no work to upgrade (10.1.0 to 10.2.0)
PATCH: No work to upgrade (10.5.4 to 10.5.5)
MODIFIER: Optional modifier
Milestones - M1, M2, .. (10.3.0-M1,10.3.0-M2)
Release candidates - RC1, RC2, .. (10.3.0-RC1, 10.3.0-RC2)
Snapshots - SNAPSHOT
Release - Modifier will be ABSENT (10.0.0, 10.1.0)

Example versions in order:


10.0.0-SNAPSHOT, 10.0.0-M1, 10.0.0-M2, 10.0.0-RC1, 10.0.0-RC2, 10.0.0, ...
MY RECOMMENDATIONS:
Avoid SNAPSHOTs
Use ONLY Released versions in PRODUCTION

67
REST API
REST API: Architectural Style for the Web
Resource: Any information (Example: Courses)
URI: How do you identify a resource? (/courses, /courses/1)
You can perform actions on a resource (Create/Get/Delete/Update). Different HTTP Request
Methods are used for different operations:
GET - Retrieve information (/courses, /courses/1)
POST - Create a new resource (/courses)
PUT - Update/Replace a resource (/courses/1)
PATCH - Update a part of the resource (/courses/1)
DELETE - Delete a resource (/courses/1)
Representation: How is the resource represented? (XML/JSON/Text/Video etc..)
Server: Provides the service (or API)
Consumer: Uses the service (Browser or a Front End Application)

68
Spring and Spring Boot Release Cycles
What is the difference between these?
2.5.0 (SNAPSHOT)
2.4.5 (M3)
2.4.4
Release Number: MAJOR.MINOR.FIX
Spring and Spring Boot Release Cycle:
SNAPSHOT (versions under development) > Mile Stones > Released Version
Recommendation - Do NOT use SNAPSHOTs or M1 or M2 or M3
Prefer released versions!

69
JDBC to Spring JDBC to JPA to Spring Data JPA
JDBC
Write a lot of SQL queries!
And write a lot of Java code
Spring JDBC
Write a lot of SQL queries
BUT lesser Java code
JPA
Do NOT worry about queries
Just Map Entities to Tables!
Spring Data JPA
Let's make JPA even more simple!
I will take care of everything!

70
JDBC to Spring JDBC
JDBC example
public void deleteTodo(int id) {
PreparedStatement st = null;
try {
st = db.conn.prepareStatement(DELETE_TODO_QUERY);
st.setInt(1, id);
st.execute();
} catch (SQLException e) {
logger.fatal("Query Failed : " + DELETE_TODO_QUERY, e);
} finally {
if (st != null) {
try {st.close();}
catch (SQLException e) {}
}
}
}

Spring JDBC example


public void deleteTodo(int id) {
jdbcTemplate.update(DELETE_TODO_QUERY, id);
}

71
JPA Example
@Repository
@Transactional
public class PersonJpaRepository {

@PersistenceContext
EntityManager entityManager;

public Person findById(int id) {


return entityManager.find(Person.class, id);
}

public Person update(Person person) {


return entityManager.merge(person);
}

public Person insert(Person person) {


return entityManager.merge(person);
}

public void deleteById(int id) {........

Spring Data JPA Example


public interface TodoRepository extends JpaRepository<Todo, Integer>{

72
Spring Boot Auto Configuration Magic - Data JPA
We added Data JPA and H2 dependencies:
Spring Boot Auto Configuration does some magic:
Initialize JPA and Spring Data JPA frameworks
Launch an in memory database (H2)
Setup connection from App to in-memory database
Launch a few scripts at startup (example: data.sql)

Remember - H2 is in memory database


Does NOT persist data
Great for learning
BUT NOT so great for production
Let's see how to use MySQL next!

73
Congratulations
Java keeps improving:
Java 10, Java 11, Java 12, ...
Java Project - REST API in Modern Approach:
Spring
Spring Boot
Do NOT forget to leave a Review!

74
What's Next? - Don't Stop Learning!
https://github.com/in28minutes/learn

Step I: Build more applications:


REST API and Microservices
Full Stack Applications (Angular and React)
Mobile Applications
Learn Unit Testing (JUnit and Mockito) and Clean Code
Step II: Learn Java Frameworks in Depth:
Spring & Spring Boot
Hibernate and JPA
Step III: Go Cloud (AWS, Azure and Google Cloud)
Step IV: Learn DevOps

75

You might also like