PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   09-10-2021

Maven

To install on Windows download maven binary zip from apache website. Add /bin folder to your path and test maven installation.
 
mvn -v

# Apache Maven 3.8.2

PROJECT

Setup a Java project for Maven to build. Maven needs Java JDK 8 or later to build your project.
 
# /myproject/src/main/java/hello/HelloWorld.java

package hello;
public class HelloWorld {
    public static void main(String[] args) {
        Greeter.sayHello();
    }
}
... 1 lines
 
 
# /myproject/src/main/java/hello/HelloWorld.java

package hello;
public class Greeter {
    public void sayHello() {
        System.out.println("Hello World");
    }
}
... 1 lines
 
Create Maven pom.xml project definition (next to src folder).
 
<?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 
        https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>hello-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer 
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
... 35 lines
 
Compile and run the project.
 
cd D:\developments\myproject
mvn package
 
java -jar target/hello-maven-0.1.0.jar
    #Hello World

Dependencies

Add dependencies to external library in pom.xml (withing project element).
 
<?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 
        https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>hello-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.2</version>
    </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer 
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
... 42 lines
 
Use external library (joda) in your project.
 
// /myproject/src/main/java/hello/HelloWorld.java

package hello;
import org.joda.time.LocalTime;

public class HelloWorld {
    public static void main(String[] args) {

        LocalTime currentTime = new LocalTime();
        System.out.println("The current local time is: " + currentTime);
        Greeter.sayHello();
  }
}
... 4 lines
 
Compile and run the project.
 
java -jar ./target/hello-maven-0.1.0.jar
    # The current local time is: 16:57:00.610
    # Hello World

.M2/

Maven also maintains a local repository for dependencies quick access. The .m2 directory is located in your home directory.
 
C:\Users\Catalin\.m2\repository
Spring Boot Project    (2/2)

Spring Boot Project

Create a Spring Boot Web project.
 
/**
 * /sbproject/src/main/java/com/example/App.java 
 */

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class App {

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

    @GetMapping("/sbproject/")
    public String welcome() {
        return "Welcome";
    }

    @GetMapping("/sbproject/hello")
    public String hello() {
        return "Hello World";
    }

}
... 17 lines
 
Add Spring Boot dependencies to 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 
        https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sbproject</artifactId>
    <version>0.0.1</version>
    <name>spproject</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

</project>
... 34 lines
 
Compile, build and run the project.
 
mvn package
 
java -jar ./target/sbproject-0.0.1.jar
 
http://localhost:8080/sbproject/
    #Welcome

http://localhost:8080/sbproject/hello/
    #Hello World

Questions    
Ajax