Posts

Spring Boot Maven Plugin Explained In Detail

Maven is a build automation tool for Java. The Spring Boot Maven plugin provides various capabilities for developing/executing Spring Boot projects with Maven. In this article, we will take a closer look at this plugin. What is the Spring Boot Maven Plugin The Spring Boot Maven plugin simplifies the process of building, packaging, and running Spring Boot applications using Maven.  Some of the things that you can do using this plugin are: Build/package Spring Boot applications as executable JAR or WAR files. These files include all necessary dependencies, making it easy to deploy and run Spring Boot applications. Start an embedded application server (such as Tomcat or Jetty) and deploy your Spring Boot application on it during development and testing. Manage and resolve dependencies specific to Spring Boot, ensuring that you have compatible versions of Spring Boot libraries and other related dependencies. Simplify configuration management by setting Spring Boot properties directly in yo...

Maven clean install command explained

Maven is a popular build automation and project management tool primarily used for Java projects. Java developers often run the maven clean install command to build Java projects. In this blog post, I will try to explain what the maven clean install command does. Let us break down this command.   Clean This part of the command tells Maven to clean the project. Cleaning a project means removing all the build artifacts (compiled classes, libraries, generated files, etc.) from previous builds. This ensures that you start with a clean slate before building the project again. It’s particularly useful to eliminate any potential issues caused by leftover artifacts from previous builds.   Install   This part of the command tells Maven to build and package the project and then install the resulting artifact(s) into the local Maven repository. The local repository is a local cache where Maven stores project dependencies and artifacts. When you use “install,” the ...

How to create a Jar file from a Spring Boot Application using Maven

In this article, we will learn how to create a JAR file from a Spring Boot application using Maven. Introduction The spring-boot-maven-plugin allows packaging a Spring Boot application as a JAR/WAR file. It needs to be specified in the project  pom.xml along with the type of packaging ( jar / war ). Project Creation and Setup Step 1 – Let us use an existing project as a starting point. Clone the code from this Spring Boot Thymeleaf project. Step 2 – Update the pom.xml to specify the packaging and the `spring-boot-maven-plugin` configuration. So the pom file should look as follows: <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>com.learnjava</groupId> <artifactId>learnjava-springbootthymeleaf-demo</artifactId...

New Features in Java 11 Explained in Detail

Java 11, is the latest long-term support (LTS) version of the Java programming language. It brings along several exciting features and improvements. In this blog post, we’ll take high level look into the New Features in Java 11. Local Variable Syntax for Lambda Parameters One of the most important features in Java 11 is the ability to use the var keyword for lambda parameters. This allows for more concise and readable code when working with lambda expressions. Java 10 introduced the Local Variable Type Inference feature . However, the var keyword could not be used with lambda expressions. So, you would need to write a lambda expression as follows: (int a, int b) -> a + b This code can be rewritten in Java 11 as follows: (var a, var b) -> a + b   HTTP Client Improvements Java 11 introduces a new and modern HTTP client API that supports HTTP/1.1, HTTP/2, and WebSocket. This new client offers a more flexible and efficient way to make HTTP requests and process responses. ...

Introduction to Docker to get you started

On a recent project, I got an opportunity to work with Docker. In this article, I will be giving an introduction to Docker and explaining some basic Docker concepts. What is Docker? Docker is a container service that helps developers quickly package and run an application in any environment. It addresses the one problem that every developer has faced at least once in her life, “It works on my machine!” For example, suppose there are multiple developers working in different environments like Linux/Windows and MacOS. Docker makes it easy to move the complete application setup from one environment to the other. Suppose the application being worked on is a Java application that requires different pieces of software like a web container like Tomcat, a database like MySQL, and so on. Without Docker, you would need to individually install each software in order to set up the development environment on a different machine. Not only that, but when you move from development to test/pr...

How to use logging in SpringBoot with code samples

Image
In this article, we will learn how to use logging in SpringBoot. Introduction There are several Java logging frameworks like logback, log4j, and so on. Spring Boot supports out-of-the-box support for logback. Let us learn how to use logging in SpringBoot with logback. Step 1 – Create a New Maven Project Create a new Maven project. (Refer to  this  blog post). This will create a project in Eclipse as follows:   Step 2 – Add the Spring Boot Dependencies Add the spring-boot-starter-web dependency to the pom.xml . So, the pom.xml should look as follows: <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>com.learnjava</groupId> <artifactId>learnjava-springboot-logging-example</artifactId> <version>0.0.1-SNAPSHOT...

How to create a Maven web project using archetype selection

Image
In my earlier article, we learned how to create a Java web project in Eclipse and how to add Maven support to it.  In this article, we will learn how to create a Maven web project using archetype selection. Create Maven Web Project In Eclipse Step 1 – In Eclipse, Click on File > New > Other . Click on Maven Project . Click Next : Step 2 – The following screen is displayed. Click Next : Step 3 – In the Filter text box, type “ maven-archetype-webapp “.  Select “ org.apache.maven.archetypes “. Click Next : Step 4 – Enter Group Id and Artifact Id . Click Finish : This creates a web project with the required directory structure: Now you can add the necessary dependencies to your pom file as explained in this post. So for example, if you are creating a Spring application, you can add Spring dependencies. You can also use this as a template to create any web application with Maven support. Further Learning Apache Maven Beginner to Gur...

