Gradle Examples
Gradle Examples
Raghu
Gradle
Gradle is a build system (open source) which is used to automate building, testing, deployment
etc. “build. gradle” are scripts where one can automate the tasks.
Highly customizable — Gradle is modeled in a way that is customizable and extensible in the
most fundamental ways.
Fast — Gradle completes tasks quickly by reusing outputs from previous executions,
processing only inputs that changed, and executing tasks in parallel.
Powerful — Gradle is the official build tool for Android, and comes with support for many
popular languages and technologies.
Widely used at: Java, Android, C++, Kotlin, Groovey, Scala and Java Script
Maven Gradle
It is a software project management system It is a build automation system that uses a
that is primarily used for java projects. Groovy-based DSL (domain-specific
language )
It uses an XML file for declaring the project, It does not use an XML file for declaring the
its dependencies, the build order, and its project configuration.
required plugin.
In maven, the main goal is related to the In Gradle, the main goal is to add
project phase. functionality to the project.
It does not use the build cache; thus, its It avoids the work by tracking input and
build time is slower than Gradle. output tasks and only runs the tasks that
have been changed. Therefore it gives a
faster performance.
Maven has a limited number of parameters Gradle is highly customizable; it provides a
and requirements, so customization is a bit wide range of IDE support custom builds.
complicated.
The compilation is mandatory in Maven. Gradle avoids the compilation of Java.
1|Page
Mr. Raghu
JDBC Application
1. build.gradle
plugins {
id 'java'
}
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
implementation 'com.mysql:mysql-connector-j:8.0.32'
}
Test.java
package com.app.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
2|Page
Mr. Raghu
Hibernate Application
1. build.gradle
plugins {
id 'java'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
implementation group: 'org.hibernate', name: 'hibernate-
core', version: '5.4.32.Final'
compileOnly group: 'org.projectlombok', name: 'lombok',
version: '1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
runtimeOnly group: 'mysql', name: 'mysql-connector-java',
version: '8.0.25'
}
/*jar {
manifest {
attributes 'Main-Class': 'com.app.raghu.Test'
}
}*/
3|Page
Mr. Raghu
2. Entity class
package com.app.raghu;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name="prodtab")
public class Product {
@Id
@Column(name="pid")
private Integer prodId;
@Column(name="pcode")
private String prodCode;
@Column(name="pcost")
private double prodCost;
}
3. hibernate.cfg.xml
4|Page
Mr. Raghu
<property name="hiberante.dialect">
org.hibernate.dialect.MySQL8Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property
name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.app.raghu.Product" />
</session-factory>
</hibernate-configuration>
4. Test class
package com.app.raghu;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
ses.saveOrUpdate(p);
tx.commit();
ses.close();
}
}
Servlets Application
5|Page
Mr. Raghu
1. build.gradle
plugins {
id 'java'
id 'war'
id "org.gretty" version "3.0.5"
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compileOnly group: 'javax.servlet', name: 'javax.servlet-
api', version: '3.1.0'
}
war {
archiveName = 'sample.war'
}
gretty {
httpPort = 8080
contextPath = '/'
servletContainer = 'jetty9'
}
2. Welcome Servlet
package com.app.raghu;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/welcome")
6|Page
Mr. Raghu
public class WelcomeServlet extends HttpServlet {
plugins {
id 'java'
}
sourceCompatibility = 11
targetCompatibility = 11
repositories {
//jcenter()
mavenCentral()
}
dependencies {
//spring context
implementation group: 'org.springframework', name: 'spring-
context', version: '5.3.6'
// project lombok
compileOnly group: 'org.projectlombok', name: 'lombok', version:
'1.18.20'
}
7|Page
Mr. Raghu
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
3. Spring Bean
package com.app.raghu;
import lombok.Data;
@Data
public class Employee {
4. Test class
package com.app.raghu;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
Scopes:
compileOnly : A Jar is used only at compile time (ex: Lombok)
implementation: A Jar is used at compile and runtime (ex: hibernate, spring ..etc)
testImplementation: A Jar is used only at Executing Test cases (Ex: Junit)
runtimeOnly: A Jar is used only at running application (ex: Mysql, Ojdbc ..etc)
8|Page