Notes for Maven

====Eclipse======
Maven Clean

Run as -> Maven Build – clean (mvn clean) -. Deletes traget dir

Maven Build… – compile (mvn compile) – Creates class files

Maven Build… – test-compile

Maven Build… – test

Maven Install -> creates jar

Maven Build… – install (jar or war will be created)

======mvn cmd========
mvn clean
mvn compile
mvn test-compile
mvn test
mvn install -> jar file

====================

Maven Build cycle (command)

validate
compile (validate, compile)
test (validate, compile)
package
integration-test
verify
install (validate, compile, test, package, integration-test,verify,install)

deploy (all the above)

clean

top Command

Top Command

by pressing :

  • 1 you will see no of cpu cores and their status
    • %CPU0 :
    • us – cpu used by user process
    • sy – system process
    • ni – show the process which has been given priority
    • id – idle time of CPU
    • wa – waiting for IO operation
    • hi – hardware interrupt like ctrl+C to stop a process which is utilizing cpu process
    • si – system interrupts
    • st – (steal time)amount of time consumed by your virtual machines hypervisor
  • M (in caps) you will sort by memory the top table
  • m – you will shot the memory is progress bar
  • P – (in caps) sort by cpu utilization
  • u – (type the user name) – to see the processes of that perticular user
  • c – command line argument of that process
  • z – it will become colorfull

