GithubHelp home page GithubHelp logo

in28minutes / mavenin28minutes Goto Github PK

View Code? Open in Web Editor NEW
193.0 193.0 439.0 872 KB

Maven Tutorial for Beginners with Examples

Home Page: http://www.in28minutes.com

License: MIT License

Java 96.04% CSS 3.96%

mavenin28minutes's Introduction

Maven Tutorial for Beginners - with Examples In28Minutes

Image

Installing Eclipse and Java

https://github.com/in28minutes/SpringIn28Minutes/blob/master/InstallationGuide-JavaEclipseAndMaven_v2.pdf

Course Overview

  • We will use Handson Real World Examples to understand what Maven can do.
  • We will understand how Maven makes the life of an application developer easy.
  • We will learn how Maven helps us to automate things like compilation, running unit tests, creating a war, creating an ear, running a web application in tomcat.
  • We will learn how to use Maven effectively in combination of an IDE like Eclipse.
  • We will use
    • Maven for dependency management, building and running the application in tomcat.
    • Eclipse IDE.

Step List

Lets now look at the steps in this tutorial.

  • Step 1 to 4 we will learn about the basics of Maven : Project Object Model and Build LifeCycle
  • Steps 5 and 6 we will learn about dependency management and transitive dependencies
  • Step 7 we will learn about a maven project with multiple layers. Typical projects have a web layer, data layer, external interface layer. We will learn how to create such projects using maven. Multi Module Maven Project.
  • Step 8 : We will learn how to create a war, deploy to tomcat.

Running Examples

Youtube Video

https://courses.in28minutes.com/p/maven-tutorial-for-beginners-in-5-steps

0. What is Maven?

Defining what Maven does is very difficult.

Every Day Developer

	Manages Dependencies - Web Layer (Spring MVC), Data Layer (JPA - Hibernate) etc..                  
	Build a jar or a war or an ear
	Run the application locally - Tomcat or Jetty
	Deploy to a T environment
	Add new dependencies to a project
	Run Unit Tests

Maven helps us do all these and more...

	Generate Projects
	Create Eclipse Workspace

1. Beginner Maven Project

What is the power of Maven?

First Project : My aim is to create a Spring.jar. Think as if you are developing Spring. Some other developers want to use the framework you are developing. Steps in creating a JAr

  • App.Java -> App.class
  • AppTest.Java -> AppTest.class
  • Run Unit Tests
  • Package in a particular format
  • Earlier this was done using long tedious ant scripts

Convention over Configuration

  • Pre defined folder structure
  • pom.xml
  • mvn --version
  • mvn compile (compiles source files)
  • mvn test-compile (compiles test files) - one thing to observe is this also compiles source files
  • mvn clean - deletes target directory
  • mvn test - run unit tests

You are ready for theory on Build Life Cycle

Pre-defined sequence of steps that are done when we run a maven command. Plugins can be attached to lifecycle stages. Default plugins are already defined in the super pom.

mvn install

  • package - creates the jar
  • install - copies the created jar to local maven repository - a temp folder on my machine where maven stores the files.

Build LifeCycle

	Validate
	Compile
	Test
	Package
	Integration Test
	Verify
	Install
	Deploy

Lets understand pom.xml

Project Object Model (POM)

	Name (if another project want to refer to our project, how do they do it?)
	Version (Major Version, Minor Version, Incremental Version)
	Packaging 
	Dependencies
	Plugins

Maven repository stores all the versions of all dependencies. JUnit 4.2,4.3,4.4

Local Repository - all the dependencies that are downloaded for 1st time are stored.

2. Intermediate Maven Project

  • Transtive Dependencies (add Hibernate dependency)
  • Exclusions - Add an exclude
  • Dependency Hierarchy - See how each dependency is coming in
  • Versions [4.1,] [,4.1] [,4.1)
  • Scope - Dependencies are needed only for tests. They are not part of the war or jar. we can use scope for that.
  • Maven Compiler Plugin - Change source from 1.5 to 1.8.
  • Effective Pom - Super Pom (Similar to Java Inheritance) - Check the build part of super pom (Convention over configuration - defaults are defined). We can change it - for example source directory. Recommended not to override defaults. Easy to move from one project to another project. Definitions of various plugins. mvn help:effective-pom

Dependency Management

	Scope
	Transitive Dependencies
	Excluding Dependency
	Dependency Versions

Sample Project Object Model

	Maven Plugins
	Convention over configuration
		Source Code
			${basedir}/src/main/java
			${basedir}/src/main/resources
		Test Code
			${basedir}/src/test

Hierarchy of POMS

	Super POM

3. Multi Module Maven Project

  • Most projects have multiple layers. Each layer has its own dependencies. Also multiple layers may share same dependency. We will see the best practices in managing dependencies in a multi module maven project.
  • Parent pom has type pom
    • Defines Modules
  • Dependency Management Section
  • Properties
  • Pre-defined Variables ${project.version}
  • Run from command prompt - mvn clean install

4. Maven Web Application

Packaging

	War
	Ear

Running application in Tomcat

5. Tip and Tricks

Important Commands

	help:effective-settings
	help:effective-pom
	dependency:tree
	dependency:sources
	--debug

Maven Archetypes

	archetype:generate

6. Missing (To Discuss)

  • Plugins : Show in super pom.xml
  • Eclipse Integration
  • Profiles

Expectations

  • You should know Java.

Running Examples

  • Download the zip or clone the Git repository.
  • Unzip the zip file (if you downloaded one)
  • Open Command Prompt and Change directory (cd) to folder containing pom.xml
  • Open Eclipse
    • File -> Import -> Existing Maven Project -> Navigate to the folder where you unzipped the zip
    • Select the right project
  • Choose the Spring Boot Application file (search for @SpringBootApplication)
  • Right Click on the file and Run as Java Application
  • You are all Set

Troubleshooting

Youtube Playlists - 500+ Videos

Click here - 30+ Playlists with 500+ Videos on Spring, Spring Boot, REST, Microservices and the Cloud

Keep Learning in28Minutes

in28Minutes is creating amazing solutions for you to learn Spring Boot, Full Stack and the Cloud - Docker, Kubernetes, AWS, React, Angular etc. - Check out all our courses here

mavenin28minutes's People

Contributors

in28minutes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mavenin28minutes's Issues

Maven issue instalation

Hello Team

I am trying to install Maven and i am getting some errors :

  • please refer to file output of mvn -X install -> MVN intallation error.txt

  • my java version is :

java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

  • please refer to pom.xml configuration file (I have renamed to txt just to downlead it here )

  • Maven version is

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: C:\maven
Java version: 19.0.1, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-19
Default locale: en_US, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

MVN intallation error.txt
pom.txt

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.