Skip to content

Latest commit

 

History

History

maven-in-5-steps

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

First 5 Steps in Maven

  • It is tough to define what Maven accomplishes.

  • Every day, Developer undertakes a variety of tasks.

    • 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
  • All of this and more is made possible by Maven...

    • Generate Projects
    • Create Eclipse Workspace

Getting Started

Step 1 : Creating and importing a Maven Project

  • We will create a Spring Boot project using http://start.spring.io
  • We will import it into Eclipse as a Maven Project

Step 2 : Understanding the pom.xml Project Object Model

  • Naming a project
  • Declaring Dependencies

Step 3 : Maven Build Life Cycle

  • run "mvn clean install"
  • Life Cycle Maven Build - Validate > Compile > Test > Package > Integration Test > Verify > Install > Deploy
  • Predefined folder structure trumps configuration.
    • Source Code
      • ${basedir}/src/main/java
      • ${basedir}/src/main/resources
    • Test Code
      • ${basedir}/src/test

Step 4 : How does Maven Work?

  • Local Repository
  • Maven repository
    • stores all the versions of all dependencies. JUnit 4.2,4.3,4.4
    • mvn install - copies the created jar to local maven repository - a temp folder on my machine where maven stores the files.

Step 5 : Important Maven Commands

  • 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
  • mvn package - creates the jar
  • help:effective-settings
  • help:effective-pom
  • dependency:tree
  • dependency:sources
  • --debug

Complete Code Example

/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.in28minutes.learning.maven</groupId>
    <artifactId>maven-in-few-steps</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

    <name>maven-in-few-steps</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>17</java.version>
        <junit-jupiter.version>5.9.2</junit-jupiter.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <!--

    Remote Maven Repository
    ->
    Local Maven Repository

     - Local Repository => Local System

     - Remote Maven repository => Central Repositories
       - stores all the versions of all dependencies. JUnit 4.2,4.3,4.4

     - mvn install vs mvn deploy
       - copies the created jar to local maven repository - a temp folder on my machine where maven stores the files.
    -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

/src/main/java/com/in28minutes/learning/maven/maveninfewsteps/MavenInFewStepsApplication.java

package com.in28minutes.learning.maven.maveninfewsteps;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MavenInFewStepsApplication {

    public static void main(String[] args) {
        SpringApplication.run(MavenInFewStepsApplication.class, args);
    }
}

/src/main/resources/application.properties


/src/test/java/com/in28minutes/learning/maven/maveninfewsteps/MavenInFewStepsApplicationTests.java

package com.in28minutes.learning.maven.maveninfewsteps;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MavenInFewStepsApplicationTests {

    @Test
    public void contextLoads() {
    }

}