Git Branch and Fork Differences explained

In this article, we will learn about the differences between git fork and branch. To learn about some Git terms you can refer to this blog post. What is a Git branch? A branch represents a line of development. Often, developers use different branches for production releases, bug fixes, etc. Having code in different branches enables developers to make changes to code without messing up the main line of development. What is a Git fork? Forking is the process of copying someone else’s repository into your own account. So, when you fork a repository, a copy of the entire repository is available in your own account. Forking is typically used when developers want to make changes to someone else’s repository for their own purpose. Forking does not in any way affect the original repository. Developers can however submit their changes to the project owner by creating a pull request. Differences between a branch and a fork The following table lists the differences between a branch and a fork: E...

Maven Wrapper - Need, Installation and Benefits

Maven is a build management system. The Maven wrapper can be used to install the right version of Maven. In this article, we will be learning about the wrapper and its benefits. Need for Maven Wrapper Normally, applications require specific Maven versions for the application to run successfully. So, developers need to ensure that the right Maven version is installed and available on the path. The Maven wrapper solves this issue. Using the wrapper, you just need to specify the desired Maven version. The wrapper then automatically installs the specified Maven version and makes it available on the path. Wrapper Installation Steps Let us first learn about the steps to install the wrapper. Open a command prompt/terminal window and navigate to the directory where you want to install the wrapper Run the following command: mvn -N io.takari:maven:wrapper 3. This command installs files related to the wrapper. So, you will see the following files/folders: .mvnw – This folder contains wrapper...

Git commands CheatSheet - List of most used Git commands

In this article, I will be listing some of the important Git commands along with their syntax and examples. This article is aimed to be a Git commands cheatsheet for developers. Important Git Commands git init The git init command creates a local repository.  It creates a hidden .git folder that represents the local repository . Syntax git init Examples git init      # Initializes the current directory as a git repository git init <path> # Initializes the specified directory as a git repository git add The git add command adds files to the git staging area Syntax git add <filename> Examples git add Hello.java # adds Hello.java git add . # adds everything from the current directory recursively OR git add -A # same as git add . git commit The git commit command adds files to the local repository . You can use it to add a single file as well as a bunch of files. Syntax git commit -m "<msg>" Examples git commit -m "Initial commit" git commit -am ...

How to Create a Remote Github repository

Image
In this article, I will be listing down a few basic instructions to create a remote repository in git. Creating a remote Git repository is a two-step process. First, you need to create a remote repository on your Git hosting system (Github) and then you can push your local changes to it. Let us now take a look at these steps. Creating a repository on Github Step 1: Go to Github.com. Sign in/Sign up Step 2: Click on the “+” sign in the top right and click New Repository: Step 3: In the new repository page, enter the name ( github-demo ). Click on the checkbox to “ Add a Readme ” file. Add a description if required. Click on “Create Repository” : This creates a repository as follows:   Create a Local Repository The next step is to create a local git repository. For this, you need to follow the steps given below. Step 4: Open a command prompt/terminal window/git bash prompt Step 5: Clone the newly created repository by running the following command...

TestNG Annotations Explained In Detail

In this article, I will be explaining some of the important TestNG annotations. Introduction TestNG is a popular unit testing framework. It allows creating and running tests. Though largely inspired by JUNIT, it has many advanced features like generating HTML reports, running parametrized tests, and so on. One of the key features of TestNG is its ability to be configured via annotations. Annotations can be embedded within the TestNG code and help to control the flow of test execution. TestNG Annotations Let us now take a closer look at the TestNG annotations. @Test Designates a method as a TestNG test. Thus, you need to specify this annotation on any method that you want to run as a TestNG test. The @Test annotation has several attributes. Some of these are as follows: group: Specifies the group that the test belongs to. (TestNG allows creating test groups, so you can group related tests and run them in one go) description: Specifies the test description enabled: Specifies whether tes...

Some Git Terminologies Explained in Brief

In this article, I will try to explain some of the important terminologies associated with Git. Some Git Terms Working Directory Working directory refers to a project folder on the file system. It may or may not be tracked by Git. Staging Area A staging area represents the files that are ready to be added to a repository. Typically, projects include dynamically generated files like class files, test reports, etc. These are not added to a repository, only the source code/configuration files are added to a repository. Thus, whenever you want to add some code to a repository, it first needs to be added to the staging area, from where it can be committed to the repository. The process of adding code to the staging area is known as indexing . Local Repository As explained in my Git v/s Github article, Git maintains a local repository where it keeps track of all the changes made to the project on the local system. Remote Repository The repository on your repository hosting system like Githu...

Git and Github Differences Explained In Short

