Skip to main content

Posts

Showing posts from November, 2016

Gradle : How to run a specific gradle task or multiple tasks

How to run a specific gradle task When you want to run a particular task only then you can run that task using the following command. gradle -q <task-name> -q option tells gradle that you want to run only the tasks that are provided. In this example we have instructed gradle to run only one task How to run multiple gradle tasks Multilple gradle tasks can be run using the following syntax. gradle -q <task-name> <task-name> <task-name> In second example we have told gradle to run 3 tasks, they are run in the order they are provided. However if one of the task is dependent on any other task, then that task will execute first. Also A task is run only one time even if it is specified multiple times. For example if we have the following tasks in build.gradle task A { doLast { println 'Running task A' } doFirst{ println(" Pring first") } } task B(dependsOn: A) { doLast { println 'Running task B'

Gradle : Difference between doFirst() and doLast() methods

Point to Remember doFirst() and doLast() are the methods of org.gradle.api.Task interface. Both these methods take a Closure as a parameter. When a task is called this closure is called. A Gradle task is sequence of Action objects and when a task is run all these actions are called in a sequence by calling Action.execute(T) What are doFirst() and doLast() methods in a Gradle task. doFirst(Closure action) This method will add your closure as an action to the beginning of the action sequence. All the code that is written in this closure is executed first when the task is executed. doLast(Closure action) This method will add your closure as an action to the end of the action sequence. All the code in this closure is executed in last when the task is executed. Let's take a look at the following example. task A { doLast { println("Running task A :: LAST") } doFirst{ println("Running task A :: FIRST ") } } When we run this task usin

Gradle : How to continue a build even if a failure occurs

How to continue a build even if a failure occurs Many a times there is a need to continue running a build process even if some of the tasks fails. To run a build even if the some of the tasks fail we can use the --continue switch. It will execute all tasks even if some of them fails. Normal behavior is that build stops as soon as first failure is encountered. It will show the report of encountered failures at the end of the build. When a task fails all the tasks that depends on this task will not be executed . This is because, if compilation fails then, there is no point of packaging the project. Now lets take see a few examples build.gradle group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' } task failure(){ doLast{ println " I am going to fail now" throw ne

Gradle : What is a Gradle Task and how to write a custom task

What is a Gradle Task. Lets first create a gradle file build.gradle with the following contents in it. group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' } Now, lets run the the following command from terminal in the root directory where build.gradle is saved. gradle tasks It will give the list of all the tasks that are defined. It includes both gradle default tasks and custom tasks . Now, you can run any defined task using the command. gradle <task-name> How to write a Custom Task in Gradle. A task in gradle is an imlpementation of org.gradle.api.Task interface. Task interface has two methods doLast() and doFirst() which can define the order of task execution. doFirst(Closure action) - This adds the given closure to the beginning of this task's action list. doLast(Closu

Gradle : How to import a gradle file in another gradle file

How to Import Gradle file in another gradle file. To import a gradle file in a gradle file you can use the following command in .gradle . We have a gradle file build.gradle and we are including two other gradle files tasks.gradle and test.gradle . apply from : 'tasks.gradle' apply from : 'https://ekiras.github.io/temp/test.gradle' Here, in line 1, we have imported tasks.gradle which lies in the same directory as build.gradle and in line 2 we have imported a test.gradle from a remote server. (Git hub pages in this case, it can be an s3 url or your server file url) Test Imported gradle file task build.gradle has the following content group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'java' apply from : 'tasks.gradle' apply from : 'https://ekiras.github.io/temp/test.gradle' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: &

Java 9 : How to Terminating or Destroy a Running Process

Points To Remember You cannot destroy or terminate the current process not even forcefully. You can destroy a process by using the methods destroy() or forcefully using destroyForcibly() . Read More Java 9 Feature List all processes running on the OS Get the information of the current process Start a new Process and get its Process Id How to get Process Information from Process Id Destroy a running process Destroy a Process There are two methods in interface ProcessHandle that can be used to destroy a process. destroy() - to destroy a process noramally destroyForcibly() - to destroy a process forcefully. However the process may not be terminatted instantly since operating system access controls may prevent the process from being killed thus resulting in case where isAlive() method may return true for a brief period after destroyForcibly() is called. In the example below we will first create a process and then kill it forcefully. import java.io.IOException; import java.lang.Pro

Java 9 : How to get Process Information from Process Id in Java

Points to Remember You need to run this code with Java 1.9 ProcessHandle.of(Long processId) method will return an object of Optional<ProcessHandle> . Optional is a class introduced in java 8 which is a container object which may or may not contain a non-null value . It has methods boolean isPresent() which returns true if a value is present else returns false. T get() method returns the object if present or else throws NoSuchElementException . Read More Java 9 Feature List all processes running on the OS Get the information of the current process Start a new Process and get its Process Id How to get Process Information from Process Id Destroy a running process Get Process Information from Process Id If we have the process id of a process, then we can get the information about the process as shown in the following code. import java.io.IOException; import java.lang.ProcessHandle; import java.lang.Process; /** * @author ekiras */ public class DestroyProcess { publ

Java 9 : How to start a new Process and get process id

Points to Remember This post used Java 1.9 since ProcessHandle was introduced in Java 1.9. Read More Java 9 Feature List all processes running on the OS Get the information of the current process Start a new Process and get its Process Id How to get Process Information from Process Id Destroy a running process Get the Current Process Information. In this example we will try to get information about the current process. For this we will use the m import java.io.IOException; /** * @author ekiras */ public class StartProcess { public static void main (String[] args) { try { Process process = startProcess( "tail -F /dev/null" ); printProcessInfo(process.toHandle()); } catch (IOException e) { e.printStackTrace(); } } public static void printProcessInfo (ProcessHandle processHandle) { System.out.println( "---Process Info---" ); System.out.println( " Process Id :

Java 9 : How to get Current Process Information

Points to Remember Java 9 introduced the new Process Api in java.lang package. ProcessHandle interface can be used to perform operations with processes like start, destroy, list processes. Read More Get the information of the current process Create a new process and then terminate it Terminating the current process Destroy a process forcefully Get children of a process Get the Current Process Information. In this example we will try to get information about the current process. For this we will use the method ProcessHandle.current() method, this will return the object ProcessHandle which can be used to get the information of the process. import java.io.IOException; import java.lang.ProcessHandle; /** * @author ekiras */ public class CurrentProcess { public static void main (String[] args) throws IOException { currentProcessInfo(); } public static void currentProcessInfo () { System.out.println( "Current Process : " );

Java 9 : How to list all processes running on the OS

Points to Remember Java 9 introduced the new Process Api in java.lang package. ProcessHandle interface can be used to perform operations with processes like start, destroy, list processes. Read More Java 9 Feature List all processes running on the OS Get the information of the current process Start a new Process and get its Process Id How to get Process Information from Process Id Destroy a running process List all Processes running on the OS. To get all the processes running on the OS you can use the static method allProcesses() of the ProcessHandle , this will return a Stream<ProcessHandle> stream of process handles which can be used to get information about each process. The below example shows how we can list all the running processes. import java.lang.ProcessHandle; import java.util.stream.Stream; /** * @author ekiras */ public class ListAllProcesses { public static void main (String[] args) { allProcesses(); } public static void allPr

Java 9 : How to use Java 9 Process Api

Introduction to Java 9 Process Api The New Java 9 Processes API will help provide Identifies the processess Control of Native processes Monitoring of processes List Children of processess Start a new Process Destroy a running process Access to the process input,output and error streams. On Exit Handle when a process is destroyed or completed. Testing Process API operations In this post java program we are testing the following operations List all processess running in the OS. Print the information of the current process. Create a new process and then terminate it. Do some action when the process is destroyed or completed. Try to terminate the current process. Destroy a process forcefully Get children of a process. List all Processess ProcessHandle.allProcesses() is a static method in ProcessHandle interface that returns Stream<ProcessHandle> object. We will iterate this stream to print the information of all the processess running. package com.ekiras.java9.processapi; import

Java 9 : How to create and run a custom Module

What is a Module in Java 9 ? Java Modules are a part of Project Jigsaw . A module is a named, self-describing collection of code and data . Its code is organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information. What are the Type of Modules in Java 9 ? There are two types of Java Modules Java Standard Modules , they start with string java. Java Non-Standard Modules , they start with string jdk. Creating a Custom Module. We can make the following types of modules Module that is independent of all other modules. Module that requires other modules as dependencies. Module that exports itself for use by other packages. Module that imports some modules and exports some packages for other modules to use. Note When we are exporting the packages in modules, only the publically expoted modules are available to use by other modules.(We will get to this later in the post.) A simple module that does not impo

Java 9 : What is Project Jigsaw ?

Project Jigsaw : Points to Remember It is introducded in Java 9. Its main aim is to modularize the Java SE Platform. This will modularize the JDK, and we can use only those modules that we need instead of the full JDK installation. Scale platform to run on smaller applications and devices. Also Read Creating Custom Java Modules Design Implementations of Project Jigsaw. New java commands related to Project Jigsaw added are the following --module-path <module path>... A : separated list of directories, each directory is a directory of modules. --upgrade-module-path <module path>... A : separated list of directories, each directory is a directory of modules that replace upgradeable modules in the runtime image -m <module>[/<mainclass>] --module <modulename>[/<mainclass>] the initial module to resolve, and the name of the main cla

SpringDataJPA : One to Many Mapping in Spring Boot Hibernate JPA with Spring Data

Also Read One To One Mapping Let's create two entities/domains Employee and Department such that they have the following relation between them. Department has-many Employees which means an Employee can belong to only one Department and a Department can have many Employee and Department is the owner of the relation between the two. Owner of the relation means that Owner can exist without the dependent entity but dependent entity cannot stay without the owner entity . Dependent Entity of Relationship will containes the 'foreign key' ID of the Owner entity . In this case, Address will contian the Employee Id in >its table as shown in the table structure below. So a Department can exist without Employee but an Employee cannot be there without a Department. If you on deleting an Employee, the Employee-Department mapping will be removed on deleting a Department, all the employees in the department should be deleted. Employee.java package com.ekiras.domain; import jav