In the table of top

  • PR – means priority
  • NI – Nice value(Priority that is set to that process)
  • VIR – Virtual Memory
  • RES – reserved memory (This cannot be swapped for other process)
  • SHR – Shared resources of that process
  • S – Status (R – Running in CPU, S – Sleeping

cat /proc/loadavg //when cpu utilization is high since top is heavy (w command also works)
0.41 0.65 0.65 4/813 5416
how to check loadavg?
1st section 1 min load avg
2nd section 5 min load avg
3rd section 15 min load avg

if load avg is like
20 10 5
This means your load avg is increasing and you need to worry
if load avg is like
5 19 15
This means your load avg is decreasing and you need not worry

Install and Setup Maven

Prerequisite for Maven :

  • Make sure JDK is installed
  • Make sure JAVA_HOME path variable is set

How to install Maven? (Windows)

  • Download the zip from Apache web site
  • Extract it into a directory
  • Set M2_HOME and MAVEN_HOME for the install directory
  • Set the bin folder of installed directory in path variable

Follow the steps below to install on ubuntu :

https://www.heapwizard.com/install-java-and-maven/

User/System variable for Maven

  • Add both M2_HOME and MAVEN_HOME variables in the environment variable and point it to the maven directory.

Check maven version with below command :

mvn -version

What is Maven?

  • Maven is a build tool
    • Compile Source Code
    • Copy Resource
    • Compile and Run Tests
    • Package Project
    • Deploy Project
    • Clean up

Why Maven came to be ?

  • Developers wanted a standard way of building project
  • A clear definition of what project consists of
  • Easy way to publish jar information and a way to share JARs accross several projects
  • The result is a tool that can now be used for building and managing any Java bases Projects which is called Maven

What is POM ?

  • A fundamental unit of work in Maven, POM is an XML file that contains information about project and configuration details used by Maven to build the project.
  • POM stands for Project Object Model
    • Describes a project
    • Name and Version, Artifact Type, Source Code Location, Dependencies
    • Plugins
    • Profiles (Alternate build configuration)
    • Uses XML by Default
    • Not the way Ant uses XML

Maven Objectives :

  • Making the build process easy.
  • Providing a uniform build system.
  • Providing a quality project information
  • Providing guidelines for best practices for development
  • Allowing transparent migration to new features

Soft link and Hard link

  • symbolic or soft link is an actual link to the original file, whereas a hard link is a mirror copy of the original file.
  • If you delete the original file, the soft link has no value, because it points to a non-existent file. But in the case of hard link, it is entirely opposite.
  • Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as a mirror copy of the original file.

In a nutshell, a soft link

  • can cross the file system,
  • allows you to link between directories,
  • has different inode number and file permissions than original file,
  • permissions will not be updated,
  • has only the path of the original file, not the contents.

In a nutshell, a hard link

  • can’t cross the file system boundaries (i.e. A hardlink can only work on the same filesystem),
  • can’t link directories,
  • has the same inode number and permissions of original file,
  • permissions will be updated if we change the permissions of source file,
  • has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.

View the Symbolic link on file system (ln stands for LiNk i.e soft and hard links)

ls -lia

Command to create a softlink

ln -s source.file softlink.file

Command to create a hardlink

ln source.file hardlink.file

More details on symbolic link

man ln

Installing XAMPP on ubuntu

sudo chmod +x xampp-linux-x64-7.4.6-0-installer.run
  • Now run the file using below command
sudo ./xampp-linux-x64-7.4.6-0-installer.run

Xampp will be installed at below location

/opt/lampp

Starts xampp control panel using below command :

sudo /opt/lampp/manager-linux-x64.run

Starts all the servers and test the below urls to check if everything is installed correctly :

http://localhost/dashboard/
http://localhost/phpmyadmin/

htdocs location :

/opt/lampp/htdocs

Source Code of JARs in Gradle

Note :

Jars that are added in gradle for those jars only we can download source code in gradle, if we use an external library example JDK then gradle is not going to download the source code for them, you need to add the external zip file to the eclipse to view the source code.

build.gradle

plugins {
    id 'eclipse'
}

eclipse {
    classpath {
        downloadJavadoc = true
        downloadSources = true
    }
}

Run the below command to reflect the changes in eclipse

gradle cleanEclipse eclipse

Java Predicate

To do :

  • Negate a predicate

What is predicate in General ?

  • Predicate in general meaning is a statement about something that is either true or false.
  • In programming, predicates represent single argument functions that return a boolean value.

Predicate in java 8

  • Predicates in Java are implemented with interfaces.
  • Predicate<T> is a generic functional interface representing a single argument function that returns a boolean value. 
  • It is located in the java.util.function package.
  • It contains a test(T t) method that evaluates the predicate on the given argument.

Example :


import java.util.List;
import java.util.function.Predicate;

class BiggerThanFive<E> implements Predicate<Integer> {

    @Override
    public boolean test(Integer v) {

        Integer five = 5;

        return v > five;
    }
}

public class JavaPredicateEx {

    public static void main(String[] args) {

        List<Integer> nums = List.of(2, 3, 1, 5, 6, 7, 8, 9, 12);

        BiggerThanFive<Integer> btf = new BiggerThanFive<>();
        nums.stream().filter(btf).forEach(System.out::println);
    }
}

Example : Lambda

import java.util.List;
import java.util.function.Predicate;

public class JavaPredicateEx2 {

    public static void main(String[] args) {

        List<Integer> nums = List.of(2, 3, 1, 5, 6, 7, 8, 9, 12);

        Predicate<Integer> btf = n -> n > 5;

        nums.stream().filter(btf).forEach(System.out::println);
    }
}

Reference :

http://zetcode.com/java/predicate/

Mock Data

You can generate your own mockdata at https://mockaroo.com/

Reading Mock Data file :

  • Place your mock data in your resource folder
  • Write the below piece of code to read your data
public class MockData {

  public static ImmutableList<Person> getPeople() throws IOException {
    InputStream inputStream = Resources.getResource("people.json").openStream();
    String json = IOUtils.toString(inputStream);
    Type listType = new TypeToken<ArrayList<Person>>() {
    }.getType();
    List<Person> people = new Gson().fromJson(json, listType);
    return ImmutableList.copyOf(people);
  }

  public static ImmutableList<Car> getCars() throws IOException {
    InputStream inputStream = Resources.getResource("cars.json").openStream();
    String json = IOUtils.toString(inputStream);
    Type listType = new TypeToken<ArrayList<Car>>() {
    }.getType();
    List<Car> cars = new Gson().fromJson(json, listType);
    return ImmutableList.copyOf(cars);
  }

build.gradle

plugins {
    id 'java'
    id 'application'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.google.guava:guava:28.0-jre'
    testImplementation 'junit:junit:4.12'
	testImplementation 'com.google.guava:guava:11.0.2'
	testImplementation 'org.assertj:assertj-core:3.6.1'
	testImplementation 'com.google.code.gson:gson:2.3.1'
	testImplementation 'commons-io:commons-io:2.6'
}

application {
    mainClassName = 'java8.App'
}

Below are the json file downloaded from mockaroo (people.json and cars.json)

Beans :

package beans;

public class Car {

  private final Integer id;
  private final String make;
  private final String model;
  private final String color;
  private final Integer year;
  private final Double price;


  public Car(Integer id, String make, String model, String color, Integer year,
      Double price) {
    this.id = id;
    this.make = make;
    this.model = model;
    this.color = color;
    this.year = year;
    this.price = price;
  }

  public Integer getId() {
    return id;
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return model;
  }

  public String getColor() {
    return color;
  }

  public Integer getYear() {
    return year;
  }

  public Double getPrice() {
    return price;
  }

  @Override
  public String toString() {
    return "Car{" +
        "id=" + id +
        ", make='" + make + '\'' +
        ", model='" + model + '\'' +
        ", color='" + color + '\'' +
        ", year=" + year +
        ", price=" + price +
        '}';
  }
}
package beans;

public class Person {

  final Integer id;
  final String firstName;
  final String lastName;
  final String email;
  final String gender;
  final Integer age;

  public Person(Integer id, String firstName, String lastName, String email, String gender,
      Integer age) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.gender = gender;
    this.age = age;
  }

  public Integer getId() {
    return id;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public String getEmail() {
    return email;
  }

  public String getGender() {
    return gender;
  }

  public Integer getAge() {
    return age;
  }

  @Override
  public String toString() {
    return "Person{" +
        "id=" + id +
        ", firstName='" + firstName + '\'' +
        ", lastName='" + lastName + '\'' +
        ", email='" + email + '\'' +
        ", gender='" + gender + '\'' +
        ", age=" + age +
        '}';
  }
}
package beans;

public class PersonDTO {

  private final Integer id;
  private final String name;
  private final Integer age;

  public PersonDTO(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  public Integer getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public Integer getAge() {
    return age;
  }


  @Override
  public String toString() {
    return "PeopleDTO{" +
        "id=" + id +
        ", name='" + name + '\'' +
        ", age=" + age +
        '}';
  }

  public static PersonDTO map(Person person) {
    return new PersonDTO(person.getId(), person.getFirstName(), person.getAge());
  }
}

Example :

  @Test
  public void declarativeApproachUsingStreams() throws Exception {
    ImmutableList<Person> people = MockData.getPeople();
    
    MockData.getPeople()
      .stream()
      .filter(p -> p.getAge() <= 18)
      .limit(10)
      .collect(Collectors.toList())
      .forEach(System.out::println);
  }