Often, the terms Git and Github are used interchangeably. Those new to Git and Github often find this confusing. In this post, I will try to clear some confusion. What is Git? In very simple terms, Git is a local version control system. Unlike other version control systems like SVN and CVS, it creates a local repository. It can be used to track changes to files. What is Github? Github is a git repository hosting service. It can be used to share git repositories across teams.  In addition to Github, there are other git repository hosting services like Gitlab, Bitbucket, etc. Git and Github Differences The following table lists the differences between Git and Github: Edit Git Github Version control system that can be used to track changes to files Git repository hosting service that can be used to share git repositories across teams. Is a command line tool Has a Graphical User Interface Needs to be installed locally on the system Does not require any additional software to be ins...

Create a local repository in Git with step by step commands

In this article, I will be listing down a few basic instructions to create a local repository in git. Step 1: Open a command prompt/terminal window. Navigate to the directory where you want to create a git repository. Step 2: Initialize the directory as a local repository using the following command: git init Step 3: Add the files from the current directory to the local repository: git add .   Step 4: Commit files to your local repository: git commit -m "Initial commit" That is all!

How to use TestNG with Maven and Eclipse

Image
Earlier, we had seen how to use JUNIT for unit testing. TestNG is another such unit testing framework. In this article, we will be learning how to use TestNG with Maven and Eclipse. TestNG Overview TestNG is a very powerful unit testing framework. Not only does it allow creating and running tests, but it has many advanced features like generating test reports, parameterizing tests, and much more. Let us now learn how to use TestNG to test a simple Java program. Project Creation and Setup  Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the TestNG dependencies (You can refer to this blog post). Alternatively, you can add the following to your pom.xml file: <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope></dependency> Writing and Running Code Step 3 – Write code to be tested...

How to add Days to a Date in Java with code samples

Very often, when programmers perform date manipulation, they are required to add a specified number of days to a given date. In this article, we will learn how to add days to a Date in Java. Using LocalDate The LocalDate class makes it very easy to add days to a Date. Let us take a look at a few examples. Adding days to a LocalDate The following code sample demonstrates adding days to a LocalDate: LocalDate date1 = LocalDate.of(2018, 05, 10);System.out.println("Before adding days:"+date1);LocalDate date2 = date1.plusDays(6);System.out.println("After adding days:"+date2); So in this case, the code above invokes the LocalDate.plusDays method. This adds the specified number of days to the LocalDate object on which it is invoked and returns and updated LocalDate. So, this code prints the following output: Before adding days:2018-05-10After adding days:2018-05-16 Similarly, the LocalDate class has a minusDays method, that subtracts the specified number of days from a ...

How to read a file on the classpath in Java

Image
There are often programming situations when you need to read a file on the classpath via Java. In this article, I will show you how to write code for this. Reading a file on the classpath Let us first take a look at the code that you need to write to read a file on the classpath. public class ReadFileFromClassPathDemo { public static void main(String[] args) throws IOException { // read in the file from the resources directory ClassLoader classLoader = ReadFileFromClassPathDemo.class.getClassLoader(); URL url = classLoader.getResource("input.txt"); String fileName = url.getFile(); File file = new File(fileName); String str = new String(Files.readAllBytes(file.toPath())); System.out.println(str); }} Java has an in-built class called ClassLoader .  It is used to dynamically load classes. It can also be used to load resources from the classpath. The code above first obtains a ClassLoader . It then invokes the getResource method on the classLoader . This l...

How to use Flyway with Spring Boot with code samples

Image
At its core, Flyway is a version control system for databases. It allows applying incremental changes to a database. In this article, we will be learning how to use Flyway with a Spring Boot application. Flyway Overview As explained earlier, Flyway is a version control system for databases. To explain in very simple words, it allows specifying database DDL/DML in an SQL file (also known as a migration). It then applies these changes to the database. In case you need to apply further changes, you need to specify another migration with a higher version number with the incremental changes. Flyway then applies the new changes to the database. Let us now understand how to use Flyway with Spring Boot and a MySQL database. Project Creation and Setup  Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the Spring Boot, Flyway, and MySQL dependencies . So, the  pom.xml  file should be similar to the following: <proj...

Tomcat Maven Plugin with code sample

Image
The Tomcat Maven plugin helps to run a Java web application in an embedded Tomcat container. Thus, this allows developers to quickly develop and build a web application without having to install Tomcat. In this article, we will be learning how to use this plugin. Creating a Maven Project Let us first create a simple Maven web project. I will be using Eclipse IDE. In order to create a Maven web project in Eclipse, you need to follow the steps given below: Step 1 – Click on File > New > Other . Click on Maven Project . Click Next : Step 2 – The following screen is displayed. Click Next : Step 3 – In the Filter text box, type “maven-archetype-webapp” .  Select “ org.apache.maven.archetypes “. Click Next : Step 4 – Enter Group Id and Artifact Id . Click Finish : This creates a web project with the required directory structure. It also has a basic hello world page:   Adding the Tomcat plugin In order to support the Tomcat Maven plug...